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

后端 未结 10 697
南方客
南方客 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:11

    How to know the remote tracked branch? It is often origin/branch but need not be.

    Git 2.5+ introduces a new shortcut which references the branch you are pushing to. @{push}: that would be the remote tracking branch which is of interest here.

    That means you 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"

提交回复
热议问题