Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Squash and remove previous commits

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)?

like image 944
Matías Fidemraizer Avatar asked Jul 26 '15 10:07

Matías Fidemraizer


2 Answers

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?

like image 104
Tim Biegeleisen Avatar answered Oct 02 '22 11:10

Tim Biegeleisen


The issue is that all squashed commits are still there

If those commits are still accessible by any other reference (other branch or tag), there would still be visible, even after the current branch is rebased.

Try instead squashing the commits with a git reset --soft.
If HEAD does still reference your 10 commits:

git reset --soft HEAD~10
git commit -m "squashed 10 commits"
like image 43
VonC Avatar answered Oct 02 '22 13:10

VonC