如何处理“fatal:当前分支的上游分支与当前分支名称不匹配”的问题?

4

有一个相关的SO问题,涉及如何更改push命令的参数以避免此错误消息:

fatal: The upstream branch of your current branch does not match the name of your current branch

我想知道如何消除这个消息本身,而不需要更改我的本地/远程分支名称或使用花哨的push命令。

假设我有一个跟踪不同名称的远程分支的本地分支:

user@home:~ git branch -vv
branch-name abcd1234 [remote/origin/branch-name] last commit message

现在我希望只需输入git push即可推送我的提交更改。当我这样做时,我会收到以下消息:
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 origin HEAD:remote/origin/branch-name

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

    git push origin branch-name

我如何强制git自动将代码推送到上游分支,即使名称不匹配? 我正在使用git 1.9.1

1个回答

9

较新的Git (版本1.9或更高)

git config --global push.default upstream

旧版本的 Git

git config --global push.default tracking

Git 2.3仍然将tracking作为上游的同义词接受。

只需执行一次,每当你执行"git push"时,它都会将当前分支推送到其配置的上游。

这也会在全局配置中设置它,可能会被存储库配置中的不同设置所遮盖。


谢谢Andrew,我输入了那个命令,然后键入了 git push,但是收到了相同的消息。 - quant
查看代码仓库的 .git/config 文件或系统 /etc/gitconfig 文件中是否存在冲突的设置。 - Andrew C
“tracking” 看起来至少有点可疑:列出的五种可能取值是 “current”、”matching”、”nothing”、”simple” 和 “upstream”。在这种情况下,“upstream” 似乎是最有可能被期望的一个。 - torek
跟踪和上游是相同的,我在我的答案中提到了这一点。Git在1.7和2.0之间的某个时候更改了名称,我不知道具体是什么时候。在2.1之前,跟踪仍将起作用,但如果您配置了上游并尝试使用1.7或更早版本,则肯定不会起作用。 - Andrew C
谢谢,很难相信这不是默认行为。我追踪这个远程分支是有原因的。 - Vistritium

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接