How to revert last commit and remove it from history?

巧了我就是萌 提交于 2019-12-02 16:35:46

First off, git revert is the wrong command here. That creates a new commit that reverts an older one. That's not what you're asking for. Secondly, it looks like you want to revert HEAD instead of HEAD^.

If you haven't pushed this anywhere, you can use git reset --hard HEAD^ to throw away the latest commit (this also throws away any uncommitted changes, so be sure you don't have any you want to save). Assuming you're ok with the sensitive information being present in your copy and nobody else's, you're done. You can continue to work and a subsequent git push won't push your bad commit.

If that's not a safe assumption (though if not I'd love to hear why), then you need to expire your reflogs and force a garbage collection that collects all outstanding objects right now. You can do that with

git reflog expire --expire=now --expire-unreachable=now --all
git gc --prune=now

though this should only be done if you really absolutely need to do it.


If you have pushed your commit, then you're pretty much out of luck. You can do a force-push to revert it remotely (though only if the remote side allows that), but you can't delete the commit itself from the remote side's database, so anyone who has access to that repository can find it if they know what to look for.

If you don't care about the commit, just do:

git reset --hard HEAD~

to blow away the commit.

If you want the changes to be in working directory, do:

git reset HEAD~

Depending on what you have done with git revert, you might have to change the above commands. Revert creates a new commit that reverts the commit you wanted to revert. So there will be two commits. You might have to do HEAD~2 to remove them both.

Note that, usually, revert is the safer way to, well, revert changes. But here, since you want to remove sensitive data, reset is the best approach.

There is a nice solution here. To delete the last (top) commit you can do

git push [remote] +[bad_commit]^:[branch]

where [bad_commit] is the commit that [branch] currently points to, or if the [branch] is checked out locally, you can also do

git reset HEAD^ --hard
git push [remote] -f

If you have not pushed the commit yet, you can just:

git reset --hard HEAD~2

(HEAD~2 to remove your original commit and your "revert" commit).

This will reset your current branch to the point in history before the commit you want to remove. If that commit is not in any other branch, it will not be pushed to your origin.

Here is a simple working solution that delete the last commit from remote:

  1. clone the repo and find the last 'good' commit (....c407)
$ git clone git@host:PROJ/myrepo.git 
$ cd myrepo 
$ git log --pretty=oneline

234987fed789de97232ababababcef3234958723 bad_commit
e4a2ec4a80ef63e1e2e694051d9eb3951b14c407 v4.3
2f116449144dbe46e7260d5dac2304803751b5c2 v4.2
  1. checkout the last good commit to a new temp branch
$ git checkout e4a2ec4a80ef63e1e2e694051d9eb3951b14c407
$ git checkout -b temp_branch
  1. replace the remote branch ( by delete and push the temp )
git push origin --delete dev_branch
git push origin temp_branch:dev_branch
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!