簡體   English   中英

Git設置遠程跟蹤分支

[英]Git setup remote tracking branch

我想從新的遠程存儲庫跟蹤遠程主分支。 兩者都已存在。

我怎么用git來解決這個問題? 我似乎無法做對。 我試過了:

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

但是,我收到以下錯誤:

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

正如評論中所述: 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

你可以試試

git checkout -b myTrack otherRepo/master 

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM