代码之家  ›  专栏  ›  技术社区  ›  Jonathan S. Fisher

Git删除特定提交后的远程历史记录

  •  16
  • Jonathan S. Fisher  · 技术社区  · 12 年前

    我有一个Git/Gitlab存储库。我们曾经直接提交给master,但我们决定在这个版本中改用像世界其他地方一样的功能分支。

    我们需要将远程主机重置为上次发布后的状态。如果有人已经直接向master提交了,我如何将其重置为干净状态,删除上次发布之前的所有历史记录?

    我花了大约一个小时在谷歌上搜索,但找不到这个具体问题的答案。很抱歉,如果它看起来多余,这似乎是一个简单的任务,没有明显的答案!

    4 回复  |  直到 12 年前
        1
  •  39
  •   Paul Draper    10 年前

    为了复位本地分支,

    git branch -f master last-release
    

    为了重置远程分支,

    git push -f origin last-release:master
    

    哪里 last-release 是要重置的ref(提交id或分支) master

    (这两个都不会影响你的工作树;如果你愿意,你甚至可以从一个简单的repo中执行这些操作。)

        2
  •  6
  •   Jonathan S. Fisher    12 年前

    有时,你在Stack Overflow上发帖,一秒钟后你就会马上发现:

    $ git reset --hard HEAD~9
    $ git push --all --force
    

    现在删除本地存储库,重新克隆。

        3
  •  0
  •   Mohammed Maaz    3 年前

    按照以下步骤,签出特定提交并推送新分支并删除分支保护规则,强制将新分支推送到主分支并添加回您删除的分支保护规则。

    git log --online (to get the commit hash that you wish to revert) 
    git checkout <commit-hash>
    git branch <new-branch-name> <new-commit-hash> 
    git push origin <new-branch-name>
    
    # Goto Github and remove branch protection rule (for master)
    git push -f origin <new-branch-name>:master
    
    # Go back to Github and add back the branch protection rule (for master/main)
    
    
        4
  •  0
  •   Adrien Bénété    3 年前

    对于远程回购

    git push -f origin <last-release-number>:<branch_name>
    

    对于本地回购

    git reset --hard <last-release-number>
    

    “最后发布号”必须是您要返回的最后一次提交