1

My repo has a huge number of commits and I would like to squash everything up until the last five commits - so at the end the repo would have a squash a commit and then my five most recent commits.

Before:

10
9
8
7
6
5
4
3
2
1

After:

10
9
8
7
6
squash commit

I tried git reset --soft at the end and beginning of the squashed portion, but I'm not sure how to keep my last five commits, which go away when I squash the portion before the five.

mango
  • 1,183
  • 6
  • 17
  • 33
  • 1
    This probably isn't a good idea, as it will change the commit hashes for *all* commits, including the ones that don't get squashed. Unless you are the only person working on the repository you can easily run into trouble that way. Do you actually want to squash the commits, or just to truncate your history? – ChrisGPT was on strike Jul 22 '14 at 22:56
  • yep, i'm the only person for now – mango Jul 22 '14 at 22:56
  • I've seen this type of question before. This is a duplicate. Let me find the other one... –  Jul 22 '14 at 23:08
  • @torek ah, that was a good find. –  Jul 22 '14 at 23:14
  • @torek if I were to close as a duplicate, I would want to close as [this one](http://stackoverflow.com/q/435646/456814). Would you consider "Squash everything except for last X commits" questions to be the same as "Squash initial Y commits" questions? –  Jul 22 '14 at 23:44
  • @Cupcake: seems appropriate (all but last L out of N = first F = N - L after all). – torek Jul 22 '14 at 23:47
  • Duplicate of [Combine the first two commits of a Git repository?](http://stackoverflow.com/q/435646/456814). –  Jul 22 '14 at 23:54

1 Answers1

1

Use git rebase -i --root. (If your git is not new enough to have --root in its interactive rebase, it's a bit more painful as you must first create an "orphan" branch.) You'll be presented with an editor session on a file looking like this:

pick b9491ea commit-10
pick d1574b8 commit-9
...
pick ab31c0f commit-1

# Rebase ...
# [commentary with instructions]

Change commits 2 through 5 to "squash", save, exit the editor, and rebase will squash commits 2 through 5 into a copy of commit #1, and then add copies of commits 6 through 10. The resulting repo will have copies of the final commits (and none of the original commits—well, they're still in there, just invisible now, and eventually will be garbage-collected).

(You will get a chance to fix up the squashed-commit's commit text, too.)

torek
  • 448,244
  • 59
  • 642
  • 775