How to squash all git commits into one?

GitRebaseGit RebaseSquashGit Rewrite-History

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

As of git 1.6.2, you can use git rebase --root -i.

For each commit except the first, change pick to squash.

Solution 2 - Git

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
  1. 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.

  1. 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 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.

Solution 3 - Git

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:

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:

Solution 4 - Git

Perhaps the easiest way is to just create a new repository with current state of the working copy. If you want to keep all the commit messages you could first do git log > original.log and then edit that for your initial commit message in the new repository:

rm -rf .git
git init
git add .
git commit

or

git log > original.log
# edit original.log as desired
rm -rf .git
git init
git add .
git commit -F original.log

Solution 5 - Git

echo "message" | git commit-tree HEAD^{tree}

This will create an orphaned commit with the tree of HEAD, and output its name (SHA-1) on stdout. Then just reset your branch there.

git reset SHA-1

To do the above in a single step:

git reset $(git commit-tree HEAD^{tree} -m "Initial commit.")

Solution 6 - Git

Here's how I ended up doing this, just in case it works for someone else:

Remember that there's always risk in doing things like this, and its never a bad idea to create a save branch before starting.

Start by logging

git log --oneline

Scroll to first commit, copy SHA

git reset --soft <#sha#>

Replace <#sha#> w/ the SHA copied from the log

git status

Make sure everything's green, otherwise run git add -A

git commit --amend

Amend all current changes to current first commit

Now force push this branch and it will overwrite what's there.

Solution 7 - Git

I read something about using grafts but never investigated it much.

Anyway, you can squash those last 2 commits manually with something like this:

git reset HEAD~1
git add -A
git commit --amend

Solution 8 - Git

The easiest way is to use the 'plumbing' command update-ref to delete the current branch.

You can't use git branch -D as it has a safety valve to stop you deleting the current branch.

This puts you back into the 'initial commit' state where you can start with a fresh initial commit.

git update-ref -d refs/heads/master
git commit -m "New initial commit"

Solution 9 - Git

In one line of 6 words:

git checkout --orphan new_root_branch  &&  git commit

Solution 10 - Git

First, squash all your commits into a single commit using git rebase --interactive. Now you're left with two commits to squash. To do so, read any of

Solution 11 - Git

create a backup

git branch backup

reset to specified commit

git reset --soft <#root>

then add all files to staging

git add .

commit without updating the message

git commit --amend --no-edit

push new branch with squashed commits to repo

git push -f

Solution 12 - Git

To do this, you can reset you local git repository to the first commit hashtag, so all your changes after that commit will be unstaged, then you can commit with --amend option.

git reset your-first-commit-hashtag
git add .
git commit --amend

And then edit the first commit nam if needed and save file.

Solution 13 - Git

To squash using grafts

Add a file .git/info/grafts, put there the commit hash you want to become your root

git log will now start from that commit

To make it 'real' run git filter-branch

Solution 14 - Git

I usually do it like this:

  • Make sure everything is committed, and write down the latest commit id in case something goes wrong, or create a separate branch as the backup

  • Run git reset --soft `git rev-list --max-parents=0 --abbrev-commit HEAD` to reset your head to the first commit, but leave your index unchanged. All changes since the first commit will now appear ready to be committed.

  • Run git commit --amend -m "initial commit" to amend your commit to the first commit and change the commit message, or if you want to keep the existing commit message, you can run git commit --amend --no-edit

  • Run git push -f to force push your changes

Solution 15 - Git

For me it worked like this: I had 4 commits in total, and used interactive rebase:

git rebase -i HEAD~3

The very first commit remains and i took 3 latest commits.

In case you're stuck in editor which appears next, you see smth like:

pick fda59df commit 1
pick x536897 commit 2
pick c01a668 commit 3

You have to take first commit and squash others onto it. What you should have is:

pick fda59df commit 1
squash x536897 commit 2
squash c01a668 commit 3

For that use INSERT key to change 'insert' and 'edit' mode.

To save and exit the editor use :wq. If your cursor is between those commit lines or somewhere else push ESC and try again.

As a result i had two commits: the very first which remained and the second with message "This is a combination of 3 commits.".

Check for details here: https://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit

Solution 16 - Git

Let's say you have 3 commits in your branch and it is already pushed to remote branch.

Example:

git log -4

Will display results like:

<your_third_commit_sha>
<your_second_commit_sha>
<your_first_commit_sha>
<master_branch_commit_sha - your branch created from master>

You want to squash your last 3 commits to one commit and push to remote branch. Here are the steps.

git reset --soft <master_branch_commit_sha>

Now all commits changes are integrated but uncommitted. Verify by:

git status

Commit all changes with a message:

git commit -m 'specify details'

Forcefully push the single commit to remote branch:

git push -f

Solution 17 - Git

This answer improves on a couple above (please vote them up), assuming that in addition to creating the one commit (no-parents no-history), you also want to retain all of the commit-data of that commit:

  • Author (name and email)
  • Authored date
  • Commiter (name and email)
  • Committed date
  • Commmit log message

Of course the commit-SHA of the new/single commit will change, because it represents a new (non-)history, becoming a parentless/root-commit.

This can be done by reading git log and setting some variables for git commit-tree. Assuming that you want to create a single commit from master in a new branch one-commit, retaining the commit-data above:

git checkout -b one-commit master ## create new branch to reset
git reset --hard \
$(eval "$(git log master -n1 --format='\
COMMIT_MESSAGE="%B" \
GIT_AUTHOR_NAME="%an" \
GIT_AUTHOR_EMAIL="%ae" \
GIT_AUTHOR_DATE="%ad" \
GIT_COMMITTER_NAME="%cn" \
GIT_COMMITTER_EMAIL="%ce" \
GIT_COMMITTER_DATE="%cd"')" 'git commit-tree master^{tree} <<COMMITMESSAGE
$COMMIT_MESSAGE
COMMITMESSAGE
')

Solution 18 - Git

This worked best for me.

git rebase -X ours -i master

This will git will prefer your feature branch to master; avoiding the arduous merge edits. Your branch needs to be up to date with master.

ours
           This resolves any number of heads, but the resulting tree of the merge is always that of the current
           branch head, effectively ignoring all changes from all other branches. It is meant to be used to
           supersede old development history of side branches. Note that this is different from the -Xours
           option to the recursive merge strategy.

Solution 19 - Git

I usually squash the entire tree when I restore a template from a git repository, to get a cleaner history and to ensure legal compliance. My workflow looks like this:

git clone https://git.invalid/my-awesome-template.git my-awesome-foo
cd !$
git branch -M master my-awesome-template/master
git checkout --orphan master
git rm -rf /
git commit --allow-empty --allow-empty-message -m 'Initial commit'
git merge --squash --allow-unrelated-histories my-awesome-template/master
git commit
git branch -D my-awesome-template/master
# you can now `drop' that "Initial commit":
git rebase -i --root

This squashes your entire history into a single large commit message.

In this particular example:

  • master is the working branch
  • my-awesome-template/master is an intermediate branch

Solution 20 - Git

Since I had some trouble with the solutions proposed here, I want to share a really simple solution (to squash all commits on a feature branch into one):

git merge origin/master && git reset --soft origin/master

The preceding merge cmd ensures, that no recent changes from master will go on your head when committing! After that, just commit the changes and do git push -f

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVerhogenView Question on Stackoverflow
Solution 1 - GitJordan LewisView Answer on Stackoverflow
Solution 2 - GitryenusView Answer on Stackoverflow
Solution 3 - Gituser456814View Answer on Stackoverflow
Solution 4 - GitPat NotzView Answer on Stackoverflow
Solution 5 - GitkusmaView Answer on Stackoverflow
Solution 6 - GitLoganView Answer on Stackoverflow
Solution 7 - GitR. Martinho FernandesView Answer on Stackoverflow
Solution 8 - GitCB BaileyView Answer on Stackoverflow
Solution 9 - GitkybView Answer on Stackoverflow
Solution 10 - GitFrerich RaabeView Answer on Stackoverflow
Solution 11 - GitDavidView Answer on Stackoverflow
Solution 12 - GitT.EL MOUSSAOUIView Answer on Stackoverflow
Solution 13 - GitdimusView Answer on Stackoverflow
Solution 14 - GitKent Munthe CaspersenView Answer on Stackoverflow
Solution 15 - GitVladislav AshikhinView Answer on Stackoverflow
Solution 16 - GitPrabhu PView Answer on Stackoverflow
Solution 17 - GitjavabrettView Answer on Stackoverflow
Solution 18 - GitDerrick PetzoldView Answer on Stackoverflow
Solution 19 - GitremView Answer on Stackoverflow
Solution 20 - GitseebiView Answer on Stackoverflow