3

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

$ 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.

但是,这是我的问题所在

$ git checkout master
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

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

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.
4

1 回答 1

3

没有内置这样的东西,并且跟踪仅存储在另一个方向:“X跟踪Y”,而不是“Y被...跟踪”,这更复杂,因为“...”部分可以扩展为不止一项。(此外,我认为 Y 首先是远程跟踪分支更为典型,在这种情况下,您永远不可能在 Y 上——尽管尝试查找“Y 跟踪什么”的命令肯定会占用论点,所以你可以说“告诉我我跟踪的任何分支origin/master)。

也就是说,当然可以建造这样的东西。该算法看起来像这样,在 Python 式的伪代码中:

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,您只需计算在各种分支对中可找到的修订,方法相同git status,在 shell 中只是:

# 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

如果你落后但不领先,你可以快进。

for 的细节get_what_branch_tracks只是简单地做一些git config --gets, of和 of ,而 for 的细节更复杂:if is then what is in很简单,但如果它是一个实际的遥控器,无论是 in必须通过相应的行才能找到适当的远程跟踪分支。branch.branch.remotebranch.branch.mergeanalyzeremote.trackedtrackedfetch

于 2014-12-31T06:08:14.843 回答