1

I would like to permanently remove specified commits from the entire history of a git repository. I saw this thread, in which the following is recommended:

git reset --hard HEAD~4
git push origin HEAD --force

That's fine and resets the status of my repository, but in no way does it remove my commits. I would like to literally roll back history and discard changes to the repo since 4 revisions ago.

Could anyone help me with this? When I try the above and look at the revision history in Github, I still see unwanted commits sitting there.

I saw this article, but I wanted to check whether or not there were other options before investigating this solution.

Community
  • 1
  • 1
jml
  • 1,745
  • 6
  • 29
  • 55
  • You write: *in no way does it remove my commits*. The commits you're trying to get rid of may be part of other references' ancestry. – jub0bs Sep 26 '14 at 02:06
  • I should have been clear - I'm only dealing with the master branch, and no other author was involved in the commits. – jml Sep 26 '14 at 02:07
  • Ok, but in what sense does that command *in no way remove [your] commits*? Can you still see them in the output of `git log`? In your remote repo? Or do you mean something else? – jub0bs Sep 26 '14 at 02:08
  • I see them on Github – jml Sep 26 '14 at 02:10
  • 1
    Have you tried (be sure that's what you want, though) `git push --force origin master`? – jub0bs Sep 26 '14 at 02:11
  • PERFECT - please post this as an answer for me - this is precisely what I needed. – jml Sep 26 '14 at 02:14
  • This does the exact same thing as the command you posted... – remram Sep 26 '14 at 02:50
  • @remram Only if `HEAD` is indeed pointing at `master`. – jub0bs Sep 26 '14 at 08:31
  • If it is not pointing at master, then the previous reset is very wrong and you lost data on another branch... – remram Sep 26 '14 at 15:05

2 Answers2

1

From your question, what actually happens when you run

git push origin HEAD --force

is unclear, but I can think of at least two reasons why it may fail to force-push master to origin:

  1. the currently checked out branch is not master and is a branch already up-to-date on origin, or
  2. you have a detached HEAD.

Make sure master is indeed the currently checked-ou branch, by running git checkout master; then run your force push command. Alternatively, specify master explicitly in the git push command:

git push --force origin master

That should do it.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
1

If the unwanted commits are not referenced anymore by a branch or a tag (i.e. called "loose objects"), then they will be automatically removed by the garbage collector. By default, the GC acts on objects older than 90 days.

You can still manually trigger an earlier GC with:

git reflog expire --expire=now --all
git gc --prune=all

However, that will act only on your local repository, not on GitHub. GitHub runs a weekly GC with its own options so if you want to remove the commits now, then you will have to delete the repository, recreate it and push your local after GC cleanup.

Julien Carsique
  • 3,915
  • 3
  • 22
  • 28