我可以运行这些命令并获得预期的输出。
$ 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.发布于 2014-12-31 06:08:14
不存在这样的东西,而且跟踪只存储在另一个方向:"X跟踪Y",而不是"Y被.跟踪“,后者更复杂,称为”.“。部件可以扩展到多个项。(而且,我认为Y作为一个远程跟踪分支是非常典型的,在这种情况下,你永远不可能在Y上-尽管一个试图查找“哪个跟踪Y”的命令肯定会使用参数,所以您可以说“告诉我任何跟踪origin/master的分支)。”
也就是说,建造这样的东西当然是可能的。在Python-esque伪代码中,算法如下所示:
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中是这样的:
# 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 --get、branch.branch.remote和analyze的细节,而analyze的细节则更复杂:如果remote是.,那么tracked中的任何内容都是简单的,但是如果是实际的远程,则必须通过相应的fetch行传递fetch中的任何内容,以找到合适的远程跟踪分支。
https://stackoverflow.com/questions/27716781
复制相似问题