If all you want to do is squash all of your commits down to the root commit, then while
git rebase --interactive --root
can work, it's impractical for a large number of commits (for example, hundreds of commits), because the rebase operation will probably run very slowly to generate the interactive rebase editor commit list, as well as run the rebase itself.
Here are two quicker and more efficient solutions when you're squashing a large number of commits:
Alternative solution #1: orphan branches
You can simply create a new orphan branch at the tip (i.e. the most recent commit) of your current branch. This orphan branch forms the initial root commit of an entirely new and separate commit history tree, which is effectively equivalent to squashing all of your commits:
git checkout --orphan new-master master
git commit -m "Enter commit message for your new initial commit"
# Overwrite the old master branch reference with the new one
git branch -M new-master master
Documentation: git-checkout(1) Manual Page.
Alternative solution #2: soft reset
Another efficient solution is to simply use a mixed or soft reset to the root commit <root>
:
git branch beforeReset
git reset --soft <root>
git commit --amend
# Verify that the new amended root is no different
# from the previous branch state
git diff beforeReset
Documentation: git-reset(1) Manual Page.