Matías Fidemraizer
Matías Fidemraizer

Reputation: 64933

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

Upvotes: 12

Views: 22429

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520928

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?

Upvotes: 8

Anusha Rao
Anusha Rao

Reputation: 11

I faced the similar issue and figured out the actual cause for it: The flow:

git rebase -i HEAD~10
# Retain the first commit from below( as pick) and change the rest of the `pick` to `squash`
# After your rebase is successful
git log
# You can see all your commits squashes to one commit

Then now when you git pull from your remote branch, it will pull the rest of the commits which is not there in local ( basically all the commits you had squashed previously, since it is now present in one commit) and hence you are seeing the previous commits as well.

Better way is to git push -f to your remote branch, if you are confident that you have no new changes added there.

P.S: If you have any new changes in the remote branch, better to:

git rebase origin remote_branch 

and then squash your commits.

Upvotes: 1

VonC
VonC

Reputation: 1323463

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"

Upvotes: 4

Related Questions