20 Git Command I Use All The Time — Git CheatSheet
In this article, I just want to lay down a quick cheat sheet. It will include commands that I shared in that first article, but it will also include some new git commands. Recently, for various senior projects, I have been collaborating with different teams. There are a few commands that have become a staple for me.
So let's get started:
1. git init
This command is used to initialize a project as a git repository.
2. git remote add origin
Example:
git remote add origin https://github.com/MrKrishnaAgarwal/Git-CheatSheet.git
This command is used to add or connect to a remote repository.
3. git remote
This command is used to view connected remote repositories.
4. git status
This command is used to view the status of files in your local repository. Are files tracked? untracked? modified?
5. git add
Example:
git add index.html
git add index.html style.css style.scss
This command is used to stage modified or untracked files.
git add -a
This command is used to stage ALL unstaged files.
6. git reset
This command is used to unstage files.
7. git commit
This command is used to commit staged files.
git commit -m “
Example:
git commit -m "added navigation bar"
This command is used to commit staged files AND provide a commit message for the commit history.
git commit --amend
The git commit --amend command is a convenient way to modify the most recent commit.
8. git push -u origin
Example:
git push -u origin master
This command is used to push committed files to the remote repository(aka GitHub) in the specified branch. Use this command for the very first time you push files to the remote repository. It will establish where you are pushing these files to. The next time(s) you push files, you can use git push
git push
This command is used to push committed files to the remote repository. You can only use this command to push files to the remote repository AFTER having pushed files using the previous command.
9. git fetch
This command is used to fetch the most updated version of your local repository. It checks for new files, new branches, deletions etc.
10. git pull
This command is used to take that information you just fetched and pull it into your local repository. This updates your local repository to the most updated version of your remote repository.
11. git rm -r — cached
Example:
git rm -r --cached config.js
This command is used to remove a file from the remote repository(GitHub) without deleting it in your local repository.
- git branch This command is used to preview the branch you are currently on
git branch -a
This command is used to preview all the branches in your local and remote repository.
git branch -r
This command is used to preview all the branches in your local repository (aka branches you have already visited).
git branch
This command is used to create a new branch in your local repository.
13. git checkout — track origin/
Example:
git checkout --track origin/develop
This command is used to switch branches. This is specifically for when you are visiting a branch (created in GitHub/remote repository) for the very first time.
14. git checkout
Example:
git checkout master
git checkout develop
This command is used to switch to branches you have already visited before.
15. git merge
This command is used to merge two branches together. To do this, enter the branch that you want to inherit the changes. And the branch name you would use along with this command is the branch that will provide the changes.
Example: master branch will inherit code from develop branch
git merge develop
16. git merge — abort
This command is used to abort a merge.
If there are no conflict errors, merges will always be successful. Ergo, this abort can only be used in situations where a merge failed.
How will you know this can be used?
For starters, your terminal will say merge failed. It may also tell you to fix the merge conflicts.
Look at the very end of the first line. In parentheses, it says (master)
. This is because we are in the master branch. If you are in the develop branch, it would say (develop)
.
If your merge fails, it will say (master|merging)
or something like that. Maybe it says merge or maybe it’s a forward slash or maybe you’re in another branch. Regardless, you get the idea.
This indicates your merge failed.
git merge --abort
would just abort the merge entirely.
17. git merge -X theirs
Example:
git merge -X theirs develop
This command is used to merge two branches together. And if there are merging conflicts, this command will just assume that you’d prefer the changes made in the mentioned branch (rather than the current one).
18. git reset — hard HEAD
This command will erase all the changes you’ve made in your local repository and update it to the latest version that was committed to GitHub.
19. git clean -f
This command is used to delete untracked files in your local repository
20. git clean -d
This command is used to delete untracked directories in your local repository. You can also combine it into git clean -fd to do both.
- Suggested by people in comments
21. git bisect
The git bisect command is used to discover the commit that has introduced a bug in the code. It helps track down the commit where the code works and the commit where it does not, hence, tracking down the commit that introduced the bug into the code.
22. git reset HEAD^
This command moves the current branch backward by two commits, effectively removing the two snapshots we just created from the project history. it reverts an accidental commit and keep the changes.
23. git diff
Diff command is used in git to track the difference between the changes made on a file.
24. git rebase
Rebasing is especially useful if you worked on a branch, but then need to apply the changes of that branch on top of the latest main or release branch. With a git rebase you can "move" your branch on top of the latest release. It is also useful if the team (or you yourself) are following common conventions for commits, like squashing commits together or splitting out "big" commits into "smaller" ones. It's basically for "reorganizing" your commits.
git rebase -i HEAD~N
To rebase the last N commits interactively. From there you can decide what action to apply on each commit: pick, reword, squash etc.
25. git stash
Stashing is good when you are working on a branch, but then you need to switch to another branch, but you're not ready yet to make a full commit on your current branch because the current code is "messy". So you stash them, switch to the other branch, come back to your original branch, then unstash your changes.
26. git reflog
git reflog is important when you did something wrong and really messed up your commits.
27. git cherry-pick
Cherry picking in Git means to choose a commit from one branch and apply it onto another.
This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch.
git cherry-pick <commit-hash>
Thank you for reading! If you have some commands that you use all the time, please share them!
If you found this helpful, make sure to show your support with a like, and if you want to help others in their projects, a share would be greatly appreciated! Let me know what you think about this! Happy Learning!
More commands can be found here:
Follow me on GitHub and DEV.
Top comments (75)
I sometimes use
git rebase -i HEAD~N
to rebase the last N commits interactively. From there you can decide what action to apply on each commit: pick, reword, squash etc.Some open source projects enforce single commit pull requests so it comes handy then and makes it easy to squash commits.
Interaction for the win.
Interactively Rebase with git
Jesse Phillips ・ Jan 24 '20 ・ 3 min read
Thanks for sharing
Thanks
I'll add it
git fetch && git rebase origin/main && git push --force
This is basically what I do to "merge" from main into a dev branch. But I find the results are usually much better than using
git merge
. Easier conflict resolution process and clean commit history. But as always when rebasing and force-pushing, you have to consider the pain it may cause to others working in the same branch (but if it's just you - go for it!)Don't use the
--force
flag, even in that situation. Use--force-with-lease
for the rare case where you do end up with a remote change during the rebase.That should be named --nudge
Or change to --force --force when you really mean it.
Yes
Right
Thanks for sharing
You are absolutely right.
We should avoid using force.
But thanks for your suggestions 👍
Good list!
Though I do think the 'git checkout' is a bit dated.
'git switch' is able to switch branches and it's easier to remember. It will also automatically pull remote branches if you switch to something on the remote.
git-scm.com/docs/git-switch
'git checkout' I only use to checkout a specific commit hash if I need to do that
git switch --detach <hash>
Yes
Thanks for sharing
Thanks I'll update that.
Most important one: git bisect!
Added in the post
Thanks
git add .
actually only adds all the files in the current folder (.
) which means if you are in a subdirectory this can potentially not add a chance you made. What really adds ALL changes isgit add -a
Okay
Thanks a lot
I'll update that one
There's a certain distinction between "should now" and "use regularly", at least for me as I switched to GUI tool for better representation of git tree.
On regular basis, I use only these commands:
git rebase -i
- most used command to amend whole branch (remove commits, edit commit message and many more)git reset HEAD^1
- to get code back last commit changes into working tree and continue working - as a development strategy doing backup and WIP commitgit branch --contains <hash>
- to check in which branches given commit is included (really helps if there are lots of staging/demo branches apart from master branch)git log -G<string> --all
- use lots of time to search code in git historyAlso, if you're making a list of commands one should know, this list certainly is missing a
git diff
command.Cheers!
Added in the post.
Sorry and Thanks
Nothing to be sorry about! Cheers!
Thanks 💯
Just a suggestion: In some of your headers, the dash dash (--) is being converted to a single en dash ( — ), probably automatically by your word processor.
Example:
git rm -r — cached
should begit rm -r --cached
Yes
I'll update that one
Thanks a lot
Very useful article. All the commands are indeed the important ones. One suggestion I have is that for many commands you've just put abstract/generic description which does not explain a reader as to what that commit is used for exactly.
For example: git reflog - you mentioned why it is important but adding an additional line explaining the details would have been better.
Thanks
I'll definitely look upon it
I also find the command:
git reset HEAD^
really useful too, lets you revert an accidental commit and keep the changes :)Added in the post
Thanks
Interesting, I never used git branch -r (thank's for the tip ^^).
I don't think everyone should know all them, but indeed it's always useful to know that they exist (and that's not a problem if you don't know the syntax )
I realized that in my daily life, I'm using a lot a GUI for all the branch, status, commit, and merge stuff
And for some obscure reason I only use git stash on cli ...
Welcome!
Glad you liked them :)