Git -- How to squash all commits on branch

Ripped from Git: How to squash all commits on branch.

Question

I make new branch from master with:

git checkout -b testbranch

I make 20 commits into it.

Now I want to squash those 20 commits. I do that with:

git rebase -i HEAD~20

What about if I don’t know how many commits?

Answer

Another way to squash all your commits is to reset the index to master:

 git checkout yourBranch
 git reset $(git merge-base master yourBranch)
 git add -A
 git commit -m "one commit on yourBranch"

This isn’t perfect as it implies you know from which branch “yourBranch” is coming from.

Leave a comment