50+ GIT Commands with Downloadable Cheat Sheet
GIT is unarguably the most powerful, feature-rich, and stable version control system (VCS) out there. It’s a distributed VCS, which means that all contributors have a copy of the entire repository, along with all the change history.
This makes it easier for different people to simultaneously work on a project, without encountering conflicts. It also means that there is no single point of failure, i.e. even if the repository server crashes, a copy from any developer machine can be used to restore it.
If you do a
man git on your machine, it will show you hundreds of commands, along with their descriptions. This can be overwhelming, especially if you are just getting started. The good part is that you don’t need to know all of these commands, just a small subset.
In this article, we will be sharing a GIT cheat sheet with the top 50 commands every GIT user should know.
Let’s start with the very basics.
git clone <URL>
git add <file>
git stash pop
git remote add origin <URL>
git config --global user.email firstname.lastname@example.org
git config --global user.name "user name"
GIT maintains a log of all the changes/snapshots/commits ever made to a repository. You can switch back to a commit at any time, or undo a change you made in your working directory. Here are a few commands to remember in this regard:
git revert <commit hash>
git reset <file>
git reset --hard <commit hash>
git checkout <commit hash>
git reset --hard HEAD~2
git clean -n
git clean –f.
git clean -f
git clean –n command to see which files will be removed.
git rm –r <file or directory name>
Branching is arguably GIT’s strongest feature. You can think of a branch as an independent copy of the original repository. Using branches, you can start developing new features and push them to the remote server, without changing anything in the original repository.
When a feature has been fully developed and tested, the branch can be merged to the main repository. Here are a few important branching commands to know:
git branch <branch name>
git checkout <branch name>
git merge <branch name>
git branch –d <branch name>
git push origin --delete <branch name>
git checkout -b <branch name> origin/<branch name>
git merge <source branch> <target branch>
git branch -m <old name> <new name>
git rebase <branch name>
Here is a list of commands you can use to monitor and inspect GIT history:
git log --summary
git log --oneline --graph --decorate
git log --after="2022-7-1"
git log --oneline <commit hash 1>..<commit hash 2>
Conflict Resolution Commands
Now let’s talk about that part of GIT that no one likes: resolving conflicts. Conflicts arise when more than one people change the same line in the same file, or if one person deletes a file while another modifies it. Most people encounter conflicts while merging/rebasing branches. Here are a few commands that can reduce the pain of resolving conflicts:
git config merge.tool vimdiff
git checkout --ours filename.php
git checkout --theirs filename.php
git merge --abort
git config merge.conflictstyle diff3
diff3 merge style, which is much more intuitive than the default one.
To finish off our GIT cheat sheet, here are some other useful commands that can come in handy:
git push --set-upstream origin <branch name>
git reset --soft HEAD~1
git cherry-pick <commit hash>
git checkout <branch name> /path/to/folder/*
git commit --amend -m "New commit message"
git mv <old name> <new name>
git config --global core.excludesfile <file>
git diff branchX...branchY
git update-index --assume-unchanged /path/to/file
Downloadable Cheat Sheet
Download the 2560×1440 wallpaper cheat sheet for easy reference.