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

时间:2018-09-28 17:22:13

标签: git

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

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

如果这些命令使用了它们,我不是100%。我所知道的是,提交不再位于远程存储库中。我虽然在本地git存储库中提交了。它不是HEAD(最新提交)。就像git log输出中的10次提交一样:

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

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

我正在考虑这样做:

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

这将确保从本地计算机上删除特定的git commit吗?

1 个答案:

答案 0 :(得分:1)

You have to be extremely careful to make other people aware of the situation so that things like this don't happen.

So... how can it be fixed? It can be corrected on your side, actually. So... say the commit that you want to be removed has ID xxxxxxxx, then:

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

That way the revision has been removed from history... it has implications if there are yet more developers working on the project (because they would have the revision you want to delete on their branches). Make sure that all developers are aware and rebase their work if needed before trying to push on the "new" clean branch.