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?
Squashing a commit In the list of branches, select the branch that has the commits that you want to squash. Click History. Select the commits to squash and drop them on the commit you want to combine them with. You can select one commit or select multiple commits using Command or Shift .
Another simple way to do this: go on the origin branch and do a merge --squash . This command doesn't do the "squashed" commit. when you do it, all commit messages of yourBranch will be gathered.
As of git 1.6.2, you can use git rebase --root -i
.
For each commit except the first, change pick
to squash
.
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'
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.
No need to squash, use git commit-tree
to create an orphan commit and go with it.
create a single commit via git commit-tree
What 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.
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.
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 https://github.com/toolbear/panda.git 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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With