2010-02-20 180 views
8

我想從一個新的遠程倉庫跟蹤一個遠程主分支。兩者都已經存在。Git安裝遠程跟蹤分支

我怎麼去這混帳?我似乎無法做到。我想:

git remote add otherRepo git://... 
git branch -t myTrack otherRepo/master 

不過,我得到以下錯誤:

fatal: Not a valid object name: 'otherRepo/master'

+3

在嘗試分支之前,你是否做過'git fetch otherRepo'? 'git remote add'只是配置遠程,它不會自動執行提取。如果你已經提取了它,你確定它有一個名爲master的分支嗎? 'git branch -r'或'git remote show -n otherRepo'(*取出後*)來檢查它擁有哪個分支。 – 2010-02-20 23:03:21

+0

@crhis:謝謝,現在它可以工作。這似乎是合乎邏輯的,我需要獲取,但是這增加了其他分支的所有其他分支。我可以拿取其他Repo/Master嗎?我不想混亂分支-r。 – 2010-02-20 23:16:12

回答

12

所涵蓋的評論:git remote add otherRepo …只配置了遙控器,它不會從獲取任何東西。您需要運行git fetch otherRepo來獲取遠程存儲庫的分支,然後才能根據它們創建本地分支。


(由OP應對進一步的評論)

如果你只想跟蹤單個分支從遠程倉庫,您可以重新配置了遠程的獲取財產(remote.otherRepo.fetch)。

# done as a shell function to avoid repeating the repository and branch names 
configure-single-branch-fetch() { 
    git config remote."$1".fetch +refs/heads/"$2":refs/remotes/"${1}/${2}" 
} 
configure-single-branch-fetch "$remoteName" "$branch" 
# i.e. # configure-single-branch-fetch otherRepo master 

在此之後,git fetch otherRepo只會抓取遠程倉庫的master分支到你的本地庫的otherRepo/master「遠程跟蹤分支」。

清理其他「遠程跟蹤分支」,你可以將它們全部刪除,並重新獲取你想要的一個,或者你也可以選擇全部刪除,除了你想要的一個:

git for-each-ref --shell --format='git branch -dr %(refname:short)' refs/remotes/otherRepo | sh -nv 
# remove the -nv if the commands look OK, then 
git fetch otherRepo 

# OR 

git for-each-ref --shell --format='test %(refname:short) != otherRepo/master && git branch -dr %(refname:short)' refs/remotes/otherRepo | sh -nv 
# remove the -nv if the commands look OK 

如果您決定要跟蹤多個遠程分支,但不是所有的人,你可以有多個讀取配置(與git config --add remote."$remoteName".fetch …或使用git config --edit直接複製和修改版本庫中的配置文件行)。

如果你也想避免從遠程獲取標籤,配置遠程的tagopt財產(remote.otherRepo.tagopt)。

git config remote."$remoteName".tagopt --no-tags 
# i.e. # git config remote.otherRepo.tagopt --no-tags 
+0

感謝這個非常完整的答案。看起來很糟糕,這種簡單的操作變得如此複雜。隨你 – 2010-02-21 07:39:11

4

你可以嘗試

git checkout -b myTrack otherRepo/master 

這將創建一個新的分支myTrack,其跟蹤otherRepo/master分支。

+0

不工作時,給出的錯誤是: 致命的:git的結帳:更新路徑是與開關支路不兼容。 您是否打算簽出無法解析爲「提交」的'otherRepo/master'? ' – 2010-02-20 23:00:31

+0

做了遠程添加和抓取後爲我工作。 – Von 2011-06-27 01:35:37