Git设置远程跟踪分支

时间:2010-02-20 22:19:23

标签: git

我想从新的远程存储库跟踪远程主分支。两者都已存在。

我如何用git来解决这个问题?我似乎无法做对。我试过了:

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

但是,我收到以下错误:

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

2 个答案:

答案 0 :(得分:12)

如评论中所述:git remote add otherRepo …仅配置遥控器,它不会从中获取任何内容。您需要运行git fetch otherRepo来获取远程存储库的分支,然后才能基于它们创建本地分支。


(回应OP的进一步评论)

如果您只想跟踪远程存储库中的单个分支,则可以重新配置远程的fetch属性(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

答案 1 :(得分:4)

你可以尝试

git checkout -b myTrack otherRepo/master 

这将创建一个新的分支myTrack,它跟踪otherRepo / master分支。

相关问题