调用 git pull 时删除本地提交

发布于 2024-12-06 11:26:48 字数 636 浏览 3 评论 0原文

我进行了一次提交,但出现错误(程序未编译),提交后:git push (I)
我的同事完成了:git pull (She) 并获得存储库的未编译状态。之后添加了一些提交(关于项目文档 - 编译并不重要)并再次完成:git push (She)
之后,我们得到了以下存储库状态:

  1. 她的一些提交
  2. 她的一些提交
  3. 我的提交有错误
  4. 我的另一个提交
    我确实想删除提交 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:

  1. Some her commit
  2. Some her commit
  3. My commit with error
  4. My another commit
    I wanted exactly to delete commit 3. And for that I've made
    git-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的轨迹 2024-12-13 11:26:48

你已经发现了改变公共历史的问题:每个看到改变的人都必须同意以同样的方式改变它。在这个简单的情况下,他们可能还可以通过执行与您相同的 rebase 命令来获取新的正确版本:

git-rebase --onto <sha of commit 4> <sha commit 3> master

这将更新他们的本地 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:

git-rebase --onto <sha of commit 4> <sha commit 3> master

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 execute git reset --hard origin/master but this will erase any local commits made on the current branch that weren't in master 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 in git log and related commands, but doesn't cause issues for everyone else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文