如何在创建本地分支时设置自定义上游分支名?
如何在创建本地分支时配置自定义上游远程分支名?
我的工作流程中,本地分支名称不希望在远程仓库显示,需要在创建本地分支时就设置好对应的上游分支名称,这样执行git push时无需额外配置。
我试过三种常见的设置上游分支的方法,但都有问题:
- 使用
git config --local remote.origin.push LOCAL_BRANCH:REMOTE_BRANCH:推送时能推送到预期远程分支,但会重置该配置项的现有值,不符合需求。 - 使用
git branch --set-upstream-to REMOTE_BRANCH:远程分支不存在时无法使用,会抛出错误:fatal: the requested upstream branch '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" - 使用
git push -u或git push origin LOCAL_BRANCH:REMOTE_BRANCH:需要在推送时操作,不符合“创建分支时完成配置”的需求。
我曾尝试先推送到一个名称不同的新远程分支,此时本地Git配置会生成以下条目:
branch.foo.remote=origin branch.foo.upstream=foobar branch.foo.merge=refs/heads/foobar
但如果删除这些条目后手动创建不同名称的配置,再次推送(不加-u)时,Git仍然会使用本地分支名作为远程分支名。
所以我想确认:是否存在一种方法,能在创建新本地分支时就配置好对应的远程分支名,且首次推送时无需显式指定该名称?
编辑:经测试,找到的方法在命令行Git中有效,但在IntelliJ中似乎无效(推测是IntelliJ缓存了Git配置信息)。
内容的提问来源于stack exchange,提问作者David Beaumont
相关产品推荐
相关产品推荐

