Git - squash entire branch - one line squash command
My preferred method is a two-liner (excluding steps 1 and 4 below). The benefits are you do not need to know/record any commit IDs, you can write a simple alias to perform all the steps involved, and your actually moving your entire branch onto origin/master so that the actual merge into master can be a fast-forward and there cannot be any conflicts.
First, my assumptions:
- You're working on a branch called
my-feature-branch
. This branch has diverged frommaster
by several commits; this is the checked-out branch. - Your local
master
tracks remote branchorigin/master
- You want to squash all of your commits from
my-feature-branch
into a single commit ontop of the current state of origin/master (not your localmaster
, which may be out of date) - All of your changes are committed, you have no unstaged changes (they will be lost during
git reset --hard
)
My process is as follows:
Fetch, so
origin/master
is current:$ git fetch
Throw away all the commits on your local branch by resetting it to point at
origin/master
$ git reset --mixed origin/master
Merge all of your old changes from the previous state of your branch into the index
$ git merge --squash HEAD@{1}
Commit your changes - Git will pre-populate your editor with a commit message containing all the commit messages from the squashed commits
The simple alias I mentioned would be:
alias squash="git fetch; git reset --mixed origin/master; git merge --squash HEAD@{1}"
This is a perfect use case for git reset --soft
.
Assume you have a commit history
D Your latest patchC Your second patchB Your first patchA Someone else's work
you have no staged changes, and git status
, git log
or git show
tell you are currently at commit D.
Then git reset --soft B
will take the cumulative changes of commits C
and D
and stage them for commit. git commit --amend
will then 'merge' these changes into commit B.
Use as follows:
git reset --soft Bgit commit --amend
The second command will bring up your editor with a chance to edit the commit message.
Note that if you have staged changes before starting this process (i.e. you have done git add XXX
but not followed up with a git commit
) then those staged changes will also be merged into the commit.
Probably the best option for this would be to use git merge --squash
at merge time. That will leave your branch as it developed, which is quite often a lot easier to troubleshoot with, because you'll have some notion of "I was changing that specific functionality in commit Z", and looking at that specific commit, you have all the context of any changes you made to multiple files - looking at a single commit that is the squashed results of your development path makes it quite a bit harder to remember "Oh, yeah, I had to change this one other thing in a different file, too...". You also have the benefit of using git bisect
when you have your entire path available - all it could tell you in the squashed case is "this huge commit here broke something".
The result of using git merge --squash
is a single commit on the branch that you are "merging" into that contains the cumulative changes from your branch, but it leaves your original branch alone.