Git: How to squash all commits on branch
I make new branch from
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? Is there any way to do something like:
git rebase -i all on this branch
3 Solutions collect form web for “Git: How to squash all commits on branch”
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.
Note: finding that origin branch isn’t easy/possible with Git (the visual way is often the easiest, as seen here).
What you’re doing is pretty error-prone. Just do:
git rebase -i master
which will automatically rebase only your branch’s commits onto the current latest master.
Another simple way to do this: go on the origin branch and do a
merge --squash. This command don’t do the “squased” commit. when you do it, all commit messages of yourBranch will be gathered.
$ git checkout master $ git merge --squash yourBranch $ git commit # all commit messages of yourBranch in one, really useful > [status 5007e77] Squashed commit of the following: ...