299

I have created a branch for testing in my local repo (test-branch) which I pushed to Github.

If I go to my Github account and select this test-branch it shows the info:

This branch is 1 commit ahead and 2 commits behind master

My questions are:

  1. How can I display this info locally (ie: a command that shows this on the terminal, rather than having to open Github to see it)?
  2. I know I can see the diffs between branches using:

    git diff master..test-branch
    

    or using Meld (which I prefer):

    git difftool master..test-branch
    

    but I was wondering if there's a way to see the ahead and behind commits separately. I.E.: is there a way to show that 1 commit ahead by itself and then those 2 commits behind by themselves?

Gabriel
  • 40,504
  • 73
  • 230
  • 404
  • http://stackoverflow.com/questions/19163803/alias-that-displays-how-many-commits-branches-are-ahead-behind-master – usha Dec 06 '13 at 21:31
  • 2
    Git 2.5+ (Q2 2015) will introduce `git for-each-ref --format="%(push:track)" refs/heads`. See [my answer below](http://stackoverflow.com/a/30720338/6309) – VonC Jun 08 '15 at 22:48
  • 2
    Because I couldn't find this question using the keywords I'm about to write, I just want to note that this amounts to taking the [*relative complement* (or "set difference")](https://en.wikipedia.org/wiki/Complement_(set_theory)#Relative_complement) between the sets of commits that make up those branches (and then counting the elements). Hopefully this makes it into search engine indices. – smheidrich Dec 19 '18 at 00:30
  • Very helpful! For help in finding this via search engines, I'll add an example where both numbers are plural: `This branch is 164 commits ahead, 85 commits behind master.` – nealmcb Jul 28 '20 at 21:37

6 Answers6

446

Part 1

As an answer on your question 1, here's a trick I found to compare two branches and show how many commits each branch is ahead of the other (a more general answer on your question 1):

For local branches: git rev-list --left-right --count master...test-branch

For remote branches: git rev-list --left-right --count origin/master...origin/test-branch

This gives output like the following:

2 1

This output means: "Compared to master, test-branch is 1 commit ahead and 2 commits behind."

You can also compare local branches with remote branches, e.g. origin/master...master to find out how many commits a local branch (here master) is ahead/behind its remote counterpart.

Part 2

To answer the second part of your question, the solution depends on what exactly you want to achieve.

To view commits

In order to have git rev-list return the exact list of commits unique on either side, replace the --count argument with something like --pretty=oneline, making the complete command to execute:

git rev-list --left-right --pretty=oneline master...test-branch

This will generate output like this:

<bba27b56ad7072e281d529d4845e4edf877eb7d7 unique commit 2 on master
<dad0b69ec50ea57b076bfecabf2cc7c8a652bb6f unique commit 1 on master
>4bfad52fbcf0e60d78d06661d5c06b59c98ac8fd unique commit 1 on test-branch

Here every commit sha is preceded by < or > to indicate which branch it can be found on (left or right, here master or test-branch respectively).

To view code

If you want to view a diff of all new commits only found on either branch, you'll need to do this in two steps:

  1. define the most recent common ancestor
$ git merge-base master test-branch
c22faff7468d6d5caef217ac6b82f3ed95e9d902
  1. diff either branch to the commit sha obtained above (short format will usually do)

To show the diff of all commits only found on master

git diff c22faff7..master

To show the diff of all commits only found test-branch

git diff c22faff7..test-branch
user1834095
  • 5,332
  • 2
  • 20
  • 38
  • 2
    This is a great solution for me to find out if i can delete a branch or if it is still ahead of develop. – Maverick1st Jul 30 '15 at 08:59
  • 1
    @Maverick1st, yes, this command is particularly useful when you are following the [gitflow workflow](http://nvie.com/posts/a-successful-git-branching-model/) (which I am) – user1834095 Aug 05 '15 at 06:34
  • 3
    This is the best for my use case of finding if your current feature branch is behind remote master (`git rev-list --left-right --count origin/master...@`) - provided the user does `git fetch` before; useful to prevent pull requests from outdated branches. – jakub.g Mar 02 '16 at 19:53
  • 12
    To check just how many commits *behind* is the current branch: `git rev-list --left-right --count origin/master...@ | cut -f1` – jakub.g Mar 04 '16 at 09:31
  • 14
    @jakub.g you don't need to cut if you use `--left-only` or `--right-only` – jasonkarns Sep 12 '18 at 18:38
  • Am trying to add this info to cmd prompt using something like # add ahead/behind info between remote and local function git_is_branch_pushed { REMOTE_TO_LOCAL=`git rev-list --left-right --count origin/$1...$1 2> /dev/null` LOCAL_TO_REMOTE=`git rev-list --left-right --count origin/$1...$1 2> /dev/null` if [[ ${REMOTE_TO_LOCAL} != ${LOCAL_TO_REMOTE} ]]; then echo -ne "R: ${REMOTE_TO_LOCAL} L:${LOCAL_TO_REMOTE}" fi } BRANCH=`git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/"` git_is_branch_pushed ${BRANCH} and its not functioning as expected. – Elamurugan Nov 22 '19 at 13:07
  • This solution also works for the current commit, not just for branches. Example: check out the commit 26 behind master: `git checkout master~26` then run `git rev-list --left-right --count master...@` and you'll see `26 0`. – tanius Jun 19 '20 at 16:54
63

First of all to see how many revisions you are behind locally, you should do a git fetch to make sure you have the latest info from your remote.

The default output of git status tells you how many revisions you are ahead or behind, but usually I find this too verbose:

$ git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 2 and 1 different commit each, respectively.
#
nothing to commit (working directory clean)

I prefer git status -sb:

$ git status -sb
## master...origin/master [ahead 2, behind 1]

In fact I alias this to simply git s, and this is the main command I use for checking status.

To see the diff in the "ahead revisions" of master, I can exclude the "behind revisions" from origin/master:

git diff master..origin/master^

To see the diff in the "behind revisions" of origin/master, I can exclude the "ahead revisions" from master:

git diff origin/master..master^^

If there are 5 revisions ahead or behind it might be easier to write like this:

git diff master..origin/master~5
git diff origin/master..master~5

UPDATE

To see the ahead/behind revisions, the branch must be configured to track another branch. For me this is the default behavior when I clone a remote repository, and after I push a branch with git push -u remotename branchname. My version is 1.8.4.3, but it's been working like this as long as I remember.

As of version 1.8, you can set the tracking branch like this:

git branch --track test-branch

As of version 1.7, the syntax was different:

git branch --set-upstream test-branch
janos
  • 120,954
  • 29
  • 226
  • 236
  • What version of `git` are you using? I can't reproduce what you get with either `git status` nor with `git status -sb`. If I try either command (after doing `git fetch`) I do not get any info on commits ahead/behind. – Gabriel Dec 07 '13 at 14:08
  • 9
    Your way to see commits ahead and behind is applied to `master` and `origin/master` and I want to see those diffs for `master` and a another branch `test-branch`. Could you reformat your answer address this issue? – Gabriel Dec 07 '13 at 14:33
  • The diff in "behind" and "ahead" revisions only works for me if I use 3 dots between the branch names (not 2). And the ^ and ^^ didn't seem to matter here E.g.: git diff master...origin/master git diff origin/master...master Anyways, "git status -sb" was very helpful. – Victor Basso Jul 24 '14 at 16:18
15

With Git 2.5+, you now have another option to see ahead/behind for all branches which are configured to push to a branch.

git for-each-ref --format="%(push:track)" refs/heads

See more at "Viewing Unpushed Git Commits", which illustrates, for instance, how you may want to know which commits you haven't pushed yet:

git log @{push}..
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Could you do this for just the current branch and its remote tracking branch? – spex Sep 08 '17 at 15:27
  • 1
    @spex yes: complete `refs/heads` with the name of the current branch (https://stackoverflow.com/a/12142066/6309): `refs/heads/$(git rev-parse --abbrev-ref HEAD)` – VonC Sep 08 '17 at 15:29
  • 1
    @RickyLevi Good point, I have edited the answer to make that option more visible. – VonC Apr 16 '23 at 18:27
6

To get the number of commits ahead of master for your current feature branch you can use the following command:

git rev-list --left-right --count master...$(git branch --show-current)
KyleMit
  • 30,350
  • 66
  • 462
  • 664
LI0131
  • 61
  • 1
  • 4
4

You can also use awk to make it a little bit prettier:

git rev-list --left-right --count  origin/develop...feature-branch | awk '{print "Behind "$1" - Ahead "$2""}'

You can even make an alias that always fetches origin first and then compares the branches

commit-diff = !"git fetch &> /dev/null && git rev-list --left-right --count"
Antonis
  • 647
  • 6
  • 15
1

After doing a git fetch, you can run git status to show how many commits the local branch is ahead or behind of the remote version of the branch.

This won't show you how many commits it is ahead or behind of a different branch though. Your options are the full diff, looking at github, or using a solution like Vimhsa linked above: Git status over all repo's

Community
  • 1
  • 1
Wade Williams
  • 3,943
  • 1
  • 26
  • 35