如何默认推送到不同名称的分支?

How to push by default to a differently named branch?

我有两个分支:

master
demo_master

还有两个遥控器:

origin
demo_origin

如何配置 git 自动推送 master 到 origin/masterdemo_master 到 demo_origin/master 发出 git push 命令时?


我已尝试通过

获得预期的结果
git push --set-upstream demo_origin demo_master:master

但是,每当我尝试使用 git push:

时,git 就会出现以下错误

fatal: The upstream branch of your current branch does not match the name of your current branch. To push to the upstream branch on the remote, use

git push demo_origin HEAD:master

To push to the branch of the same name on the remote, use

git push demo_origin demo_master

master 本地分支上使用 git push origin -u master,在 demo_master 分支上使用 git push demo_origin -u master

另一个解决方案是使用 master 分支的 git branch -u origin/masterdemo_master 分支的 git branch -u demo_origin/master

https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches#Tracking-Branches

您需要 set the push.default configuration 正确:

Defines the action git push should take if no refspec is explicitly given. Different values are well-suited for specific workflows; for instance, in a purely central workflow (i.e. the fetch source is equal to the push destination), upstream is probably what you want.

在 Git 2.0 中,此设置默认设置为 simple,以方便初学者使用:

simple - in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch’s name is different from the local one.

When pushing to a remote that is different from the remote you normally pull from, work as current. This is the safest option and is suited for beginners.

如选项所述,Git 将拒绝推送到具有不同名称的分支。为此,您需要将其设置为 upstream:

upstream - push the current branch back to the branch whose changes are usually integrated into the current branch (which is called @{upstream}). This mode only makes sense if you are pushing to the same repository you would normally pull from (i.e. central workflow).

所以,执行 git config -g push.default upstream,然后它应该可以与 git push --set-upstream demo_origin master:demo_master 一起正常工作。 (注意本地分支在local:remote中排在第一位)

With Git 2.0, this setting was set to simple by default, to make it easy for beginners:

simple - in centralized workflow, work like upstream with an added safety to refuse to push if the upstream branch’s name is different from the local one.

When pushing to a remote that is different from the remote you normally pull from, work as current. This is the safest option and is suited for beginners.

已用 Git 2.33(2021 年第 3 季度)重写,并在 git push.

周围添加了说明

参见 commit 90cfb26, commit 7e6d72b, commit b8e8b98, commit 6b010c8, commit d099b9c, commit 3b9fd83, commit 050f76b (31 May 2021) by Felipe Contreras (felipec)
(由 Junio C Hamano -- gitster -- in commit 07e230d 合并,2021 年 7 月 13 日)

doc: push: explain default=simple correctly

Cc: Elijah Newren
Signed-off-by: Felipe Contreras

The simple mode only barfs when working on a centralized workflow, and there's no configured upstream branch with the same name.

git config 现在包含在其 man page 中:

simple

Pushes the current branch with the same name on the remote.

If you are working on a centralized workflow (pushing to the same repository you pull from, which is typically origin), then you need to configure an upstream branch with the same name.

This mode is the default since Git 2.0, and is the safest option suited for beginners.


这就解释了为什么默认设置不适合您的情况,而您需要,如上所述:

git config -g push.default upstream, 
git push --set-upstream demo_origin master:demo_master

注意第二次推送只针对第一次推送:设置上游分支后,一个简单的git push(无参数)就足够了。