调用 git pull 时删除本地提交
我进行了一次提交,但出现错误(程序未编译),提交后:git push (I)
。
我的同事完成了:git pull (She)
并获得存储库的未编译状态。之后添加了一些提交(关于项目文档 - 编译并不重要)并再次完成:git push (She)
之后,我们得到了以下存储库状态:
- 她的一些提交
- 她的一些提交
- 我的提交有错误
- 我的另一个提交
我确实想删除提交 3。为此我做了
git-rebase --onto <提交 4 的 sha>大师
git push --force
现在我们有了正确的存储库状态(没有提交 3),但还有所有其他更改。 但是,如果她这样做,
git pull
git push
她将与本地提交 #3 合并,然后将其拉到存储库。我怎样才能让某人(不仅是她)在 git pull 之后纠正存储库的状态 - 进行所有更改,但没有提交#3?
注释: 可能她在最后添加了(在另一种情况下 - 功能)本地提交。并且她的本地存储库比服务器的存储库更新。
I've made a commit with an error (program isn't compiled) and after made: git push (I)
.
My colleague done: git pull (She)
and got uncompiled state of repository. After that added some commits (about project documentation - for the compiling wasn't critical) and done again: git push (She)
After that we've got following state of repository:
- Some her commit
- Some her commit
- My commit with error
- My another commit
I wanted exactly to delete commit 3. And for that I've madegit-rebase --onto <sha of commit 4> <sha commit 3> master
git push --force
Now we have correct state of repository (without commit 3), but with all another changes.
But, if she do
git pull
git push
she will make merge with her local commit #3 and then pull it to repository. How can I make that someone (not only she) after git pull will correct state of repository - with all changes, but without commit #3?
Notes:
Probably she added (in another case - feature) local commits above last. And her local repository newer than server's repository.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你已经发现了改变公共历史的问题:每个看到改变的人都必须同意以同样的方式改变它。在这个简单的情况下,他们可能还可以通过执行与您相同的
rebase
命令来获取新的正确版本:这将更新他们的本地 master 以再次与远程 master 分支达成一致,并且他们可以继续普通的。
没有办法自动导致这种情况发生。对于那些只是跟踪 master 或在执行此 rebase 时推送了他们所做的所有更改的人来说,最简单的方法可能是执行 git reset --hard origin/master 。但是这将删除在执行变基操作时不在
master
中的当前分支上所做的任何本地提交。实现相同目标的另一种方法是使用 git revert。这将简单地添加一个新的提交,它与不同的提交相反。这不会编辑历史记录,它只是以撤消历史记录的方式添加历史记录。这会导致问题提交仍然出现在 git log 和相关命令中,但不会给其他人带来问题。
You have discovered the problem with changing public history: everyone who has seen the change must agree to change it the same way. In this simple case, they can likely get the new proper version by also doing the same
rebase
command that you did:This will update their local master to agree with the remote master branch again and they can continue as normal.
There is no way to automatically cause this to happen. The easiest way for people who were simply tracking
master
, or had pushed all changes they made when you did this rebase, may be to executegit reset --hard origin/master
but this will erase any local commits made on the current branch that weren't inmaster
when you did your rebase.An alternate way to achieve the same thing is with
git revert
. This will simply add a new commit which is the inverse of a different commitish. This doesn't edit history, it simply adds to it in such a way as to undo it. This causes the problem commit to still show up ingit log
and related commands, but doesn't cause issues for everyone else.