10 essential git Commands Every Developer Should Know

salahuddin SK 23/09/2024 3 min read
10 essential git Commands Every Developer Should Know

Git is an essential tool for developers to manage their source code. Mastering Git commands will significantly improve your productivity when working on projects, especially in a collaborative environment. Below are the 10 must-know Git commands that every developer should be familiar with.

1. git init

Usage: This command is used to initialize a new Git repository. It creates a hidden .git directory in the current folder, allowing you to start tracking changes.

git init

Example: After running git init in a folder, Git will begin tracking changes in that folder.

2. git clone

Usage: This command is used to copy a Git repository from a remote server (e.g., GitHub, GitLab) to your local machine. It also initializes the repository locally.

git clone https://github.com/username/repository.git

Example: Cloning a repository from GitHub will download all the files and the commit history to your local machine.

3. git status

Usage: The git status command displays the current state of your working directory and staging area. It shows which files have been modified, added, or deleted.

git status

Example: Running git status will show you if any files are staged for commit or untracked changes in the repository.

4. git add

Usage: Use git add to add changes in the working directory to the staging area, preparing them to be committed. You can add individual files or entire directories.

git add filename.txt

Example: To add all files at once, you can use git add . to stage all modified and new files in the working directory.

5. git commit

Usage: The git commit command records changes in the repository's history. Commits include a descriptive message to explain the changes.

git commit -m \"Your commit message\"

Example: Committing after staging changes will save the snapshot of the repository's current state.

6. git push

Usage: This command sends your committed changes to a remote repository (e.g., GitHub). It's used to sync your local repository with the remote repository.

git push origin main

Example: Pushing changes to the main branch will upload your local changes to the remote repository.

7. git pull

Usage: The git pull command fetches and integrates changes from the remote repository into your local repository. It is essentially a combination of git fetch and git merge.

git pull origin main

Example: Pulling changes from the main branch will update your local repository with any changes made in the remote repository.

8. git branch

Usage: This command lists all the branches in your repository. You can also use it to create or delete branches.

git branch

Example: To create a new branch, you can use git branch feature-branch. To switch to that branch, use git checkout feature-branch.

9. git checkout

Usage: The git checkout command is used to switch between branches or restore files in your working directory.

git checkout feature-branch

Example: This command allows you to work on different features or stages of your project by switching between branches.

10. git merge

Usage: This command is used to combine the changes from one branch into another. Typically, you would use this to merge a feature branch into the main branch after completing a feature.

git merge feature-branch

Example: Merging the feature-branch into the main branch combines the changes and updates the history.

These 10 Git commands are the foundation for efficiently managing version control in any project. Whether you're collaborating with a team or working alone, knowing these commands will make your development workflow smoother and more productive.

Comments

Leave a Comment