0

git branch will show ahead and behind

  master    03ea915f82 [behind 16] test

I think it means master is behind 16 of remote/origin/master ?

Is it possible to show ahead and behind between local branches?

For example, I have master and develop, I often update master then rebase master in develop, it would be help if it shows something like this:

  master    03ea915f82 test
  develop   1231231231 [behind master 16] test
Sato
  • 8,192
  • 17
  • 60
  • 115
  • You sure you want it? I've seen instructions on how to accomplish this but know that simple commands such as `git pull` will now also consider that remote branch. In other words, if you are on develop and issue a git pull, you will pull from master. – Lasse V. Karlsen Apr 14 '17 at 08:37
  • `rebase master in develop` ... this makes no sense to me. Typically, you would rebase `develop` on `master`, and then fast forward the latter with the former. – Tim Biegeleisen Apr 14 '17 at 08:42
  • `git merge-base master develop` ... this would at least show you the most recent common ancestor commit. You could then count how many commits are between this and the tips of the `master` and `develop` branches. This would give you an idea of how rough the rebase might be. – Tim Biegeleisen Apr 14 '17 at 08:45

1 Answers1

0

git branch will show ahead and behind

master    03ea915f82 [behind 16] test

I think it means master is behind 16 of remote/origin/master ?

It's not possible to tell from this alone. The count(s) is/are for whatever is set as the upstream for branch master. With -vv, the name of the upstream is included:

$ git branch -vv
* master          e0688e9b2 [origin/master: behind 479] git svn: fix
  stash-exp       8dbdf339c [origin/master: ahead 1, behind 1142] st

Note that both branches here have origin/master as their upstream, but stash-exp is more behind than master is (and stash-exp is also ahead 1).

Is it possible to show ahead and behind between local branches?

Yes—but you get only one upstream per branch. If you set the upstream of local branch A to local branch B, you see only how far ahead or behind A is with respect to B, and not to origin/A.

You can compute your own counts. What both git branch and git status do here is to run (the internal equivalent of) git rev-list --count:

$ git rev-list --count stash-exp..origin/master
1142
$ git rev-list --count origin/master..stash-exp
1

These are the two counts git branch -vv printed. We can get both at once:

$ git rev-list --count --left-right stash-exp...origin/master
1       1142

Note the three dots here, which produce a symmetric difference: commits reachable from either branch tip, but not from both branch tips. The --count code in git rev-list counts the two separately when using --left-right, and prints the left hand side count (1) on the left and the right hand side count (1142) on the right. Swap left and right sides and the counts swap:

$ git rev-list --count --left-right origin/master...stash-exp
1142    1

If I wish to find the relationship between two local branches, such as master and stash-exp, I can use those here:

$ git rev-list --count --left-right master...stash-exp
663     1

which tells me there are 663 commits reachable from the tip of master that are not reachable from the tip of stash-exp, and 1 commit reachable from the tip of stash-exp that is not reachable from the tip of master.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775