DEV Community

Cover image for 20 Git Commands Every Developer Should Know

20 Git Commands Every Developer Should Know

Krishna Agarwal on July 21, 2022

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 s...
Collapse
 
joolsmcfly profile image
Julien Dephix

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.

Collapse
 
jessekphillips profile image
Jesse Phillips
Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks for sharing

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks
I'll add it

Collapse
 
mistval profile image
Randall

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!)

Collapse
 
fjones profile image
FJones

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.

Collapse
 
jessekphillips profile image
Jesse Phillips

That should be named --nudge

Or change to --force --force when you really mean it.

Thread Thread
 
krishnaagarwal profile image
Krishna Agarwal

Yes
Right
Thanks for sharing

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

You are absolutely right.
We should avoid using force.

But thanks for your suggestions 👍

Collapse
 
fish1 profile image
Jacob Enders

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

Collapse
 
jessekphillips profile image
Jesse Phillips

'git checkout' I only use to checkout a specific commit hash if I need to do that

git switch --detach <hash>

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Yes
Thanks for sharing

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks I'll update that.

Collapse
 
codenameone profile image
Shai Almog

Most important one: git bisect!

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Added in the post
Thanks

Collapse
 
dominik90 profile image
Dominik Bartsch

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 is git add -a

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Okay
Thanks a lot
I'll update that one

Collapse
 
banesto profile image
Ernests Kečko • Edited

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 commit
  • git 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 history

Also, if you're making a list of commands one should know, this list certainly is missing a git diff command.

Cheers!

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Added in the post.
Sorry and Thanks

Collapse
 
banesto profile image
Ernests Kečko

Nothing to be sorry about! Cheers!

Thread Thread
 
krishnaagarwal profile image
Krishna Agarwal

Thanks 💯

Collapse
 
a5dev profile image
a5dev

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 be git rm -r --cached

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Yes
I'll update that one
Thanks a lot

Collapse
 
jashgopani profile image
Jash Gopani

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.

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks
I'll definitely look upon it

Collapse
 
selectivehouse profile image
Joe Jordan

I also find the command: git reset HEAD^ really useful too, lets you revert an accidental commit and keep the changes :)

Collapse
 
krishnaagarwal profile image
Krishna Agarwal • Edited

Added in the post
Thanks

Collapse
 
eltha9 profile image
eltha9

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 ...

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Welcome!
Glad you liked them :)

Collapse
 
paulknulst profile image
Paul Knulst • Edited

This is a nice list on the basic Git commands.

I have a nice and very important addition to that list.

I recently experienced with Git Aliases and have to say that they are really awesome and should be know by everyone!

I created an article that explain how I use them to delete all my local branches. If you are interested you can read about it here in my personal blog: knulst.de/how-to-remove-local-git-...

Also it is published on Medium: betterprogramming.pub/how-to-remov...

And here on Dev.to: dev.to/paulknulst/how-to-remove-lo...

If you did not know about Git Alias it's worth reading about! (wherever you want) :)

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks
I'll check it out.

Collapse
 
binoytv9 profile image
Binoy T V

git stash
git cherry-pick

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks
I'll update the post.

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Updated
Thanks for Contributing

Collapse
 
snowlyg profile image
snowlyg • Edited

I try git branch -r, Don't show all the local branches ,but show all the remote branches.

I check git docs , -r, --remotes is use for act on remote-tracking branches.

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks
I'll add it.

Collapse
 
leob profile image
leob • Edited

git commit --amend and git merge --no-ff --no-commit are other handy ones that I use pretty often :)

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks 👍
I'll add them.

Collapse
 
stephen_dsouza profile image
Stephen D Souza

command is used to take that information you just fetched

Not sure if anyone has pointed it out but ‘git pull’ is a combo of ‘git fetch’ and ‘git merge’, so you don’t need to run a ‘git fetch’ before a pull.

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Yes, you are absolutely correct
Thanks for the point

Collapse
 
psypher1 profile image
James 'Dante' Midzi

Ever since I learnt about git add -p ive never looked back

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

👍

Collapse
 
madza profile image
Madza

Useful commands 👍✨💯

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks a lot 😊

Collapse
 
andrewbaisden profile image
Andrew Baisden

git stash is a life saver.

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

True

Collapse
 
wjplatformer profile image
Wj

Perfect execution ✅🤣

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks :)

Collapse
 
ajshivali profile image
Shivali Pandey

Very informative!

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks 👍

Collapse
 
ayodejii profile image
Isaac Ayodeji Ikusika

thanks for sharing.
As sweet and helpful as this is, I do not remember the last time I had to use git commands, thanks to Visual Studio. 😎

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Welcome!
Glad you liked it :)

Collapse
 
code_rabbi profile image
Emeka Orji

This was useful! Bookmarked!

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks for supporting

Collapse
 
bobbyiliev profile image
Bobby Iliev

Great list! Well done!

For anyone who wants to learn more about Git and GitHub, I could suggest this free eBook here:

GitHub logo bobbyiliev / introduction-to-git-and-github-ebook

Free Introduction to Git and GitHub eBook

💡 Introduction to Git and GitHub

This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.

🚀 Download

To download a copy of the eBook use one of the following links:

📘 Chapters

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks a lot
I checked it , it's useful

Collapse
 
sanzeeb3_18 profile image
Sanzeeb Aryal

How is here no git diff command? One of the most used.

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Added in the post.
Sorry and Thanks

Collapse
 
fyodorio profile image
Fyodor

I thought there are three of them only... 😅

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

😄
Thanks for visiting.

Collapse
 
somtobiotech profile image
somtobiotech

interesting

Collapse
 
vamsitupakula_ profile image
Vamsi Krishna • Edited

Nice 🔥

Collapse
 
samuelrivaldo profile image
Samuelrivaldo

Thanks 🙏

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Welcome 🤗

Collapse
 
lorio profile image
Lori

Thanks. Bookmarked.

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

🤗 Welcome
Glad you liked it :)

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

Thanks a lot
I'll update the list with these commands
👍

Collapse
 
hiholas profile image
Nico Delgado

bisect <3

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

👍

Collapse
 
dovey21 profile image
Somtochukwu Nnaji

Atleast adding more to the ones I already will do no harm. So nice of you to share

Collapse
 
krishnaagarwal profile image
Krishna Agarwal • Edited

Glad you liked it :)

Collapse
 
vairasza profile image
Michael Bierschneider

does „git rm -r - cached config.js“ only delete the remote file in the latest commit or in all previous commits too? or maybe does github create a new commit, leaving the previous commits untouched?

Collapse
 
krishnaagarwal profile image
Krishna Agarwal

It deletes the file you want remotely
It'll not delete any file locally

I hope it is clear now.

Collapse
 
rosacodes profile image
Rosacodes

Great list!

Collapse
 
davidsilva83 profile image
David

Very good, this article
I never used git bisect 🤯