2013-03-07 39 views
8

我正在尋找我希望的是簡單的一行命令來確定當前檢出分支的正確上游引用?命令確定當前HEAD的上游引用?

本質

git branch --remote HEAD

它(如果它的工作)將象徵性的圖案HEAD轉換爲當前分支的名稱,然後選擇--remote然後變爲遠程的裁判跟蹤分支。 (但它沒有做到這一點!)

如果我有分支morehelp

remote = origin 
merge = refs/heads/morehelp 

一個配置簡單的命令行會返回refs/remotes/origin/morehelp這是它的上游跟蹤分支(理想的git reset --hard <ref>箱子通過重寫更新)

回答

13

我想你想

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

看起來業務。我以爲我早些時候已經過去了,但正在嘗試'git rev-parse --symbolic-full-name HEAD'和一些無法使用的遠程選項。這感覺就像應該更明顯一些。 (即使是@ {u}'也有註釋「缺少的ref默認爲當前分支) – 2013-03-08 00:02:33

+2

_」感覺應該更明顯一些。「但是這是Git,所有真正有用的命令都是非常不明顯;) – 2013-03-08 00:16:20

+0

+100(b ;-) for that。我一直在考慮爲'git reset --hard '添加一個'--to-usptream'來讓我重置爲上游的情況,當我通過我的一個私人開發分支機器之間通過我的Github回購現在我可以使用你的一行創建一個別名 – 2013-03-08 08:02:56