git:以编程方式知道分支在远程分支前后的数量 [英] git: programmatically know by how much the branch is ahead/behind a remote branch

查看:93
本文介绍了git:以编程方式知道分支在远程分支前后的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提取在 git status 之后打印的信息,如下所示:

 #在分支master上
#你的分支在2次提交之前在'origin / master'前面。

当然,我可以解析 git status 但这不推荐,因为这个人类可读的输出可能会改变。

有两个问题:


  1. 如何知道远程追踪分支?它通常是 origin / branch 但不一定是。

  2. 如何获取数字?如何知道它是否在前/后?通过多少次提交?那么分歧的分支案例呢?


解决方案

更新



正如amalloy所指出的,最近的git版本支持通过给定branchname @ {upstream}(或branchname @ {u }或@ {u}用于HEAD的跟踪分支)。这有效地取代了下面的脚本。你可以这样做:

git rev-list @ {u} ..
git rev-list --left-right --boundary @ {u} ...
gitk @ {u} ...

等。例如,我有 git q 别名为 git log --pretty ='...'@ {u} .. 显示我排队提交准备推送。

原始答案

似乎没有一种简单的方法可以找到跟踪分支,而不用解析更多的git配置,而不是在几个shell命令中实际使用的。但是在很多情况下,这将会有很长的路要走:

#找出当前分支名称
currentbranch = $(expr $(git symbolic-ref HEAD):'refs / heads / \(。* \)')
[-n$ currentbranch] || 你似乎不在分支上
#在配置中查找这个分支
remote = $(git config分支。$ currentbranch.remote)
remote_ref = $( git config branch。$ currentbranch.merge)
#将远程ref转换为跟踪引用...这是一个破解
remote_branch = $(expr $ remote_ref:'refs / heads / \(。 * \)')
tracking_branch = refs / remotes / $ remote / $ remote_branch
#now $ tracking_branch应该是本地ref参考HEAD
git rev-list $ tracking_branch..HEAD

另一个更蛮力的方法:

  git rev-list HEAD --not --remotes 

jamessan的答案解释了如何使用 git rev-list 找到$ tracking_branch和HEAD之间的相对差异。你可以做的一件有趣的事情:

pre $ g $ rev $ list -left-right $ tracking_branch ... HEAD

(注意$ tracking_branch和HEAD之间的三个<\\ em>点)。这将显示在前面具有区分标记的两个手臂上的提交:<对$ tracking_branch进行提交,对HEAD进行提交时使用>。

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/master' by 2 commits.

Of course I can parse the output of git status but this is not recommended since this human readable output is liable to change.

There are two problems:

  1. How to know the remote tracked branch? It is often origin/branch but need not be.
  2. How to get the numbers? How to know whether it is ahead/behind? By how many commits? And what about the diverged branch case?

解决方案

update

As pointed out by amalloy, recent versions of git support finding the matching tracking branch for a given branch by giving "branchname@{upstream}" (or "branchname@{u}", or "@{u}" for the tracking branch of HEAD). This effectively supercedes the script below. You can do:

git rev-list @{u}..
git rev-list --left-right --boundary @{u}...
gitk @{u}...

etc. For example, I have git q aliased to git log --pretty='...' @{u}.. to show me "queued" commits ready for pushing.

original answer

There doesn't seem to be an easy way to find the tracking branch in general, without parsing lots more git config than is practical in a few shell commands. But for many cases this will go a long way:

# work out the current branch name
currentbranch=$(expr $(git symbolic-ref HEAD) : 'refs/heads/\(.*\)')
[ -n "$currentbranch" ] || die "You don't seem to be on a branch"
# look up this branch in the configuration
remote=$(git config branch.$currentbranch.remote)
remote_ref=$(git config branch.$currentbranch.merge)
# convert the remote ref into the tracking ref... this is a hack
remote_branch=$(expr $remote_ref : 'refs/heads/\(.*\)')
tracking_branch=refs/remotes/$remote/$remote_branch
# now $tracking_branch should be the local ref tracking HEAD
git rev-list $tracking_branch..HEAD

Another, more brute-force, approach:

git rev-list HEAD --not --remotes

jamessan's answer explains how to find the relative differences between $tracking_branch and HEAD using git rev-list. One fun thing you can do:

git rev-list --left-right $tracking_branch...HEAD

(note three dots between $tracking_branch and HEAD). This will show commits on both "arms" with a distinguishing mark at the front: "<" for commits on $tracking_branch, and ">" for commits on HEAD.

这篇关于git:以编程方式知道分支在远程分支前后的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆