List of frequently used Git CLI commands
Global Information Tracker (Git) is the ubiquitous tool used by several companies and individuals for version control. It is free and open source. Git tracks the changes you make to files and also makes collaboration easier, allowing changes by multiple users to be merged into one source.
GitHub is a cloud-based hosting service that lets you manage Git repositories. While Git is a command line tool, GitHub provides a web-based graphical interface. Tools like Bitbucket and TortoiseGit are other widely used code collaboration and version control tools that integrate with Git repositories.
You can work on Git repositories using command line or through GUIs provided by tools like GitHub and TortoiseGit. Developers might be very comfortable using CLI for Git operations. Others usually use a combination of GUI and CLI. Here I am listing some of the commonly used Git CLI commands for operations like merge, rebase and resolving merge conflicts.
- To list all branches in your repo —
git branch
- To clone the repository into the folder on the local machine —
git clone <repo> <folder>
- To checkout the remote branch say master like a local branch —
git checkout master
- To create a new branch locally –
git branch <new-branch> or git checkout -b <new-branch>
- To checkout a new branch —
git checkout -b <branch> or git checkout -b <new-branch>
(checkout is used to switch branches) - To upstream —
git push --set-upstream origin <branch> or git push -u origin <branch>
(to set the default remote branch for your current local branch so that git pull and git push works) - To duplicate a branch —
git checkout -b <new-branch> <old-branch>
- To delete local copy of the branch without merge —
git branch -d <branch> or git branch -D <branch>
(deletes without warning message) - To delete branch from the remote —
git push origin --delete <branch> or git push origin :<branch>
- To get status of a branch —
git status
- To get log of a branch —
git log
- Steps to update changes from master/release to the branch –
git checkout master
git pull
git checkout <branch>
git merge master
git push
Here to add a commit message while merging, press i
to insert comment, and then press Esc
and enter :wq
to save and exit from the editor.
- Another method to keep the branch updated and in sync with the master
git checkout master
git pull
git checkout branch
git pull
git fetch origin master or git fetch upstream master
git rebase upstream/master or git merge origin/master -m 'master sync'
git push origin branch
- To add all the branch commits over the merge —
git pull –-rebase
- To reset your local master/branch to the remote branch last commit —
git reset --hard origin/master
- To fetch contents of a remote branch before checking out —
git fetch --all
- To update the local representation of the remote branch and prune deleted branches —
git fetch -p
Prior to merge:
Confirm the receiving branch — git checkout <receiving branch>
Fetch latest remote commits — git fetch
Ensure receiving branch has latest updates — git pull
To merge — git merge <branch to be merged>
Fast-forward merge occurs when there is no diverge between the two branches and the path is linear.
Another method to resolve conflicts using command line is — git mergetool
It will bring up the different versions of the file in different Vim splits panels.
+ — — — — — — — — — — — — — — — — +
| LOCAL | BASE | REMOTE |
+ — — — — — — — — — — — — — — — — +
| MERGED |
+ — — — — — — — — — — — — — — — — +
You can navigate among these views using ctrl+w
.
If you want to get changes from REMOTE type :diffg RE
If you want to get changes from BASE type :diffg BA
If you want to get changes from LOCAL type :diffg LO
Type :wqa
to save the changes and quit vi
Commit the merge. Use git clean
to remove the untracked files (*.orig) created by the tool.
Compare Git Commands
git fetch and git pull —
git fetch
is similar to pull but doesn’t merge.git pull
gets data from remote and instantly merges.
git merge and git rebase —
git rebase
moves a feature branch into a master. It combines intermediate commits into a single commit.git merge
adds a new commit preserving the complete history of commits.
Common Issues
Here are two common issues that you may encounter while using Git for updating files in local or remote repositories.
Your branch is ahead of ‘origin/master’ by X commits:
You might get this message if you made changes in your local master and didn’t push them to remote. One way to resolve this issue is to push your local changes by using the command, git push origin
If you want to discard your local changes and bring it in synch with the master then use the command, git reset --hard origin/master
Your branch and ‘origin/branch’ have diverged:
Make sure that your local branch is not behind the remote when you push changes from local to the remote branch.
To eliminate this issue do - git pull origin <branch>
and then do git push origin <branch>
If you want to override the remote branch changes with your local branch, use — git push -f origin <branch>
For a comprehensive Git tutorial you can refer the Atlassian guide and the Git documentation.