git: programmatically know by how much the branch is ahead/behind a remote branch

后端 未结 10 513
南方客
南方客 2021-01-30 10:29

I would like to extract the information that is printed after a git status, which looks like:

# On branch master
# Your branch is ahead of \'origin/         


        
相关标签:
10条回答
  • 2021-01-30 11:15

    Edit: My original answer was actually not very good because it relied on the user to have a remote called "origin". It also failed if the current branch was had a tracking branch besides origin-head. These flaws essentially made it useless. However, the answer by @araqnid is not the most efficient method and the way he arrives at $tracking_branch is less than strait forward. The most efficient (fastest) method I have found to get the same functionality is the following:

    # get the tracking-branch name
    tracking_branch=$(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD))
    # creates global variables $1 and $2 based on left vs. right tracking
    # inspired by @adam_spiers
    set -- $(git rev-list --left-right --count $tracking_branch...HEAD)
    behind=$1
    ahead=$2
    

    original answer: (inferior, but given for clarity)

    Perhaps the simplest method I could find (inspired by @insidepower)

    # count the number of logs
    behind=$(git log --oneline HEAD..origin | wc -l)
    ahead=$( git log --oneline origin..HEAD | wc -l)
    

    I had previously been using the method of @araqnid, but now I think I'll move some of my scripts to this method since it is much simpler. This should work on any unix system.

    0 讨论(0)
  • 2021-01-30 11:26

    Why wouldn't this work:

    #!/bin/sh
    git diff origin/master..HEAD --quiet --exit-code
    RETVAL=$?
    if [ $RETVAL -gt 0 ]; then
        echo "You need to git push!"
    else
        echo "No git push necessary!"
    fi 
    
    0 讨论(0)
  • 2021-01-30 11:27

    You can try git branch -v -v. With -v flag given twice, it outputs names of upstream branches. Sample output:

    * devel  7a5ff2c [origin/devel: ahead 1] smaller file status overlay icons
      master 37ca389 [origin/master] initial project check-in.
    

    I think this format is more stable than git status output.

    0 讨论(0)
  • 2021-01-30 11:28

    The top chunk of code in araqnid's answer doesn't work for me, so maybe something in git has changed since it was written 18 months ago. It works if I change:

    tracking_branch=refs/remotes/$remote/$remote_branch
    

    to

    tracking_branch=$remote/$remote_branch
    

    However there is still an issue when tracking a local branch, in which case you have to trim the remote part (which becomes '.'):

    tracking_branch=${tracking_branch#./}
    

    Then you can programmatically obtain the number of revisions behind and ahead as follows:

    set -- `git rev-list --left-right --count $tracking_branch...HEAD`
    behind="$1"
    ahead="$2"
    

    I've written scripts to do all that (and more - e.g. they can also attempt to spot remotes on the other side of a git-svn bridge), and published them in my git-config repository on github. For example, here's my git-compare-upstream. See the README for installation instructions and other handy related scripts.

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