繁体   English   中英

如果要修改上一次远程提交,为什么需要进行git merge?

[英]Why is a git merge required if I want to amend last remote commit?

有时候我提交或推送一些代码,然后我意识到某个地方存在一个小错误。 所以我用新的更改来“修改最后一次提交”,而当我尝试推送时就会发生这种情况:

如果我还没有推送最后一次提交,那么一切正常

如果我已经推送了最后一次提交,我会得到

$ git push origin
To https://github.com/Kyklos-Italy/GitFashion.git
 ! [rejected]        develop -> develop (non-fast-forward)
error: failed to push some refs to 'https://github.com/Kyklos-Italy/GitFashion.git'
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.

所以我合并得到

$ git pull origin
Merge made by the 'recursive' strategy.

然后允许我推。

为什么会有这样的行为?

修改将创建一个全新的提交。 这意味着,您必须将本地生成的分支与远程更改合并。

例如,您有一些提交:

A---B---C 

当您修改这些更改后,它看起来像这样,其中C是您的原始内容, D是您的修改后的提交:

A---B---D
     \
      C

只要没有其他分支指向C ,您就不会再看到它,并且看起来您的版本没有任何变化(已推送时,远程分支仍应指向C ,例如origin/master )。 因为现在有一个新分支 ,所以您必须合并它们。

也许,您可以通过使用--force选项来避免合并。 赋予您所需的特权:

git push origin master --force

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM