git:以编程方式了解分支领先/落后远程分支多少
我想提取 git status
之后打印的信息,如下所示:
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.
当然我可以解析 git status
的输出,但不建议这样做,因为这人类可读的输出很容易改变。
这里有两个问题:
- 如何知道远程跟踪的分支?它通常是
起源/分支
,但不一定是。 - 如何获取数字?如何知道是否领先/落后?通过多少次提交?那么分歧分支的情况又如何呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
git rev-list origin..HEAD 将显示当前分支中的提交,但不显示 origin 的提交——即,您是否领先于 origin 以及哪些提交。
git rev-list HEAD..origin
将显示相反的情况。如果两个命令都显示提交,则说明您有分歧的分支。
git rev-list origin..HEAD
will show the commits that are in your current branch, but not origin -- i.e., whether you're ahead of origin and by which commits.git rev-list HEAD..origin
will show the opposite.If both commands show commits, then you have diverged branches.
更新
正如 amalloy 所指出的,最新版本的 git 支持通过给出“branchname@{upstream}”(或“branchname@{u}”或“@”来查找给定分支的匹配跟踪分支) {u}”用于 HEAD 的跟踪分支)。这有效地取代了下面的脚本。你可以这样做:
等等。例如,我将
git q
别名为git log --pretty='...' @{u}..
来向我展示“已排队”提交准备好推送。原始答案
一般情况下,似乎没有一种简单的方法可以找到跟踪分支,而无需解析比一些 shell 命令中实用的更多的 git 配置。但对于许多情况,这将大有帮助:
另一种更暴力的方法:
jamessan 的答案解释了如何使用 git rev-list 找到 $tracking_branch 和 HEAD 之间的相对差异。您可以做一件有趣的事情:(
注意 $tracking_branch 和 HEAD 之间的三个点)。这将显示两个“手臂”上的提交,并在前面有一个区分标记:“<”对于 $tracking_branch 上的提交,以及“>”用于 HEAD 上的提交。
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:
etc. For example, I have
git q
aliased togit 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:
Another, more brute-force, approach:
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:(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.
您可以尝试
gitbranch -v -v
。使用-v
标志给出两次,它会输出上游分支的名称。示例输出:我认为这种格式比 git status 输出更稳定。
You can try
git branch -v -v
. With-v
flag given twice, it outputs names of upstream branches. Sample output:I think this format is more stable than
git status
output.在现代版本的 git 中,
@{u}
指向当前分支的上游(如果已设置)。因此,要计算您在远程跟踪分支后面的提交次数:
并查看您领先远程分支的距离,只需切换顺序:
为了获得更易读的摘要,您可以要求提供日志:
对于我自己的出于目的,我正在编写一个脚本,如果尚未设置上游,该脚本将用适当的猜测替换
@{u}
。不幸的是,目前没有@{d}
来代表下游(您将推送到的位置)。In modern versions of git,
@{u}
points to the upstream of the current branch, if one is set.So to count how many commits you are behind the remote tracking branch:
And to see how far you are ahead of the remote, just switch the order:
For a more human-readable summary, you could ask for a log instead:
For my own purposes, I am working on a script that will replace
@{u}
with an appropriate guess, if no upstream is yet set. Unfortunately there is at this time no@{d}
to represent the downstream (where you would push to).编辑:
我原来的答案实际上不是很好,因为它依赖于用户拥有一个名为“origin”的遥控器。如果当前分支除了 origin-head 之外还有跟踪分支,它也会失败。这些缺陷基本上使其毫无用处。然而,@araqnid 的答案并不是最有效的方法,而且他到达
$tracking_branch
的方式也不是直线前进。我发现获得相同功能的最有效(最快)的方法如下:原始答案:(较差,但为了清楚起见而给出)
也许是我能找到的最简单的方法(受到@insidepower的启发)
我以前一直在使用该方法@araqnid,但现在我想我会将一些脚本移至此方法,因为它要简单得多。这应该适用于任何 UNIX 系统。
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:original answer: (inferior, but given for clarity)
Perhaps the simplest method I could find (inspired by @insidepower)
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.
git status
有一个--porcelain
选项,用于通过脚本进行解析。它基于--short
输出 - 它们在撰写本文时几乎相同(请参阅 git status 手册页 了解详细信息)。主要区别在于--short
具有颜色输出。默认情况下,不显示分支信息,但如果添加
--branch
选项,您将得到如下输出:如果您是最新的(在获取之后),则分支行将是:
如果你领先:
如果你落后:
对于两者:
请注意,
git status --porcelain --branch
仅在 1.7.10.3 或更高版本(尽管git status --short --branch
已可用自 1.7.2 开始。git status
has a--porcelain
option that is intended for parsing by scripts. It is based on the--short
output - they are almost identical at the time of writing (see the "Porcelain Format" section of the git status man page for details). The main difference is that--short
has colour output.By default no branch information is shown, but if you add the
--branch
option you will get output like:If you are up to date (after a fetch), the branch line will just be:
If you are ahead:
If you are behind:
And for both:
Note that
git status --porcelain --branch
is only available in 1.7.10.3 or later (thoughgit status --short --branch
has been available since 1.7.2 ).为什么这不起作用:
Why wouldn't this work:
araqnid 的答案中最上面的代码块对我来说不起作用,所以也许 git 中的某些内容自 18 个月前编写以来已经发生了变化。如果我更改为:
但是
跟踪本地分支时仍然存在问题,在这种情况下,您必须修剪远程部分(变成“。”):
然后您可以以编程方式获取前后的修订数量,如下所示如下:
我已经编写了脚本来完成所有这些操作(以及更多 - 例如,他们还可以尝试在 git-svn 桥的另一侧发现遥控器),并将它们发布在 git-svn">)。 com/aspiers/git-config#readme" rel="nofollow">我在 github 上的 git-config 存储库。例如,这是我的 git-compare-upstream。有关安装说明和其他方便的相关脚本,请参阅自述文件。
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:
to
However there is still an issue when tracking a local branch, in which case you have to trim the remote part (which becomes '.'):
Then you can programmatically obtain the number of revisions behind and ahead as follows:
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.
Git 2.5+ 引入了一个新的快捷方式,它引用您要推送到的分支。
@{push}
:这将是这里感兴趣的远程跟踪分支。这意味着您有另一个选项可以查看配置为推送到分支的所有分支的前方/后方。
请参阅“查看未推送的 Git 提交”了解更多信息
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.
See more at "Viewing Unpushed Git Commits"
对于最新版本的 git,您应该使用
并检查前方/后方:
With recent versions of git you should use
and check for ahead/behind: