2013-03-05 60 views
4

我有一個本地存儲庫,我通過git clone --mirror <ssh-url>克隆。然後我使用git remote update --prune保持最新。如何更新鏡像克隆中的HEAD分支?

在這一點上,HEADrefs/heads/master

然後我去我的github倉庫的管理部分,並更改默認分支。我所有的分支被更新爲正常,但頭還是refs/heads/master(是的,在分行有不同的散列)

我目前想到的是用git ls-remote得到頭的哈希值與各分公司,然後用一些的grep/awk的魔術,提取HEAD的散列,然後選擇第一個分支與匹配的散列,並使用git symbolic-ref HEAD <found branch name>在本地進行設置。

但是,有沒有一種更簡單的方法來獲取遠程HEAD分支名稱(以可以在腳本中更新的方式)?

+0

如果它很重要/有幫助,我使用的是從自制軟件安裝在Mountain Lion上的git 1.8.1.4版本。 – Charlie 2013-03-07 14:51:42

回答

2

是的,有這個命令:

git remote set-head origin -a 

git help remote

With -a, the remote is queried to determine its HEAD, then the symbolic-ref refs/remotes//HEAD is set to the same branch. e.g., if the remote HEAD is pointed at next, "git remote set-head origin -a" will set the symbolic-ref refs/remotes/origin/HEAD to refs/remotes/origin/next. This will only work if refs/remotes/origin/next already exists; if not it must be fetched first.

+1

問題是我們正在使用鏡像克隆,因此沒有遠程跟蹤分支(本地分支和標籤是爲每個遠程分支和標籤生成的),並且此命令出錯的錯誤是:錯誤:不是有效的ref:refs/remotes/origin/new-master'另外有趣的是,它似乎使用SHA來確定遠程HEAD,因爲如果遠程有多個分支指向HEAD修訂版本,我們會改爲'error:多個遠程HEAD分支。請用以下方式明確選擇一個:<分支機構列表>' – Charlie 2013-03-07 14:49:56

+0

您可以爲此添加一個遙控器。如果您使用需要此協議的協議,Git將僅猜測遠程HEAD。不幸的是我不知道這是哪個協議。 – Chronial 2013-03-07 15:25:44

-1

由於您使用SSH,你可以,如果你有正確的ssh訪問使用這個命令:

scp [email protected]:/path/to/the/server/repo/HEAD local/repo/.git/HEAD 
+0

不起作用。在這種情況下,遠程是Github Enterprise實例。但即使是針對公共github,您也會遇到:'無效的命令:'scp -f -org/repoName.git/HEAD'和一個關於使用ssh克隆'git://'url的消息。 – Charlie 2013-03-07 19:34:07

+0

我說「如果你有正確的ssh訪問」 - 你沒有在github上正確的ssh訪問。 – Chronial 2013-03-07 20:31:55

+0

爲什麼downvote? – Chronial 2013-10-18 21:43:03

0

要查詢遠程HEAD,請使用

git remote show origin 
* remote origin 
    Fetch URL: git://... 
    Push URL: git://... 
    HEAD branch: whateverremotehead 
    Remote branches: 
    ... 

,並解析輸出,尋找「蓋分支」,並使用在

git symbolic-ref HEAD refs/heads/whateverremotehead 

我不知道任何更好的方式。