locsquash
A CLI tool to squash the last commits in your Git repository into a single commit.
Installation
go install github.com/OutOfStack/locsquash@latest
Or build from source:
git clone https://github.com/OutOfStack/locsquash.git
cd locsquash
make build # version = dev
make build VERSION=v1.0.0 # version = v1.0.0
Usage
locsquash -n <count> [options]
Required
-n <count> - Number of commits to squash (must be at least 2)
Options
-m <msg> - Custom commit message for the squashed commit (defaults to the oldest commit's message)
-y, -yes - Skip confirmation prompt (useful for scripting)
-no-backup - Skip creating backup branch
-stash - Auto-stash uncommitted changes before squashing
-allow-empty - Allow creating an empty commit if squashed changes cancel out
-dry-run - Preview the git commands without executing them
-print-recovery - Print recovery commands and exit
-v, -version - Print version and exit
Examples
Squash the last 3 commits (will show commits and ask for confirmation):
locsquash -n 3
Squash the last 5 commits with a custom message:
locsquash -n 5 -m "feat: consolidated feature implementation"
Squash without confirmation prompt (for scripting):
locsquash -n 3 -y
Squash without creating a backup branch:
locsquash -n 3 -y -no-backup
Preview what would happen without making changes:
locsquash -n 3 -dry-run
Squash with uncommitted changes (auto-stash):
locsquash -n 3 -stash
How It Works
- Shows the commits that will be squashed and asks for confirmation (skip with
-y)
- Creates a backup branch (
locsquash/backup-<timestamp>) before any changes (skip with -no-backup)
- Optionally stashes uncommitted changes if
-stash is provided
- Performs a soft reset to
HEAD~N
- Creates a new commit with all changes, preserving the most recent commit's date and using the oldest commit message (unless
-m is provided)
- Restores stashed changes if applicable
Development
make build # Build binary to bin/
make build VERSION=v1.0.0 # Build with specific version
make run # Run without building
make test # Run tests with race detector
make test-docker # Run tests in Docker
make lint # Run linter
Releasing
To create a new release:
git tag v1.0.0
git push --tags
This triggers CI to build binaries for all platforms (Linux, macOS, Windows) and create a GitHub Release with that version.
Recovery
If something goes wrong, recover using the backup branch:
git reset --hard locsquash/backup-<timestamp>
To see recovery instructions before running:
locsquash -n 3 -print-recovery
If you used -no-backup, recovery is only possible via git reflog:
git reflog
git reset --hard <commit-hash-before-squash>