我如何将修改的提交推送到远程Git存储库? [英] How do I push amended commit to the remote Git repository?

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

问题描述

当我使用我的源代码工作了一段时间后,我做了平常的事情提交,然后推送到远程存储库。但后来我注意到我忘了在源代码中组织我的导入。因此,我执行修改命令来替换之前的提交:

 > git commit --amend 

不幸的是,提交不能被推回到版本库。这是被拒绝的:

 > git push origin 
到//my.remote.repo.com/stuff.git/
! [被拒绝] master - > master(非快速转发)
错误:无法将一些文件推送到'//my.remote.repo.com/stuff.git/'

我该怎么办? (我可以访问远程仓库。)

解决方案

我实际上曾经用 - force .git 存储库,并受到Linus BIG TIME 的指责。一般来说,这会给其他人造成很多问题。一个简单的答案是不要做。



我看到别人给出了这样做的秘诀,所以我不会在这里重复。但是,在 之后,您已经使用--force(或+ master)推出了修订后的提交,这里提供了一个小技巧。

  • 找到您修改的旧提交(称之为 old ),我们将通过修改来调用您创建的新提交。新)。

  • new ,记录 new 的树,如 git checkout new&& git merge -s我们的旧版本

  • 通过将主合并到您的主文件中 >
  • 使用 git push结果更新您的主人。 HEAD:master

  • 将结果推送出去。 然后那些不幸以自己的工作为基础的人们通过修改和强制推送(这是你是一个非常非常糟糕的男孩)将会看到你的合并将会看到你青睐超过。他们后来的合并将不会看到你的修改导致的 old new 之间的冲突,所以他们不必受苦。


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

    解决方案

    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".

    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. 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.

    Then people who were unfortunate enough to have based their work on the commit you obliterated by amending and forcing a push (which is you're being a very very bad boy) 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存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆