我该怎么检查 feature_branch
和 origin/feature_branch
指向同一个提交?
我该怎么检查 feature_branch
和 origin/feature_branch
指向同一个提交?
你可以比较输出 git-rev-parse
,比如在这个shell脚本中:
export BRANCH_A=feature_branch
export BRANCH_B=origin/feature_branch
if [ x"$(git rev-parse $BRANCH_A)" = x"$(git rev-parse $BRANCH_B)" ]
then
echo $BRANCH_A and $BRANCH_B are the same
fi
如果你对在脚本中使用它不感兴趣,只是想要一些指示 feature_branch
和 origin/feature_branch
相对于彼此,然后是输出的顶部 git status
如果它们不相同,会告诉你它们的相对位置,例如:
$ git status
# On branch foo
# Your branch is behind 'origin/foo' by 187 commits, and can be fast-forwarded.
但请注意,这只适用于您的git配置指示的情况 origin/feature_branch
是“上游”的 feature_branch
。如果您已从预先存在的远程跟踪分支创建了本地分支,则通常会出现这种情况。相反,如果 feature_branch
是您在本地创建的新分支,您可以通过推送分支在配置中设置该关联 -u
,例如 git push -u origin feature_branch
。)
你可以比较输出 git-rev-parse
,比如在这个shell脚本中:
export BRANCH_A=feature_branch
export BRANCH_B=origin/feature_branch
if [ x"$(git rev-parse $BRANCH_A)" = x"$(git rev-parse $BRANCH_B)" ]
then
echo $BRANCH_A and $BRANCH_B are the same
fi
如果你对在脚本中使用它不感兴趣,只是想要一些指示 feature_branch
和 origin/feature_branch
相对于彼此,然后是输出的顶部 git status
如果它们不相同,会告诉你它们的相对位置,例如:
$ git status
# On branch foo
# Your branch is behind 'origin/foo' by 187 commits, and can be fast-forwarded.
但请注意,这只适用于您的git配置指示的情况 origin/feature_branch
是“上游”的 feature_branch
。如果您已从预先存在的远程跟踪分支创建了本地分支,则通常会出现这种情况。相反,如果 feature_branch
是您在本地创建的新分支,您可以通过推送分支在配置中设置该关联 -u
,例如 git push -u origin feature_branch
。)
您可以使用 git rev-list
。只需致电 git rev-list feature_branch...origin/feature_branch
(对称差异)。如果没有输出,则两个提交都是相同的。
最明确的方式显然是顺便说一下 git rev-parse
正如所指出的那样,比较结果 马克·朗格尔
git rev-parse origin/foo_branch
不会在远程分支上获取最新的提交哈希值。它将为您提供本地git存储库在远程分支上知道的最新提交哈希。
如果你想比较你的本地 foo_branch
是最新的 当前 遥远的,你可能想要:
# remote commit hash.
# will do a network request to get latest.
git ls-remote --head --exit-code origin foo_branch | cut -f 1
# local commit hash.
git rev-parse foo_branch
或者,如果你想使用 git rev-parse
,您应该在运行之前先获取最新的更改 rev-parse
:
git fetch origin foo_branch
git rev-parse origin/foo_branch
我喜欢 git ls-remote
方法,因为它使您的本地存储库保持不变。
如果你这样做 git log feature_branch
它显示了你的历史。只需检查提交SHA id是否与 git log origin/feature_branch
历史。
您可以使用这样的简单命令:
git diff feature_branch origin/feature_branch
只有当分支指向其他东西时,才会显示某些内容。
git log -1 --decorate feature_branch
如果它给你这样的东西
commit c1e77c1....896b (origin/feature_branch, feature_branch)
比两个分支都指向同一个提交。
commit c1e77c1....896b (feature_branch)
表示分支未同步。
你也可以使用
git merge-base feature_branch origin/feature_branch
这将指出最后的常见提交。接下来
git merge-base feature_branch origin/feature_branch | git show --decorate
正如Mark Longair所说, git status
也可以是一个解决方案(有时候最方便),但这取决于你的工作存储库和速度 git status
(什么可以加快 git gc
曾经使用过一次)
简单的git状态应该显示当前分支在远程前后的提交次数。确保所有遥控器都配置正确。