Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
git squash last two commits into one
git rebase --interactive HEAD~2
# we are going to squash c into b
pick b76d157 b
pick a931ac7 c
# squash c into b
pick b76d157 b
s a931ac7 c
# after that just edit the commit message
# This is a combination of 2 commits.
# The first commit's message is:
b
# This is the 2nd commit message:
c
@thepiercingarrow

This comment has been minimized.

Copy link

thepiercingarrow commented Mar 21, 2016

Works great, thanks! 👍

@fregante

This comment has been minimized.

Copy link

fregante commented Jul 8, 2017

Merge the last 3 commits

git reset --soft HEAD~2 # notice this is 2, not 3
git commit --amend

From:

commit 1
commit 2
commit 3

to:

commit 1 # this will include commit 2 and commit 3

Non-interactively

git reset --soft HEAD~2
git commit --amend -C HEAD # this will automatically pick `commit 1` as the commit name

Config

I have this in my git config:

	fixup = "!f(){ git reset --soft HEAD~${1} && git commit --amend -C HEAD; };f"

And I use it as

git fixup 2 # merges the last 2 commits into their parent
@FireCoding

This comment has been minimized.

Copy link

FireCoding commented Sep 7, 2017

Great gist. FYI for convenience you can make the parameter of the fixup git alias optional and set it to a default value of 1 by replacing ${1} with ${1-1}, e.g.:
fixup = "!f(){ git reset --soft HEAD~${1-1} && git commit --amend -C HEAD; };f"

So then you can just do:
git fixup # merges the last commit into its parent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.