合并分支后无法推送到Git代码库

6

我正在处理一个Git仓库,我有自己的分支(名为jviotti),并且我的更改已经合并到主分支。现在,在提交了一些更改后,我发现无法将更改推送到我的分支。

$ 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)

我觉得那应该可以解决问题。我错在哪里了?

1
你尝试过执行 git pull origin jviotti 吗? - Jonas Schäfer
当你执行 git branch 命令时,你会看到什么? - Eric Walker
1个回答

4

首先,您必须确保本地分支具有其上游分支的上游。

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

这是因为新的默认推送策略将变成“simple”

一旦完成这个步骤:

git pull --rebase origin jviotti

这可以在origin/jviotti的基础上重放您的主分支历史记录(如"git pull引起冲突但git pull --rebase不会时是什么意思?")。

但在此之后,如"git pull --rebase upstreamgit push origin拒绝非快进式合并?"中所述,您可能仍需要执行以下操作:

git push --force origin jviotti

另一个选项是重置本地仓库,正如jviotti评论中所述:

我只是重新克隆了仓库,更改了分支,然后它就工作得很好。
Git的噩梦,现在一切都好了。


他似乎在使用本地的 master 作为上游的 jviotti - 这是可能的吗? - Eric Walker
@EricWalker不是在第一次推送时出错:jviotti-> jviotti(non-fast-forward)表示本地的jviotti分支已经发生了分叉。我确保本地分支有正确的上游分支。我同意git pull origin master:jviotti看起来有些可疑,但可能是太过紧跟另一个SO答案。 - VonC
我执行了git pull --rebase origin jviotti,它起作用了,但是它在我的代码中插入了奇怪的字符并破坏了一切。然后我只是重新克隆了仓库,更改了分支,它就正常工作了。可怕的git噩梦,现在一切都好了。谢谢。 - jviotti
如果你发现自己陷入了困境,可以尝试使用 git reflog 命令。只要没有运行 git gc 命令,通常可以通过 reset --hard 命令回到之前的任何提交状态。 - Eric Walker

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