代码之家  ›  专栏  ›  技术社区  ›  Daniel Viglione

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

git
  •  1
  • Daniel Viglione  · 技术社区  · 8 年前

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

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

    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提交从我的本地机器上消失吗?

    1 回复  |  直到 8 年前
        1
  •  1
  •   eftshift0    8 年前

    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
    

    这样修订就从历史上消失了。。。如果有更多的开发人员在项目中工作(因为他们的分支上会有您想要删除的修订版),那么这会产生影响。确保所有的开发人员都知道,如果需要的话,在尝试推进“新的”clean分支之前重新调整他们的工作。