1

Possible Duplicate:
How to delete a ‘git commit’

Knowing that this will change history, I want to remove some accidentally commits from the history of a repository. I would like all other commits to retain their states of the repository. So the changes of commits I want to delete would be covered each by the commit after them.

How can I do that in Git and how can I apply that also or only to the repository on Github?

Community
  • 1
  • 1
danijar
  • 32,406
  • 45
  • 166
  • 297

1 Answers1

5

If I understand correctly, "squashing" is what you want.

  -A-B-C-D-E-F-G-H

Given the history above, you should pick the commit before the first commit you want to squash. Let's say you want to squash F and E into D and H into G, so the commit before the first one is C.

  git rebase -i C

You will be presented an editor with a text file containing something like the following:

pick D
pick E
pick F
pick G
pick H

Modify this into:

pick D
squash E
squash F
pick G
squash H

You will be asked to review the commit messages of the new commits,

The resulting history will look like:

  -A-B-C-D'-G'

Please note You're always squashing newer commits into older ones, not vice versa. Therefore pick G and squash H. Think of it as amending a commit after your history moved on.

PS: To get this into your GitHub repository, you'll have to to a forced push git push -f origin (given that origin is your GitHub remote).

PPS: For further information see the man page of git-rebase.

orad
  • 15,272
  • 23
  • 77
  • 113
Koraktor
  • 41,357
  • 10
  • 69
  • 99
  • Then my history will become `A-B-C-D-G`? – danijar Dec 30 '12 at 00:42
  • It's `-A-B-C-D'-G'`, I edited my answer. But `D'=D+E+F` and `G'=G+H`. – Koraktor Dec 30 '12 at 00:44
  • Are later commits affected? For example if I start with `A-B-C-D` and just squash `C` into `B`. Will the result be `A-B'-D` or `A-B'-D'`? – danijar Dec 30 '12 at 09:24
  • The latter. Rebasing will always rewrite the whole history after the first changed commit, even if you only change the commit message. – Koraktor Dec 30 '12 at 09:30
  • This is sad but I understand that. And isn't there a way to squash a commit into the *next* one? That makes a difference to me since the author of these two commits differ. – danijar Dec 30 '12 at 09:34
  • Effectively you can, but git-rebase walks your history from old to new. So the old commit is first and the new one is squashed into it. The patches of both commits are combined and you're able to select which commit message to use (or even write a new one). So the result makes no differences which commit is squashed into which other commit. – Koraktor Dec 30 '12 at 09:42