Git Squash and remove previous commits

Matías Fidemraizer picture Matías Fidemraizer · Jul 26, 2015 · Viewed 10.4k times · Source

Maybe I misunderstood how GIT works.

I've run git rebase -i HEAD~10 and I could squash 10 commits into one. The issue is that all squashed commits are still there, and I thought they were going to be dropped after merging them all into one.

Is this the expected result? If so, can I rewrite the history to remove useless commits (since these changes are already in the commit which all previous commits were squashed)?

Answer

Tim Biegeleisen picture Tim Biegeleisen · Jul 26, 2015

When you began your interactive rebase session, you should have been prompted with a list of the last 10 commits from the current branch:

git rebase -i HEAD~10

pick as2i8dw first commit
pick ee361eb second  commit
...
pick b2762sx most recent commit

You need to change this file to the following:

pick as2i8dw first commit
squash ee361eb second commit
...
squash b2762sx most recent commit

Then you need to do a git commit to save the changes. Now when doing a git log you should only see the as2i8dw commit and none of the other ten.

That being said, is this what you did?