How to Squash All Git Commits into One?
How Do You Squash Your Entire Repository Down to the First Commit? I Can Rebase to the First Commit, but That Would Leave Me with 2 Commits. Is There a Way to...
How do you squash your entire repository down to the first commit?
I can rebase to the first commit, but that would leave me with 2 commits. Is there a way to reference the commit before the first one?
20 Answers
As of git 1.6.2, you can use git rebase --root -i.
For each commit except the first, change pick to squash.
Must Read
Update
I've made an alias git squash-all.
Example usage: git squash-all "a brand new start".
[alias]
squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} -m \"${1:-A new start}\");};f"
Note: the optional message is for commit message, if omitted, it will default to "A new start".
Or you can create the alias with the following command:
git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} -m "${1:-A new start}");};f'
One Liner
git reset $(git commit-tree HEAD^{tree} -m "A new start")
Here, the commit message "A new start" is just an example, feel free to use your own language.
TL;DR
No need to squash, use git commit-tree to create an orphan commit and go with it.
Explain
create a single commit via
git commit-treeWhat
git commit-tree HEAD^{tree} -m "A new start"does is:
Creates a new commit object based on the provided tree object and emits the new commit object id on stdout. The log message is read from the standard input, unless -m or -F options are given.
The expression HEAD^{tree} means the tree object corresponding to HEAD, namely the tip of your current branch. see Tree-Objects and Commit-Objects.
- reset the current branch to the new commit
Then git reset simply reset the current branch to the newly created
commit object.
This way, nothing in the workspace is touched, nor there's need for rebase/squash, which makes it really fast. And the time needed is irrelevant to the repository size or history depth.
Variation: New Repo from a Project Template
This is useful to create the "initial commit" in a new project using another repository as the template/archetype/seed/skeleton. For example:
cd my-new-project
git init
git fetch --depth=1 -n
git reset --hard $(git commit-tree FETCH_HEAD^{tree} -m "initial commit")
This avoids adding the template repo as a remote (origin or otherwise) and collapses the template repo's history into your initial commit.
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: