Delete commits from a branch in Git

后端 未结 30 2054
醉话见心
醉话见心 2020-11-21 07:17

I would like to know how to delete a commit.

By delete, I mean it is as if I didn\'t make that commit, and when I do a push in the future, my changes wi

30条回答
  •  北恋
    北恋 (楼主)
    2020-11-21 07:57

    If you want to fix up your latest commit, you can undo the commit, and unstage the files in it, by doing:

    git reset HEAD~1
    

    This will return your repository to its state before the git add commands that staged the files. Your changes will be in your working directory. HEAD~1 refers to the commit below the current tip of the branch.

    If you want to uncommit N commits, but keep the code changes in your working directory:

    git reset HEAD~N
    

    If you want to get rid of your latest commit, and do not want to keep the code changes, you can do a "hard" reset.

    git reset --hard HEAD~1
    

    Likewise, if you want to discard the last N commits, and do not want to keep the code changes:

    git reset --hard HEAD~N
    

提交回复
热议问题