![](/img/trans.png)
[英]How to configure git push to automatically set upstream without -u?
[英]How to configure remote upstream without `git push`
是否可以在不執行git push
的情況下為新的本地分支配置遠程上游分支?
我認為這是git branch
的--set-upstream-to
選項的用途,但我收到以下錯誤:
$ git branch --set-upstream-to remote2/foobar foobar
fatal: the requested upstream branch 'remote2/foobar' does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.
hint: Disable this message with "git config advice.setUpstreamFailure false"
我知道 push 命令的配置在推送時自動執行此操作,但我不想使用它。 我想做的是編寫一個腳本,確保主分支被推送remote1
而所有其他分支被推送remote2
。 特別是我希望它 (1) 從origin1
克隆 repo A (2) 添加origin2
作為遠程,(3) 檢查一個新的本地分支,(4) 確保git push
送到origin1
。 我以為我可以做這樣的事情但是我得到了上面的錯誤:
$ git clone git@github.com:username/repo1.git $1
$ pushd $1
$ git remote add origin2 git@github.com:username/repo2.git
$ git checkout -b $1
$ git branch --set-upstream-to origin2/$1
$ popd
如果您的意圖是在您的腳本中推送到origin2
:
您可以,尤其是在腳本中,顯式鍵入遠程引用的全名,並另外將-u
選項設置為您的git push
命令:
git push -u origin2 "$branch:refs/heads/$branch"
如果您的意圖是在您的腳本中僅設置上游分支而不是推送:
“遠程跟蹤分支”只是您本地.git/config
文件中的一個設置(實際上是兩個)。
設置不存在的跟蹤分支的一種 hacky 方法是使用git config
:
git config "branch.$branch.remote" origin2
git config "branch.$branch.merge" "refs/heads/$branch"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.