DEV Community

Cover image for 15 Git Commands You May Not Know

15 Git Commands You May Not Know

Zaiste on March 31, 2019

Using Git may be intimidating at times. There are so many commands and details to learn. The documentation, however, while being immense, is still ...
Collapse
 
rhymes profile image
rhymes

Great article, didn't know about some of these, especially 9, 11 and 12.

Adding to 15, another cleanup argument I find very useful is

git remote prune origin

to cleanup deleted branches on origin

Collapse
 
stecman profile image
Stephen Holdaway • Edited

A companion command to 14 that I use all the time is:

git tag --contains <commit>

It's great for figuring out what versions are affected by a bug once you know the commit, or what version you need to use to have a particular feature/change.

Bonus command is:

git branch --merged master

This one is handy to find old branches that can be safely cleaned up.

Another bonus is:

git describe

Which is an easy way to figure out what the next version should be, or to create a human readable identifier for a commit when packaging a build or writing in a deployment log

Collapse
 
zaiste profile image
Zaiste

Great additions!

Collapse
 
jcoelho profile image
José Coelho

Great article! Sometimes we forget how powerful git really is.

Another command I really like and most people don't use is git bisect.
It's a great way to find a buggy commit.

Collapse
 
ludamillion profile image
Luke Inglis

This is one of those commands you don’t use very often but is priceless when you do need it.

Collapse
 
sally profile image
Sally

I learned git cherrypick a few weeks ago and found that really useful - we needed a couple of commits from one branch to be brought into a new branch of fixes.

Collapse
 
gergelypolonkai profile image
Gergely Polonkai

checkout - caught me unaware last time i used my rebase-all script, as it also works with merge and rebase (and actually anywhere you need to pass a commitish to a Git command). This script of mine walks all local branches and rebases them onto the branch i specify on the command line (or origin/master if i don’t). Also, if i pass -n as the first parameter, it doesnʼt do a git fetch before traversing my branches. Now few days ago i invoked it as git rebase-all -.

My following workday wasnʼt funny. Context: i usually have 40ish local branches.

Collapse
 
claudiobernasconi profile image
Claudio Bernasconi

Very glad you wrote these important tips down. I was not aware of all of them but number 6 and 7 are shortcuts I often use.

Be aware that if you use too much shorthand git command you get the reputation as git wizard which leads to the hard to deal with problems. :-D

Very well written, thank you Zaiste.

Collapse
 
zaiste profile image
Zaiste

Thanks Claudio for kind words. I'm happy you liked this article!

Collapse
 
diogotome profile image
Diogo Tomé

In my workflow it is routine to delete branches on Github Pull Requests when they're merged. This leaves the local repo with local branches that have since lost its tracking branch. To clear this I have the following alias under .gitconfig

prune-branches = !git remote prune origin && git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D

So whenever a bunch of my PR's have been merged I just run git prune-branches and it prunes the remote branch references but also deletes the corresponding local ones.

Collapse
 
tomakesense profile image
Kevin Xiong

My small contribution

1.Count the number of commits on a branch
git rev-list --count master

2.Count number of branches in a git repository
git branch | wc -l

3.Delete remote branch cache in your local repository
git fetch -p

4.Delete remote tag cache in your local repository
git fetch -p -P

5.Delete a git alias
git config --global --unset alias.XXX

6.List git aliases
git config --list | grep alias

Collapse
 
zerquix18 profile image
I'm Luis! \^-^/

Here's my small contribution: If you added a commit and already pushed it or merged it into another branch and you need to reverse those changes, you can always go:


git reverse {hash}

It will create a new commit with the changes reversed. Also, the new commit will have a reference to the reversed commit (i.e Github will tell you "this reverses {hash}")

Collapse
 
rajeshduggal profile image
Rajesh Duggal

Which version of git has "git reverse"? I can't find it mentioned in git-scm.com/docs

Collapse
 
mfilej profile image
Miha Filej

Probably meant git revert

Collapse
 
pshchelo profile image
Pavlo Shchelokovskyy

as alternative to 11. you can also use

git mergetool [-t <tool>]

when rebase fails with merge conflicts

The default merge tool to use is configurable in .gitconfig, mine is vimdiff :-)
For vimdiff it opens 4-pane view with two conflicting versions (REMOTE and LOCAL), their common ancestor (BASE) and the actual current file with merge conflicts. After resolving the conflict and exiting it will open the next file with conflicts, one by one.

Out of the box Git knows how to work with more diff/merge tools - emerge, gvimdiff, kdiff3, meld, vimdiff, and tortoisemerge, but you can configure other ones explicitly yourself.

And there's also a similar but simpler git difftool which is just a visual frontend to git diff

git-scm.com/docs/git-mergetool
git-scm.com/docs/git-difftool

Collapse
 
guyzmo profile image
zmo

you're telling about Magit, there's also the Vimagit plugin for the one true editor!

I cannot live without it. Before using that, I was already using GitXR on OSX, before it became discontinued. I also tried git-cola on linux.

Collapse
 
leob profile image
leob

Fantastic article ... some of my favorite commands that I use a lot:

detailed overview of local/tracking branches:

git branch -avv

git fetch with "cleanup":

git fetch origin --prune

"git stash pop force":

git stash show -p | git apply -3 && git stash drop

git clone using your ssh key via the "git" protocol (particularly useful for private repos):

git clone git@github.com:somecompany/someprivaterepo

and this dev.to article is my go-to reference when I'm confused about 'rebase':

dev.to/maxwell_dev/the-git-rebase-...

Collapse
 
weakish profile image
Jang Rush

Nice! Can I translate this post to Chinese? The translation will be published at nextfe.com (sponsored by LeanCloud, a BaaS provider) and related Chinese social network accounts. The translated text will backlink to this original post.

Collapse
 
dwd profile image
Dave Cridland

Oh, I didn't know about the --patch to stash and checkout, that's nice. I usually use git add -i when I want to fiddle, since you can then see what's going on a little easier.

Collapse
 
giovanamorais profile image
Giovana Morais

I didn't know about a lot of these commands! Thanks a lot. It's always good to improve our git abilities. (:

Collapse
 
bgadrian profile image
Adrian B.G.

Not to be weird, but you should read the official docs on all technologies you use, you'll be surprise how much time you will save on the long run. Git, the IDEs, programming languages and especially libs/frameworks have good documented functionalities, eg: git-scm.com/docs

Collapse
 
zaiste profile image
Zaiste

Absolutely! It's also good to take baby steps, especially where there is a lot to learn.

Collapse
 
mte90 profile image
Daniele Scasciafratte

My collection of git alias github.com/Mte90/dotfiles/blob/mas...

I have also a command that download my fork and add automatically the upstream using the GitHub API

I use also hub.github.com/

Collapse
 
neyanderson profile image
neyanderson

Thank you Zaiste, you save my day. I've learned a lot with your all post.

Collapse
 
zaiste profile image
Zaiste

I'm happy you found it useful!

Collapse
 
koffisani profile image
Koffi Sani

Thanks for this interesting article.

Collapse
 
zaiste profile image
Zaiste

I'm glad you liked it!

Collapse
 
tpompon profile image
Thomas Pompon

Nothing about git cherry-pick ? Still a nice post, could be useful !

Collapse
 
bhupesh profile image
Bhupesh Varshney 👾

Nice 👌 post

Collapse
 
tekguy profile image
Gordon

Excellent article, thank you

Collapse
 
zaiste profile image
Zaiste

Thanks for the kind words!

Collapse
 
pjopaul profile image
pjopaul

Very informative for new Git users like me, thx

Collapse
 
zaiste profile image
Zaiste

Thank you! Take your time learning Git. And don't get discouraged!

Collapse
 
zerdaris profile image
Zerdaris

Oh nice! Didn't know about git co -, and renaming branch! Thanks!

Collapse
 
miteshkamat27 profile image
Mitesh Kamat

That's a good cheatsheet

Collapse
 
scratchcodeio profile image
Scratch Code

Great Article! I recommended this tutorial for beginners: scratchcode.io/basic-git-commands-...

Collapse
 
mjcoder profile image
Mohammad Javed

Awesome article, for someone like me that keeps on forgetting the commands, this will come in handy! I'm always finding myself in some sort of trouble when using Git! I just don't know why! Ha!

Collapse
 
sai2572910 profile image
Sai

You think Code processing spreed of Git of Magit faster?