[英]How to configure remote upstream without `git push`
Is it to possible to configure the remote upstream branch for a new local branch without doing a git push
?是否可以在不执行
git push
的情况下为新的本地分支配置远程上游分支?
I thought this is what --set-upstream-to
option of git branch
is for but I get the following error:我认为这是
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"
I'm aware of the config for the push command to do this automatically when pushing but I do not want to use that.我知道 push 命令的配置在推送时自动执行此操作,但我不想使用它。 What I'm trying to do is writing a script that makes sure main branch is pushed
remote1
while all other branches are pushed remote2
.我想做的是编写一个脚本,确保主分支被推送
remote1
而所有其他分支被推送remote2
。 In particular I want it to (1) clone repo A from origin1
(2) add origin2
as a remote, (3) checkout a new local branch, (4) make sure git push
on this branch goes to origin1
.特别是我希望它 (1) 从
origin1
克隆 repo A (2) 添加origin2
作为远程,(3) 检查一个新的本地分支,(4) 确保git push
送到origin1
。 I thought I could do something like this but I get the error above:我以为我可以做这样的事情但是我得到了上面的错误:
$ 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
If your intention is, in your script, to push to origin2
:如果您的意图是在您的脚本中推送到
origin2
:
you can, especially in a script, explicitly type the full name of the remote reference, and additionally set the -u
option to your git push
command:您可以,尤其是在脚本中,显式键入远程引用的全名,并另外将
-u
选项设置为您的git push
命令:
git push -u origin2 "$branch:refs/heads/$branch"
If your intention is, in your script, to just set the upstream branch and not push:如果您的意图是在您的脚本中仅设置上游分支而不是推送:
a "remote tracking branch" is just a setting (two actually) in your local .git/config
file. “远程跟踪分支”只是您本地
.git/config
文件中的一个设置(实际上是两个)。
one hacky way to set a tracking branch that doesn't exist is to use 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.