wojtek1902
wojtek1902

Reputation: 611

How to delete already removed commit from detached head?

I have the following problem - I wanted to delete some commits from my git repository history (yes, I know it's a bad habit, but I know what I'm doing). I made it using rebase (I got the instructions from there: https://ncona.com/2011/07/how-to-delete-a-commit-in-git-local-and-remote/) and the commits were removed from my remote branch, but there are still in the history (on a DETACHED_HEAD). How can I get rid of them so that they could disappear completely? Thank you for any help!

Upvotes: 0

Views: 1501

Answers (1)

Obsidian
Obsidian

Reputation: 3897

… the commits were removed from my remote branch, but there are still in the history (on a DETACHED_HEAD). How can I get rid of them so that they could disappear completely?

The normal way of doing is to let them die by themselves. The garbage collector will eventually get rid of everything that is no longer in use. You may also manually run it with git gc.

"DETACHED HEAD" simply means that you're currently checking out any commit that is not linked to a branch name (or you checked it out using the commit SHA sum, not the branch name), implying that if you add/commit stuff from this state, the operation will occur but won't be linked to anything by default, and no branch name will be automatically updated at this stage.

If however you have a good reason to make them disappear (accidentally committed a password in human-readable form, boss bashing messages or porn pictures stored in the wrong folder), you first need to check if your commits are no longer reachable, neither from an official reference such as a tag or a branch name, nor from any other commit remaining steady, nor from any entry in the different reflogs.

So you need to clean all this out to be sure that your commit will be scavenged.

You can take a look to this entry to know what options to pass to git gc in order to enjoin it to make everything expire right now, but don't rush into it until you have completely understood what they do.

Unfortunately, you can act only on your local repository. You may use some facilities such as "push -f" to rewrite history on your remote repository if you have the right to do so. However, for janitorial operations such as gc, you'll need a direct access to the git server then operate from there.

Upvotes: 1

Related Questions