我如何将修改的提交推送到远程Git存储库? [英] How do I push amended commit to the remote Git repository?
问题描述
当我使用我的源代码工作了一段时间后,我做了平常的事情提交,然后推送到远程存储库。但后来我注意到我忘了在源代码中组织我的导入。因此,我执行修改命令来替换之前的提交:
> 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 $之间创建合并c $ c>,记录
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).
- Find the old commit that you amended (call it
old
, and we'll call the new commit you created by amendingnew
). - Create a merge between
old
andnew
, recording the tree ofnew
, likegit checkout new && git merge -s ours old
. - Merge that to your master with
git merge master
- Update your master with the result with
git push . HEAD:master
- 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屋!