【问题标题】:Remove a commit in Git and any change with it删除 Git 中的提交及其任何更改
【发布时间】:2014-01-06 05:26:43
【问题描述】:

我在一个项目的很多文件中添加了很多行,然后我进行了提交。现在我想删除该提交及其所有更改。我的意思是我想删除提交,并随之删除该提交完成的所有代码更改。

【问题讨论】:

标签: git git-commit


【解决方案1】:

您想完全删除最后一次提交吗?可以了,但是小心,您无法撤消此操作:

git reset HEAD^ --hard

如果你想更加小心,你可以分两步做:

git reset HEAD^     # reset HEAD to previous commit but without changing files
git status          # review the changes that were in the last commit
git diff --cached   # review the changes that were in the last commit
git reset --hard    # *really* undo

【讨论】:

    【解决方案2】:

    janos 描述的git reset 方法可以正常工作,但很少需要完全消除提交,如果你养成了这种习惯,很容易陷入无法处理的情况。

    除非取消提交的原因是您不小心提交了一个巨大的文件或因为存在法律限制,否则我建议您恢复提交。这会保留提交,但会删除其所有更改。换句话说,提交仍将保留在项目的历史记录中(连同还原它的提交),但其更改将消失。

    git revert HEAD       # revert the currently checked out commit
    git revert 123456abc  # revert the commit with id 123456abc
    git revert master~    # revert the parent commit of the master branch
    

    git reset --hard 和其他重写分支历史的方法是强大且有用的工具,但了解其局限性和后果是个好主意。

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 2021-09-17
      • 2020-06-14
      • 2018-07-04
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多