Reputation: 376
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
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:
git apply
).Note that there's also the --squash
command-line option of git merge
.
This one squashes several commits at once for real.
Upvotes: 2
Reputation: 142214
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.
Upvotes: 0
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
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