July 16, 2023

Git: Squash (combine, merge) git commits together

To squash (that is to combine commits in git terminology) the last two commits, run:

$ git rebase -i HEAD~2

The text editor should display:

pick 853c982 Penultimate commit
pick 91ba811 Last commit you want to squash with the previous one

# Rebase 4d216c4..91ba811 onto 4d216c4 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Note that commits are listed in reversed order; the last one is at the bottom.

To squash the last commit into the penultimate one, replace pick by squash like that:

pick 853c982 Penultimate commit
squash 91ba811 Last commit you want to squash with the previous one

# Rebase 4d216c4..91ba811 onto 4d216c4 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Save and close the text editor.

The text editor should display this message:

# This is a combination of 2 commits.
# This is the 1st commit message:

Penultimate commit

# This is the commit message #2:

Last commit you want to squash with the previous one

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Jul 15 16:06:54 2023 +0200
#
# interactive rebase in progress; onto 4d216c4
# Last commands done (2 commands done):
#    pick 853c982 Penultimate commit
#    squash 91ba811 Last commit you want to squash with the previous one
# No commands remaining.
# You are currently rebasing branch 'refactor-chapter-five' on '4d216c4'.
#
# Changes to be committed:
#   modified:   plan.md
#

Save and close the text editor.

Check that the last two commits were squashed by running:

$ git log --pretty=oneline -n 2

To learn more about the rebase command run:

$ man git-rebase

git personal computing command-line interface (cli) gnu linux trisquel office applications wiki shell literacy

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88