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)
-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:
locsquash -n 3
Squash the last 5 commits with a custom message:
locsquash -n 5 -m "feat: consolidated feature implementation"
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
- Creates a backup branch (
locsquash/backup-<timestamp>) before any changes
- 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