Script to squash commits in a Git branch or pull-request.
Support this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️
To consolidate the commits in a branch to a single commit for a cleaner Git history.
Examples:
-
Squashing WIP commits in the default branch of a new repository. I personally do this often when starting a new project.
-
Squashing your PR's commits when squash merging is disabled on the repository you're contributing to.
-
Squashing your PR's commits when your PR is deployed through another PR (e.g. in a batched PRs, GitHub cannot squash individual PRs in the batch)
Run the script with npx from the repository branch you want to squash:
npx git-squash-branch
# You must be inside the branch you want to squash
$ git checkout branch-to-squash
$ npx git-squash-branch --base develop --message "feat: my new feature"
Successfully squashed with message:
feat: my new feature
To revert back to the original commit:
git reset --hard 4f0432ffd1
# Force push the squashed branch to remote
$ git push --force
$ git checkout main
$ npx git-squash-branch --base main --message "feat: init"
Current branch is the same as base branch. Squashing all commits to root.
Successfully squashed with message:
feat: init
To revert back to the original commit:
git reset --hard 4f0432ffd1
# Force push the squashed branch to remote
$ git push --force
⚠️ Requires GitHub CLI to be installed
From inside the repository directory, pass in the PR number, URL, or branch:
npx git-squash-branch pr [<number> | <url> | <branch>]
It will squash the PR branch into a single commit and force push it back to the PR branch.
# You must be inside the repository for gh to fetch the PR
$ cd my-repo
$ npx git-squash-branch pr 1234
✔ Successfully squashed PR 1234 with message:
feat: my PR title
To revert the PR back to the original commit:
git push -f origin 4f0432ffd1:pr-branch-name
Note: This command will not update the PR with the latest base branch.
Usage:
git-squash-branch [flags...]
git-squash-branch <command>
Commands:
pr
Flags:
-b, --base <string>
Base branch to compare against. If not specified, will try to
detect it from remote "origin".
-h, --help
Show help
-m, --message <string>
Message for the squash commit (defaults to last commit message)
-r, --remote <string>
Remote to fetch from (default: "origin")
--version
Show version
Basically runs these commands, derived from this StackOverflow answer:
$ git reset --soft $(git merge-base <base-branch> $(git branch --show-current))
$ git commit -m <message>
On top of that, it adds extra features such as:
- Tries to automatically detect the base branch based on the
origin
remote - Defaults to using the commit message from the last commit, if not provided
- Logs the current commit hash in case you want to revert the squash