Git在修改提交后阻止推送

kir*_*iri 41 git git-push git-amend git-commit

通常,我只是跑

git add file
git commit
git push
Run Code Online (Sandbox Code Playgroud)

但如果我推送之前修改了提交(with git commit --amend),则下一次推送失败

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Run Code Online (Sandbox Code Playgroud)

如何让git在不合并分支的情况下推送更改?我只有一个分支(master),我是唯一一个使用这个回购的人,为什么这么说呢?

git branch -a:

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
Run Code Online (Sandbox Code Playgroud)

编辑:使用gitk HEAD @{u},我看到我有2个分支,一个用原始提交,另一个用修改提交.

Joe*_*oey 60

只有在您修改已经推送的提交时才应该这样.通常,您应该永远不要这样做,因为您随后修改已发布的历史记录.但是,在您的情况下,您应该能够逃脱push -f,这将覆盖您修改的修订版本的远程提交.

  • 请注意 - 如果不谨慎使用`git push -f <remote> <branch>`可以覆盖您的提交历史记录.如果出现问题,请勿让您恢复提交. (2认同)

Nil*_*ner 13

是的,你不应该这样做(推送提交,然后更改它并尝试再次推送它).

相反,您可以在不更改文件的情况下将Git回滚到先前的提交,然后创建新的提交:

git reset --mixed origin/master
git add .
git commit -m "This is a new commit for what I originally planned to be an amendmend"
git push origin master
Run Code Online (Sandbox Code Playgroud)

这将创建一个新的提交,其中包含您即将修改的更改.


Ste*_*sca 8

你修改了提交的提交

git pull origin master
git commit -a --amend -m "..."
git push
Run Code Online (Sandbox Code Playgroud)

你可以通过恢复修改后的提交来解决问题:

git reset --mixed origin/master
Run Code Online (Sandbox Code Playgroud)

然后再次将其作为完全成熟的提交