git branch ahead and behind for local branch?

前端 未结 1 873
余生分开走
余生分开走 2021-01-27 05:48

git branch will show ahead and behind

  master    03ea915f82 [behind 16] test

I think it means master is behind 16 of

相关标签:
1条回答
  • 2021-01-27 06:16

    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.

    0 讨论(0)
提交回复
热议问题