【问题标题】:Can't push to Git repository after merged branch合并分支后无法推送到 Git 存储库
【发布时间】:2012-12-08 23:26:10
【问题描述】:

我正在开发一个 Git 存储库,我有我的分支(称为 jviotti)并且我的更改已合并到 Master。 现在,在提交了一些更改后,我发现自己无法推送到我的分支。

$ git push origin jviotti
To https://github.com/jorisbontje/pikapay-frontend.git
 ! [rejected]        jviotti -> jviotti (non-fast-forward)
error: failed to push some refs to 'https://github.com/jorisbontje/pikapay-frontend.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

然后我尝试了 git pull:

$ git pull
You asked me to pull without telling me which branch you
want to rebase against, and 'branch.jviotti.merge' in
your configuration file does not tell me, either. Please
specify which branch you want to use on the command line and
try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) for details.

If you often rebase against the same branch, you may want to
use something like the following in your configuration file:

    [branch "jviotti"]
    remote = <nickname>
    merge = <remote-ref>
    rebase = true

    [remote "<nickname>"]
    url = <url>
    fetch = <refspec>

See git-config(1) for details.

在 SO 浏览了一个类似的问题后,我尝试了:

$ git pull origin master:jviotti
From https://github.com/jorisbontje/pikapay-frontend
 ! [rejected]        master     -> jviotti  (non-fast-forward)

我认为这样可以解决问题。我错过了什么?

【问题讨论】:

  • 你试过git pull origin jviotti吗?
  • 当你做git branch时,你看到了什么?

标签: git


【解决方案1】:

你首先应该确保你的本地分支有上游分支有它的上游。

git branch --set-upstream-to jviotti origin/jviotti
# or
git branch -u jviotti origin/jviotti

那是因为新的默认push policy will be "simple"

完成后:

git pull --rebase origin jviotti

这可以在origin/jviotti 之上重放您的主历史记录(如“What does it mean when git pull causes a conflict but git pull --rebase doesn't?”)。

但在那之后,如“git pull --rebase upstream & git push origin rejects non-fast-forward?”,你可能仍然需要:

git push --force origin jviotti

另一种选择是重置本地 repo,如 OP jviotti comments:

我刚刚再次克隆了 repo,更改了分支,效果很好。
可怕的 git 噩梦,现在一切正常。

【讨论】:

  • 看起来他正在使用本地 master 作为上游 jviotti - 这可能吗?
  • @EricWalker 未在第一次推送:jviotti -&gt; jviotti (non-fast-forward) 表示本地 jviotti 已分道扬镳。我确保本地分支具有正确的上游分支。我同意git pull origin master:jviotti看起来很阴暗,但可能会受到另一个 SO 答案的启发。
  • 好吧,我做了 git pull --rebase origin jviotti 并且它起作用了,但是它在我的代码中插入了奇怪的字符并破坏了一切。然后我再次克隆了 repo,更改了分支,效果很好。可怕的 git 噩梦,现在一切正常。谢谢
  • @jviotti:如果您发现自己一团糟,请查看git reflog。如果git gc 尚未运行,您通常可以reset --hard 到任何先前的提交。
猜你喜欢
  • 2012-07-01
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
  • 2014-03-15
  • 2018-06-24
  • 2013-07-04
  • 2010-10-25
相关资源
最近更新 更多