8

我正在寻找我希望是简单的一行命令来确定当前签出分支的正确上游引用?

基本上

git branch --remote HEAD

哪个(如果有效)会将符号模式 HEAD 转换为当前分支名称,然后该选项--remote将其更改为远程跟踪分支的引用。(但它不会那样做!)

如果我有morehelp一个配置为的分支

remote = origin
merge = refs/heads/morehelp

简单的命令行将返回refs/remotes/origin/morehelp它的上游跟踪分支(非常适合git reset --hard <ref>通过覆盖更新的情况)

4

1 回答 1

16

我想你想要

git rev-parse --symbolic-full-name @{u}

@{u}是上游跟踪分支的缩写,HEAD该选项告诉rev-parse以您想要的格式打印它,而不是打印 SHA 提交 ID。

git help rev-parse

   --symbolic
       Usually the object names are output in SHA1 form (with possible ^ prefix); this option makes them output in a form as close to the original
       input as possible.

   --symbolic-full-name
       This is similar to --symbolic, but it omits input that are not refs (i.e. branch or tag names; or more explicitly disambiguating
       "heads/master" form, when you want to name the "master" branch when there is an unfortunately named tag "master"), and show them as full
       refnames (e.g. "refs/heads/master").
于 2013-03-07T23:50:24.237 回答