2010-02-20 148 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