Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to revert last commit and remove it from history?

Tags:

git

git-revert

I did a commit and reverted with

git revert HEAD^ 

just git log

➜  git:(master) git log commit 45a0b1371e4705c4f875141232d7a97351f0ed8b Author: Daniel Palacio <[email protected]> Date:   Tue Jan 17 16:32:15 2012 -0800      Production explanation 

But if I do git log --all it still show up. I need to remove it from the history as it has sensitive information

git log --all commit 5d44355080500ee6518f157c084f519da47b9391 Author: Daniel Palacio Date:   Tue Jan 17 16:40:48 2012 -0800      This commit has to be reset  commit 45a0b1371e4705c4f875141232d7a97351f0ed8b Author: Daniel Palacio  Date:   Tue Jan 17 16:32:15 2012 -0800      Production explanation 

How do I remove the commit 5d44355080500ee6518f157c084f519da47b9391 from the history too?

like image 268
daniel Avatar asked Jan 18 '12 01:01

daniel


People also ask

How do you revert a commit in git and remove from history?

(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.

How remove last commit from log?

In order to undo the last commit and discard all changes in the working directory and index, execute the “git reset” command with the “–hard” option and specify the commit before HEAD (“HEAD~1”).

Can I remove git commit from history?

Git doesn't have a modify-history tool, but you can use the rebase tool to rebase a series of commits into the HEAD. With the interactive tool, you can remove a commit that you want.


2 Answers

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.

like image 89
Lily Ballard Avatar answered Sep 22 '22 15:09

Lily Ballard


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.

like image 23
manojlds Avatar answered Sep 24 '22 15:09

manojlds