DEV Community

S. Sharma
S. Sharma

Posted on

5 Git Commands Everyone Should Know

Git is an amazing tool for version-control and open-source collaboration (though many companies are now using it too!). I think that most people should be able to use Git, especially if it's a requirement for them at work or school. Below are 5 Git commands everyone should know.

  • git reset --hard [commit hash] — This is fantastic as a last resort for when you've really screwed up. Generally, I've only used this when I've tried out a dumb idea (thinking it was good), merged it into my main branch, and then realized that it was, in fact, a dumb idea in the first place. You might want to read more about this.
  • git checkout -- [filename] — If you're ever in a situation where you've made so many changes to a file that you think it's just better to scrap them all, this is the command. git checkout -- [filename] will undo any uncommitted changes to a file (or a set of files). You might want to read more about this.
  • git rm [filename] — This command gets git to stop tracking a file or a set of files. It's pretty useful for removing a file from the working directory and removing it from git's tracking. You might want to read more about this.
  • git config --global alias.[aliasname] [command] — This is very helpful if you hate typing a lot. git alias allows you to create short-hands for common git commands. For example, git config --global alias.nbr "checkout -b" allows you to do git nbr my-branch to create a new branch called my-branch instead of git checkout -b my-branch(I made this exact alias myself). You might want to read more about this.
  • git revert — OK, so I'll admit, I've never used this once, but I think the command is pretty useful. Given a set of commits, it'll undo those changes and record that revert in a commit. Again, I've never used it, but I can think of many cases where development can go so awry that it's necessary. You might want to read more about this.

Opinions? Thoughts? Praises? What are some useful git commands for you? Leave a comment below! 😀

Top comments (8)

Collapse
 
esmejia277 profile image
Esteban Mejía

Know about git, is very a very important thing . Use gitkraken could be useful too, when you don't know a lot about git.

Collapse
 
jfstandart profile image
Juan Felipe Uribe Cardona

What are you doing here Fred?

Collapse
 
milburngomes profile image
MilburnGomes

Didn't know about 'gitkraken'. Will check more about it. Thanks for sharing!

Collapse
 
esmejia277 profile image
Esteban Mejía

Oh sure, you're welcome.

Collapse
 
couchcamote profile image
couchcamote

git stash

Collapse
 
abloch profile image
Akiva

My favorite git command is not a command but a flag. I love the -- to limit the effect to a single folder.
Also I adore cherry pick, a brilliant concept

Collapse
 
ancientswordrage profile image
AncientSwordRage

Git bisect is also very useful

Collapse
 
sirzarganwar profile image
Martin Jirasek

Hmm, "git revert is pretty useful but I've never used it" 🙄

  • reflog
  • reset --soft HEAD^
  • log (with lot of funny varints of args)
  • push origin :branch
  • ...