gcb
gcb

Reputation: 376

Can I only squash 2 commits at a time?

When I try to interactively rebase (squash) several commits into one, it only ever squashes the last two commits so I end up having to do the squash several times.

I am using source tree, but I have tried doing it from the console with the same results. Any ideas?

Upvotes: 1

Views: 771

Answers (4)

kostix
kostix

Reputation: 55483

(Interactive) rebasing technically squashes only two commits at a time—simply because the so-called "rebase script" you're typically asked to edit before git rebase proceeds applies an action to a single commit at a time.

This does not, however, change the outcome because logically there's no difference between squashing N commits at once or doing this commit-by-commit. In your rebase script, you just pick the action "squash" (also read up on "fixup"—it's often may be more handy than "squash") for a series of adjacent commits, and then git rebase works like this:

  1. Get the commit marked "squash". Squash it to the previous commit—effectively re-writiing that with the new content applied to it (as with git apply).
  2. Given that new state, go to step 1. ;-)

Note that there's also the --squash command-line option of git merge. This one squashes several commits at once for real.

Upvotes: 2

CodeWizard
CodeWizard

Reputation: 142214

Squash

This is exactly what interactive rebase is for.

You use the git rebase -i @~x where x stand for the number of commits you wish to squash.

Then a dialog will open up for you and you can choose commits and what you wish to do with each commit.

enter image description here

Upvotes: 0

shakhawat
shakhawat

Reputation: 2727

Try -

$ git rebase -i HEAD~4

then

pick 01d1124 commit msg 1
squash 6340aaa commit msg 2
squash ebfd367 commit msg 3
squash 30e0ccb commit msg 4

save and quit your editor

You can do the same in Sourcetree -

http://kwnetzwelt.net/wordpress/archives/2440

Upvotes: 7

Martin G
Martin G

Reputation: 18109

Given that your branch points to the last of the commits you want to squash you can use this trick:

git reset --soft <some older commit>
git commit --amend

You have now replaced <some older commit> with a new one containing everything between the two, plus their content.

Upvotes: 1

Related Questions