命令确定当前HEAD的上游引用?

时间:2013-03-07 23:44:53

标签: git git-branch

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

基本上类似

git branch --remote HEAD

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

如果我的分支morehelp的配置为

remote = origin
merge = refs/heads/morehelp

简单的命令行将返回refs/remotes/origin/morehelp,这是它的上游跟踪分支(适用于覆盖更新的git reset --hard <ref>情况)

1 个答案:

答案 0 :(得分:15)

我想你想要

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").