tags:

views:

40

answers:

2

Through a comedy of noob errors I find myself in the following situation in a local repo:

alt text

Using git reset I have maneuvered the branch tags to where I want them, and I would now like to delete the 3 commits on what looks like an unnamed branch. I have researched/tried a variety of things including gc, prune without success.

I realize that I could just blow away the local repo and re-clone it, however I'd like to use it as a learning opportunity.

So the question is: How can I delete those commits?

+1  A: 

Have you looked at the reflog? It keeps track of every way that HEAD moves inside your repository (and it’s doing its job by preventing the automatic deletion of your commits).

Examine it with git reflog or git log -g. Nuke it with git reflog expire --expire=now --all, and then you can garbage collect the unused commits.

jleedev
This will work, but I don't really see any reason to do anything so drastic... Unless those commits contain some enormous blobs, they're not doing any harm.
Jefromi
Alas, after cleaning the reflog, `git gc --aggressive --prune=now` still doesn nothing.
rlduffy
Belay that! It appears that it did do the job, but I didn't see it in gitk because I was just doing a refresh. When I restarted gitk (per Jefromi's suggestion) the dangling branch went away.
rlduffy
+5  A: 

jleedev's answer will certainly get them gone, but I really don't think you should do that.

The commits will automatically be cleaned up by the next gc (which is run automatically by several key commands) once they are no longer in the reflog, which will expire in 90 days. That is, they'll be gone eventually; git's just keeping you from deleting anything you might not mean to.

If it just bothers you to see them in gitk, all you have to do is reload the view (or restart gitk). Now that those commits aren't on a branch, they won't be shown. Yes, they'll still be in your repository, but unless you're running out of disk space, that doesn't hurt you. It's just a safety net.

Jefromi
+1 Good point, alas I'd already done the `gc`. Never had an appreciation of the distinction between `update` and `reload` in gitk.
rlduffy