Peter N. Steinmetz
Peter N. Steinmetz

Reputation: 1252

Remove extraneous commits from a git repository

I have a repository which contains a bunch of commits from a different project and which are not on any branch in the current project. They just show up in the history connected to each other but not connected to any branch in the project.

So if I perform git branch --contains <commit hash> it returns nothing.

(I believe these got there while making some changes to a remote gitolite3 repository and a fetch when its state was in error).

I would like to remove them from the commit database as they don't belong here at all.

How can I do that?

I have tried git prune before and after git reflog expire --expire=now --expire-unreachable=now --all but it doesn't remove them. I have also tried git gc but that didn't help either.

This appears related to Completely remove commit from git database but I can't find anything in the reflog or a branch referring to these commits which would prevent prune from removing them. I hate to just have to go and delete the objects from the database, which seems like a bad way to do things.

Upvotes: 3

Views: 196

Answers (1)

Peter N. Steinmetz
Peter N. Steinmetz

Reputation: 1252

The other thing that can cause git gc to not delete a commit is if there are tags referring to it or the chain which contains it.

git tag --list # shows the tags
git tag -d <extraneous tag> # removes any such tags
git gc # then deletes the extraneous commits

This was suggested by related post Remove commits that are not connected to the branch

Upvotes: 3

Related Questions