4

I was configuring git and during this phase created a few test commits (I was commiting a change of 1 character with pointless commit message). After these I was using git and created a lot of normal commits. So right now when I review commit history with git rev-list --all --pretty and scroll far enough I see these terrible first N commits.

I was trying to get rid of them and I found suggestions to redo git (which is not a valid idea for me). I also found that the first commit is special so I can not remove it.

But is there a way to remove first N commits (excluding the very first one) and rename the first one?

Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753

1 Answers1

4

If you have not "published" your commits (made them available to others), you can use the history rewriting effect of git rebase to discard the early commits.

This rewriting works by copying all the commits you want to retain, to new commits. The old ones are then abandoned. If you have published the commits, any other people who grabbed them already have them and you cannot do anything about that; and making these new copies of the ones you want to keep, and asking them to switch over to the new copies, makes a lot of useless work for them. So it's not polite and you should not do it without a really good reason.

If it's a private repository, though, you're all good.

To be able to rewrite every commit starting from the beginning, use git rebase -i --root. This will give you a list of every commit, including the first one, with pick commands for each; simply delete the ones you want to drop. (Note that this will "flatten out" any merges. It's possible to preserve them, even while removing some commits, but I have not experimented with this; see the BUGS section of the git rebase documentation.)

torek
  • 448,244
  • 59
  • 642
  • 775