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

后端 未结 10 608
南方客
南方客 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条回答
  •  Happy的楠姐
    2021-01-30 11:03

    With recent versions of git you should use

    git status --porcelain --branch
    

    and check for ahead/behind:

    ## master...origin/master [behind 1]
    

    --porcelain[=version]

    Give the output in an easy-to-parse format for scripts. This is similar to the short output, but will remain stable across Git versions and regardless of user configuration. See below for details.

    -b, --branch

    Show the branch and tracking info even in short-format.

提交回复
热议问题