将本地 git 分支与删除特定提交的远程 git 分支同步

  • 本文关键字:分支 git 提交 同步 删除 git
  • 更新时间 :
  • 英文 :


我共享一个远程 git 存储库。用户实际上删除了他们在 git 历史记录中不需要的特定提交(提交不是最新的提交(。也许他们做了这样的事情:

git reset --hard <sha1-commit-id>
git push origin HEAD --force

如果这些是他们使用的命令,我不是 100%。我只知道提交不再在远程存储库中。不过,我在本地 git 存储库中有提交。它不是 HEAD(最新的提交(。这就像 git 日志输出中的 10 次提交:

git log
...
commit a5df5
Author:
Date:
This is the commit that I have locally but no longer in remote

现在,每当我执行 git 推送时,它都会重新出现在远程存储库中。如何确保此提交不再推送到远程存储库?

我正在考虑这样做:

git fetch origin latest_stuff
git reset --hard origin/latest_stuff

这会确保特定的 git 提交从我的本地机器中消失吗?

你必须非常小心,让其他人意识到这种情况,这样这样的事情就不会发生。

那么......如何解决呢?实际上,它可以在您这边进行纠正。所以。。。假设您要删除的提交具有 ID xxxxx,则:

git checkout xxxxxxxx~1 # we go back one commit from the one we want to discard from history
git cherry-pick xxxxxxxx..the-branch # replay the history of the branch _after_ the revision you want to delete
git branch -f the-branch # set pointer of branch to new location
git push origin -f the-branch

这样,修订版已从历史记录中删除...如果有更多的开发人员在处理该项目,则会产生影响(因为他们将在其分支上具有您要删除的修订版(。确保所有开发人员都知道并在需要时重新调整他们的工作,然后再尝试推送"新的"干净分支。

最新更新