Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hard delete an orphan commit in git?

Tags:

git

I have local commit that are not on any branch that I would like to delete. I don't want to rebase them, I really want to delete them, and loose all the content related to these commit.

Is their a command to do so ?

So far I've tried interactive rebase as many suggested, but it just move commit around, it doesn't delete them. I've also tried to use reflog delete, but I can't figure out how to pass a specific commit Id to the command.

Here's the working tree:

o [master] Commit #6   |   o Commit #5   |   | o Commit #4   |/   o Commit #3   |   o Commit #2   |   o Commit #1   

I want to physically delete the commit #4.

like image 891
FMaz008 Avatar asked Oct 26 '11 18:10

FMaz008


People also ask

How do you force delete git commit?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

How do I delete an orphan branch in git?

Use git prune to remove orphaned/unused branches If you see any branches in there that you don't want, you can use the command git branch -d <branch name> . If you want to delete the branch called badbranch, use the -D switch to force the deletion if it doesn't work: git branch -d badbranch .

How do I forget a commit?

The easiest way to undo the last Git commit is to execute the “git reset” command with the “–soft” option that will preserve changes done to your files. You have to specify the commit to undo which is “HEAD~1” in this case. The last commit will be removed from your Git history.

What is an orphaned commit?

Your commits aren't gone (yet) When you use --force, you are adding new commits to Git and updating the branch reference of your branch. You have only orphaned the existing previous line of work as it is no longer referenced by anything.


1 Answers

If the commit is not referenced by anything, it will be removed with git's garbage collection, in time.

If you want to force it before hand, use git gc --prune=now --aggressive

like image 172
Andy Avatar answered Oct 08 '22 09:10

Andy