LinuxCommandLibrary

git-squash

Combine multiple Git commits into a single commit

TLDR

Merge all commits from a specific branch into the current branch as a single commit

$ git squash [source_branch]
copy

Squash all commits starting with a specific commit on the current branch
$ git squash [commit]
copy

Squash the n latest commits and commit with a message
$ git squash HEAD~[n] "[message]"
copy

Squash the n latest commits and commit concatenating all individual messages
$ git squash --squash-msg HEAD~[n]
copy

SYNOPSIS

git squash [-k|-kk] [number] [message]

PARAMETERS

-k, --keep-first
    Keep first commit message (default)

-kk, --keep-all
    Keep all commit messages in new message

number
    Commits to squash (default: 2)

message
    Custom commit message (overrides defaults)

DESCRIPTION

The git squash command, from the git-extras extension, simplifies combining the last N commits into a single commit. It performs an interactive rebase, preserving changes while allowing a custom message. Ideal for cleaning up local history before pushing, such as fixing minor commits or typos. By default, it squashes 2 commits using the first commit's message. Use -k to keep the first message or -kk to append all messages. It updates the current branch and requires a clean working directory. Not part of core Git; install git-extras first.

CAVEATS

Requires git-extras installed.
Rewrites history; unsafe for public/shared branches.
Clean working directory needed.

INSTALLATION

Linux: sudo apt install git-extras or yum install git-extras.
macOS: brew install git-extras.

EXAMPLE

git squash 3 (squash last 3).
git squash 3 "Fixed typos and updates".

HISTORY

Part of git-extras (2012), a GitHub-hosted toolkit by Linus Ægosth et al., enhancing Git workflow.

SEE ALSO

Copied to clipboard