3

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?

gonz
  • 5,226
  • 5
  • 39
  • 54

3 Answers3

4

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"
usha
  • 28,973
  • 5
  • 72
  • 93
0

Check out gitscm It has good resources on exactly how to fully remove resources. However, I think your question about pushing to a new origin leads to a simpler solution.

Objects that are not referenced in a commit in a branch you are pushing are not sent to the server. I would suggest cloning a the repo into a clean directory, pulling from your existing and then pushing to whatever destination you wanted. It will give you a clean .git directory with just the referenced objects.

jeremy
  • 4,294
  • 3
  • 22
  • 36
0

Do you want to actually "remove" from history? you can do that, but if somebody already pull those changes then they are going to have a funny time with their history.

so, what you might want to do is

git reset --hard <sha1-commit-id>//the commit you want to get rid of
git push origin HEAD --force

Before doing this, make a backup! Again, if somebody already pulled your changes then be prepared to listen to horror stories.

Guillermo Mansilla
  • 3,779
  • 2
  • 29
  • 34
  • Hey Guillermo, yes I want to modify the repo history, I'm aware of the implications of what I'm trying to do. Thanks for your answer! – gonz Nov 11 '13 at 20:13