Git push无法“推送”新分支

4
我从我们的代码库中检出了一个新分支。对于我们的develop分支,一切正常。但是在新的分支上,“push”命令没有做任何事情。一切看起来都正常——有两个提交需要推送。
-> git branch -vv
  develop   8ab7ef1 [origin/develop] Merge branch 'develop' of git.example.com:core-platform into develop
* hotfix112 8521cef [origin/hotfix/1.1.2: ahead 2] CORE-1263 - Completed merging from dev into hot fix.

但是 Push 并没有做任何事情:

-> git push
Everything up-to-date

-> git status
# On branch hotfix112
# Your branch is ahead of 'origin/hotfix/1.1.2' by 2 commits.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean

我发现 'git pull' 使用远程仓库,但 'git push' 不使用:

-> git remote show origin
* remote origin
  Fetch URL: git@git.example.com:core-platform.git
  Push  URL: git@git.example.com:core-platform.git
  HEAD branch: develop
  Remote branches:
    core-platform-1.0.0 tracked
    develop             tracked
    hotfix/1.1.2        tracked
    master              tracked
    release/1.0.0       tracked
    release/1.1.1       tracked
  Local branches configured for 'git pull':
    develop   merges with remote develop
    hotfix112 merges with remote hotfix/1.1.2
  Local ref configured for 'git push':
    develop pushes to develop (up to date)
->

我不明白为什么hotfix112没有连接到远程,但是pull操作可以。如何修复这个配置?

请查看以下内容: https://dev59.com/4m865IYBdhLWcg3wTcvg - rozar
3个回答

2

Git的行为与您所描述的情况相符,原因如下:

  • 您有一个本地分支(hotfix112),它正在跟踪不同命名的远程分支
  • 您已将push.default选项设置为匹配(默认)或简单

通常情况下,您有几种选择来设置git以按预期方式运行:

1.) 最简单的方法是将本地分支的名称更改为与远程分支的名称相匹配。之后,推送将自动开始工作。只需将分支“hotfix112”重命名为“hotfix/1.1.2”即可:

git branch -m hotfix112 hotfix/1.1.2

2.) 通过将选项push.default设置为'tracking',您可以更改推送行为。因为您已经将分支'hotfix112'设置为跟踪origin/hotfix/1.1.2,所以git push将按预期工作。要设置本地git选项,请运行:

git config --local push.default tracking

3.) 您可以手动编辑.git/config文件,并将推送设置为与本地refspec 'hotfix112'匹配到远程'hotfix/1.1.2'。您应该在[remote "origin"]部分下面添加推送行:

[remote "origin"]                                                               
  url = ...
  fetch = ...
  push = hotfix112:hotfix/1.1.2

第一种和第三种方法只适用于hotfix112分支。第二种方法适用于该存储库中的所有跟踪分支(如果使用全局选项,则适用于全局)。


1
在2.0中,push.default的值将是simple,但匹配目前是默认值(http://git-scm.com/docs/git-config)。 - Eric Mathison
你说得对,Eric。我忘记了一段时间以前在我的全局配置中设置它了...已做出相应的更新...谢谢! - jurglic
谢谢,我选择了#1。较短的分支名称似乎是个好主意,但会在下游引起混淆。 - Steve s.
但是为什么Git会说 Everything up-to-date,而不是类似于“本地分支名称与远程分支名称不匹配,并且 push.default 设置为 X”? - doubleOrt

0
尝试使用git push origin/hotfix/1.1.2(远程分支)hotfix112(本地分支),可能在将其推送到远程后会创建一个链接。奇怪的是,在你分支时它没有这样做。

谢谢提示。这个命令很好用:git push origin hotfix112:hotfix/1.1.2。但是遗憾的是配置没有改变。 - Steve s.
这个怎么样?https://dev59.com/J2sz5IYBdhLWcg3wADS6 - Jack Thor
1
Bingo: git push -u origin hotfix112:hotfix/1.1.2 分支hotfix112已设置为跟踪来自origin的远程分支hotfix/1.1.2。 - Steve s.

0

git push 的当前行为(没有任何额外的参数)是推送本地和远程仓库中具有相同名称的所有分支。由于您的本地分支名称与远程分支名称不同,如果更改它们以匹配,git push 应该会推送您的更改。请注意,这种行为将在未来的 git 版本中更改(可能是 2.0)。请参见:http://article.gmane.org/gmane.comp.version-control.git/193308。如果您想立即更改 git push 的行为,请键入 git help config 并搜索“push.default”。


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