Skip to content

Instantly share code, notes, and snippets.

@technolo-g
Last active August 29, 2015 14:08
Show Gist options
  • Save technolo-g/e59fb3fb71c1c0a1534c to your computer and use it in GitHub Desktop.
Save technolo-g/e59fb3fb71c1c0a1534c to your computer and use it in GitHub Desktop.
Squash commits in a branch

This method is harder than using the get reset method above. Also it doesn't work well if you merged the master into the feature branch previously (you'll need to resolve all conflicts again).

What we are describing here will destroy commit history and can go wrong. For this reason, do the squashing on a separate branch:

git checkout -b squashed_feature

To squash all commits since you branched away from master, do

git rebase -i master

Note that rebasing to the master does not work if you merged the master into your feature branch while you were working on the new feature. If you did this you will need to find the original branch point and call git rebase with a SHA1 revision.

Your editor will open with a file like:

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

Each line represents a commit (in chronological order, the latest commit will be at the bottom). To transform all these commits into a single one, change the file to this:

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

This means, you take the first commit, and squash the following onto it. If you remove a line, the corresponding commit is actually really lost. Don't bother changing the commit messages because they are ignored. After saving the squash settings, your editor will open once more to ask for a commit message for the squashed commit.

You can now merge your feature as a single commit into the master:

git checkout master
git merge squashed_feature

Source: http://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment