blueFast
blueFast

Reputation: 44381

Delete commit, really

New repo, no remote (so: no pushing), no tags used.

» echo "aaa" > aaa
» git init
» git add .
» git commit -a -m "Added aaa"
» echo "bbb" > aaa
» git commit -a -m "This commit is wrong"

f9892d8  :  author  :  1 second ago   : (HEAD, master)   :  This commit is wrong
e80f2bd  :  author  : 14 seconds ago  :                  :  Added aaa

» git reset --hard HEAD~1

e80f2bd  :  author  : 14 seconds ago  :                  :  Added aaa

» git checkout f9892d8
Note: checking out 'f9892d8'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at f9892d8... This commit is wrong

So, even after git reset --hard the commit is still there. Doing garbage collection (git gc) does also not help.

I really want to get rid of the wrong commit, forever. As if it never existed. Not in this branch, not in any branch, not in the history, nowhere!

Is it possible to tell git "forget this commit, it never existed"?

EDIT

To make it perfectly clear: I am not interested in removing the commit from the current branch, but on removing the commit from the whole repo. No traces of it must be anywhere in the repo: not in another branch, not as orphan commits, simply nowhere. As if it never existed.

Upvotes: 0

Views: 118

Answers (1)

torek
torek

Reputation: 488619

Git is always reluctant to really remove anything from the repository; simply making it invisible is "good enough", git insists, because maybe you'll want to un-delete it later and if it's merely invisible, why, then, you can get back!

Of course, eventually all those invisible but still-there objects (commits, trees, blobs, and even annotated tag objects) will fill up your disk. ("But disk space is infinite", git keeps telling me! :-) ) So, yes, it is possible to get that commit truly garbage-collected and gone for good.

The method is embedded in this answer: search for “Checklist for Shrinking a Repository.” You don't need to worry about the refs/original/refs/heads/master part since that's created by git filter-branch (which you didn't use here).

Upvotes: 1

Related Questions