简体   繁体   English

Git设置远程跟踪分支

[英]Git setup remote tracking branch

I want to track a remote master branch from a new remote repository. 我想从新的远程存储库跟踪远程主分支。 Both do already exist. 两者都已存在。

How do I go about this in git? 我怎么用git来解决这个问题? I can't seem to get it right. 我似乎无法做对。 I tried: 我试过了:

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

However, I get the following error: 但是,我收到以下错误:

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

As covered in the comments: git remote add otherRepo … only configures the remote, it does not fetch anything from it. 正如评论中所述: git remote add otherRepo …仅配置远程,它不从中获取任何东西。 You will need to run git fetch otherRepo to fetch the branches of the remote repository before you can create local branches based on them. 您需要运行git fetch otherRepo来获取远程存储库的分支,然后才能基于它们创建本地分支。


(responding to further comment by OP) (回应OP的进一步评论)

If you only want to keep track of a single branch from the remote repository, you can reconfigure your remote's fetch property ( remote.otherRepo.fetch ). 如果您只想跟踪远程存储库中的单个分支,则可以重新配置远程的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

After this, git fetch otherRepo will only fetch the remote repository's master branch into the otherRepo/master 'remote tracking branch' in your local repository. 在此之后, git fetch otherRepo将仅将远程存储库的master分支提取到本地存储库中的otherRepo/master “远程跟踪分支”。

To cleanup the other 'remote tracking branches', you could delete them all and re-fetch just the one you want, or you could selectively delete all of them except just the one you want: 要清理其他“远程跟踪分支”,您可以将它们全部删除并重新获取所需的那个,或者您可以有选择地删除所有这些除了您想要的那个:

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

If you decide that you want to track more than one remote branch, but not all of them, you can have multiple fetch configurations (with git config --add remote."$remoteName".fetch … or by using git config --edit to directly duplicate and edit the line in your repository's config file). 如果您决定要跟踪多个远程分支,但不是所有远程分支,则可以有多个获取配置(使用git config --add remote."$remoteName".fetch …或者使用git config --edit直接复制和编辑存储库配置文件中的行)。

If you also want to avoid fetching tags from the remote, configure your remote's tagopt property ( remote.otherRepo.tagopt ). 如果您还想避免从远程获取标记,请配置远程的tagopt属性( remote.otherRepo.tagopt )。

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

You could try 你可以试试

git checkout -b myTrack otherRepo/master 

This will create a new branch myTrack, which tracks the otherRepo/master branch. 这将创建一个新的分支myTrack,它跟踪otherRepo / master分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM