Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to completely remove a commit from git history

Tags:

git

I had to delete a the last 5 commits from a local git repo, I did so by reset --hard to the new HEAD commit:

git reset --hard abcde1

Now I need to remove those unreferenced commits from the history, after reading many answers here I tried running the following.

git gc --prune=now

and

git prune --expire=now --agressive

None of the answers I found seem to work for me, for example I can still see a those commits with git show <sha>, what am I missing here? Is possible to remove a commit from history completely?

And finally, if I push to a new origin (let's say it's a completely new repo) are those unreferenced commits also pushed?

like image 539
gonz Avatar asked Jul 19 '26 16:07

gonz


1 Answers

Unreferenced or orphaned commits will not be pushed to the origin. Those commits will be removed by git's garbage collection in sometime. Do this if you want to force the garbage collection

git config gc.reflogexpireUnreachable = now
git gc --prune=now
git config gc.reflogexpireUnreachable = "2 days"
like image 97
usha Avatar answered Jul 22 '26 07:07

usha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!