本文介绍了如何将修改后的提交推送到远程 Git 存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我对我的源代码进行了一些工作后,我做了我通常的事情提交,然后我推送到远程存储库.但后来我注意到我忘记在源代码中组织我的导入.所以我做了修改命令来替换之前的提交:

When I've worked a bit with my source code, I did my usual thing commit and then I pushed to a remote repository. But then I noticed I forgot to organize my imports in the source code. So I do the amend command to replace the previous commit:

> git commit --amend

不幸的是,提交无法推送回存储库.是这样被拒绝的:

Unfortunately the commit can't be pushed back to the repository. It is rejected like this:

> git push origin
To //my.remote.repo.com/stuff.git/
 ! [rejected]        master -> master (non-fast forward)
error: failed to push some refs to '//my.remote.repo.com/stuff.git/'

我该怎么办?(我可以访问远程存储库.)

What should I do? (I can access the remote repository.)

推荐答案

其实我曾经用 --force.git 仓库推送,被 Linus 骂了大时代.一般来说,这会给其他人带来很多问题.一个简单的答案是不要这样做".

I actually once pushed with --force and .git repository and got scolded by Linus BIG TIME. In general this will create a lot of problems for other people. A simple answer is "Don't do it".

反正我看到其他人给出了这样做的秘诀,所以我不会在这里重复.但这里有一个提示,可以在您使用 --force(或 +master)推送修改后的提交之后 从这种情况中恢复.

I see others gave the recipe for doing so anyway, so I won't repeat them here. But here is a tip to recover from the situation after you have pushed out the amended commit with --force (or +master).

  1. 使用 git reflog 查找您修改的旧提交(称为 old,我们将通过修改 new).
  2. oldnew之间创建一个merge,记录new的树,比如git checkout new &&git merge -s ours old.
  3. 使用 git merge master
  4. 将其合并到您的 master
  5. git push 的结果更新你的主人.HEAD:大师
  6. 推出结果.
  1. Use git reflog to find the old commit that you amended (call it old, and we'll call the new commit you created by amending new).
  2. Create a merge between old and new, recording the tree of new, like git checkout new && git merge -s ours old.
  3. Merge that to your master with git merge master
  4. Update your master with the result with git push . HEAD:master
  5. Push the result out.

然后,那些不幸地将他们的工作基于您通过修改和强制推送删除的提交的人将看到结果合并将看到您偏爱 new 而不是 old代码>.他们以后的合并不会看到因您的修改而导致的 oldnew 之间的冲突,因此他们不必受苦.

Then people who were unfortunate enough to have based their work on the commit you obliterated by amending and forcing a push will see the resulting merge will see that you favor new over old. Their later merges will not see the conflicts between old and new that resulted from your amending, so they do not have to suffer.

这篇关于如何将修改后的提交推送到远程 Git 存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 19:55