我怎么能检查是否feature_branch并origin/feature_branch指向相同的提交?
Mar*_*air 13
您可以比较输出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
Run Code Online (Sandbox Code Playgroud)
如果你对在脚本中使用它不感兴趣,但只是想要指出彼此在哪里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.
Run Code Online (Sandbox Code Playgroud)
但请注意,这仅在您的git配置指示origin/feature_branch"上游" 时才有效feature_branch.如果您已从预先存在的远程跟踪分支创建了本地分支,则通常会出现这种情况.相反,如果feature_branch是您在本地创建的新分支,则可以通过推送分支在您的配置中设置该关联-u,例如git push -u origin feature_branch.)
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
Run Code Online (Sandbox Code Playgroud)
或者,如果您想使用git rev-parse,您应该在运行之前先获取最新的更改rev-parse:
git fetch origin foo_branch
git rev-parse origin/foo_branch
Run Code Online (Sandbox Code Playgroud)
我喜欢这种git ls-remote方法,因为它不会影响您的本地存储库。
| 归档时间: |
|
| 查看次数: |
4855 次 |
| 最近记录: |