views:

55

answers:

3

I have the following git repo structure:

a-b-c-d-.. [master]
    \
     x

The commit x was accidentially done (can't figure out how) and has no named branch. How can I delete this commit?

For detail, git log shows this (Translated back from german, sorry for any inconvieniances):

Author: ...
Parents: 8444..
Branch:
Follows up:
Preceding:

...

The commit was not removable via git prune.

A: 

Run git gc aggressively. May take time.

git gc --aggressive

More on git gc here.

Ashish
x isn't a branch, it's rather a single commit.
FUZxxl
A branch can have only one commit. `C` and `X` have followed different paths, right?
Ashish
Thank you very much.
FUZxxl
@FUZxxl: Since one of your older comments does not exist (which created some confusion), let me confirm, `X` actually *does not* belong to a branch?
Ashish
The `--prune` option is on by default.
Ashish
@Ashish: Yes. gitk says, that there is no branch, all header fields in the commit info are empty except parent (As added above).
FUZxxl
+3  A: 

If x isn't in any branch's history, and isn't checked out, it will automatically be removed by git sooner or later. You can look at git help gc if you want to remove it right away. Setting gc.reflogExpireUnreachable to zero and then doing git gc --prune=now will probably do the trick.

git rebase, git commit --amend, and other such commands leave these kind of leftovers all the time; git is usually decent enough at dealing with them that you don't have to worry about it.

Walter Mundt
This is also very usefull. But I've not enough reputation to vote your answer up, sorry.
FUZxxl
This is the correct answer. Don't know what Ashish is talking about.
mehaase
From what I understood (from a comment that no longer exists) the `X` actually **did** belong to a branch. Otherwise, yes, the garbage collection takes care of it. Linus has stressed on its importance a lot.
Ashish
(Deleted comment: see above)
FUZxxl
OK. Does `git gc --aggressive` work? I've changed my answer accordingly.
Ashish
+1  A: 

git prune will do what you want, but it will delete all unreachable objects, not just a specific one.

haydenmuhl