10 Git commands every developer should know
Git is an essential tool for any developer, and mastering a few key commands can significantly improve your workflow and productivity.
This article will cover 10 Git commands that every developer should know. These commands will allow you to clone repositories, track and stage changes, commit and push code, merge branches, and more. Whether you are a beginner or an experienced developer, learning these commands will help you work more efficiently and effectively with Git.
git clone
:
Example 1: To create a local copy of a remote repository named “my-repo”, you can use the following command:
git clone https://github.com/username/my-repo.git
Example 2: To create a local copy of a remote repository and specify a different name for the local repository, you can use the following command:
git clone https://github.com/username/my-repo.git my-local-repo
2. git add
:
Example 1: To add a single file named “file.txt” to the staging area, you can use the following command:
git add file.txt
Example 2: To add all modified and untracked files to the staging area, you can use the following command:
git add .
3. git commit
:
Example 1: To commit all staged changes with the commit message “Added new feature”, you can use the following command:
git commit -m "Added new feature"
Example 2: To commit only a specific file, “file.txt”, with the commit message “Fixed bug in file”, you can use the following command:
git commit file.txt -m "Fixed bug in file"
4. git push
:
Example 1: To push all local commits to the remote repository named “origin”, you can use the following command:
git push origin
Example 2: To push a specific branch, “feature”, to the remote repository named “origin”, you can use the following command:
git push origin feature
5. git pull
:
Example 1: To retrieve new commits from the remote repository named “origin” and merge them into the current branch, you can use the following command:
git pull origin
Example 2: To retrieve new commits from the remote repository named “upstream” and merge them into the current branch, you can use the following command:
git pull upstream
6. git branch
:
Example 1: To create a new branch named “feature”, you can use the following command:
git branch feature
Example 2: To delete the branch named “feature”, you can use the following command:
git branch -d feature
7. git merge
:
Example 1: To merge the branch named “feature” into the current branch, you can use the following command:
git merge feature
Example 2: To merge the branch named “feature” into the branch named “development”, you can use the following command:
git checkout development
git merge feature
8. git checkout
:
Example 1: To switch to the branch named “feature”, you can use the following command:
git checkout feature
Example 2: To restore the file “file.txt” to the version in the most recent commit, you can use the following command:
git checkout HEAD -- file.txt
9. git stash
:
Example : To save all un-staged changes to a new stash
git stash
10. git reset
: This command is used to undo commits or discard changes. It can remove commits from the local repository or restore specific files to a previous version.
Example 1: To remove the most recent commit from the local repository and discard any changes, you can use the following command:
git reset HEAD~
This command will remove the commit and move the HEAD pointer to the previous commit.
Example 2: To restore the file “file.txt” to the version in the most recent commit and remove it from the staging area, you can use the following command:
git reset HEAD -- file.txt
This command will un-stage the file and restore it to the version in the most recent commit.
You can reach out to me through LinkedIn