How to permanently remove few commits from remote branch

后端 未结 8 1768
予麋鹿
予麋鹿 2020-11-27 09:07

I know that\'s rewriting of history which is bad yada yada.

But how to permanently remove few commits from remote branch?

8条回答
  •  臣服心动
    2020-11-27 09:22

    If you want to delete for example the last 3 commits, run the following command to remove the changes from the file system (working tree) and commit history (index) on your local branch:

    git reset --hard HEAD~3
    

    Then run the following command (on your local machine) to force the remote branch to rewrite its history:

    git push --force
    

    Congratulations! All DONE!

    Some notes:

    You can retrieve the desired commit id by running

    git log
    

    Then you can replace HEAD~N with like this:

    git reset --hard 
    

    If you want to keep changes on file system and just modify index (commit history), use --soft flag like git reset --soft HEAD~3. Then you have chance to check your latest changes and keep or drop all or parts of them. In the latter case runnig git status shows the files changed since . If you use --hard option, git status will tell you that your local branch is exactly the same as the remote one. If you don't use --hard nor --soft, the default mode is used that is --mixed. In this mode, git help reset says:

    Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated.

提交回复
热议问题