JBentley
JBentley

Reputation: 6260

Remove a branch including its commits

I have created a branch where I was trying an approach to a problem which didn't work out, so I want to delete the branch, including all commits I made on it. For the sake of the argument, my branches look like this (when viewed with Gitk):

A--B--C--D Master
    \
     \-E-F Unwanted

I want it to look like this:

A--B--C--D Master

If I delete the Unwanted branch I end up with this instead:

A--B--C--D Master
    \
     \-E-F

I have no use for commits E and F, and don't want them cluttering my Git history.

Apologies for what may seem to be a very simple (and possibly stupid) question, but I've been unable to find a suitable answer elsewhere.

Upvotes: 2

Views: 83

Answers (2)

Ben Jackson
Ben Jackson

Reputation: 93890

If you run git gc it will remove unreferenced branches (which F is, assuming you didn't tag it or derive other branches from it) when they are older than a certain threshold (2 weeks default, and you can adjust it with --prune=<date>).

Upvotes: 3

Ryan Stewart
Ryan Stewart

Reputation: 128919

No, that's all you do. As long as E and F aren't referenced in any branch or tag, they'll eventually be garbage collected. I assume you're looking at it in gitk and still see the commits hanging out. If you restart gitk or do a forced reload with Ctrl+F5, they'll disappear. They're still there, but git will take care of them eventually. While you can force them to be cleaned up immediately, that's not a good habit to get into. The default time-to-live of 2 weeks for orphaned commits is there because just sometimes you really did want those commits, and you just didn't know it yet, so leave them orphaned and move on.

Upvotes: 5

Related Questions