首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Git上下游状态

Git上下游状态
EN

Stack Overflow用户
提问于 2014-12-31 05:52:55
回答 1查看 1.6K关注 0票数 3

我可以运行这些命令并获得预期的输出。

代码语言:javascript
复制
$ git branch --track test
$ git checkout test
$ touch hello.txt
$ git add hello.txt
$ git commit -m hello.txt
$ git status
On branch test
Your branch is ahead of 'master' by 1 commit.

不过,我的问题是

代码语言:javascript
复制
$ git checkout master
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

我想知道我是否领先于origin/master,但我也想知道我是否落后于test。我在找一个命令来给我这样的东西:

代码语言:javascript
复制
On branch master
Your branch is up-to-date with 'origin/master'.
Your branch is behind 'test' by 1 commit, and can be fast-forwarded.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-31 06:08:14

不存在这样的东西,而且跟踪只存储在另一个方向:"X跟踪Y",而不是"Y被.跟踪“,后者更复杂,称为”.“。部件可以扩展到多个项。(而且,我认为Y作为一个远程跟踪分支是非常典型的,在这种情况下,你永远不可能在Y上-尽管一个试图查找“哪个跟踪Y”的命令肯定会使用参数,所以您可以说“告诉我任何跟踪origin/master的分支)。”

也就是说,建造这样的东西当然是可能的。在Python-esque伪代码中,算法如下所示:

代码语言:javascript
复制
table = {}
for branch in local_branches:
    try:
        remote, tracked = get_what_branch_tracks(branch)
    except ValueError:
        continue # local branch "branch" not tracking anything
    try:
        br2 = analyze(branch, remote, tracked)
    except ValueError:
        warn('upstream for %s is gone' % branch)
        continue
    # at this point br2 is, e.g., origin/master or a local branch that "branch" tracks
    table.setdefault(br2, []).append(branch)

# now table[] is a table of branches that are tracked, with
# each table[key] being the branches that track branch "key"

因此,对于table中的任何有趣的分支(Es),只需计算在各个分支对中可找到的修订,就像git status所做的那样,在shell中是这样的:

代码语言:javascript
复制
# to compute "git status" for branch X that tracks Y:
nahead=$(git rev-list --count Y..X) # we're ahead $nahead commits
nbehind=$(git rev-list --count X..Y) # and behind $nbehind

如果你落后但没有领先,你可以快速前进。

get_what_branch_tracks的详细信息只是做一些git config --getbranch.branch.remoteanalyze的细节,而analyze的细节则更复杂:如果remote.,那么tracked中的任何内容都是简单的,但是如果是实际的远程,则必须通过相应的fetch行传递fetch中的任何内容,以找到合适的远程跟踪分支。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27716781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档