A git feature commit squashing approach
03 May 2021
This is a trick that Ramon Snir taught me to transform a git-tracked feature branch with hundreds of commits (which I’ve already pushed to a GitHub-hosted remote) into looking like it never had anything but a single commit with a message of “Meaningful fake first commit message.”
git checkout the-feature-branch && git reset --hard origin/the-feature-branch
- This resets your local branch to what’s currently on GitHub
git merge origin/main && git reset --soft origin/main
- This makes your Git staging area include your branch’s content, but your Git base commit be
main
- Note that this does not pull in actual changes from the remote
main
that have happened since you started working on the feature branch.
- This makes your Git staging area include your branch’s content, but your Git base commit be
git commit -m "Meaningful fake first commit message"
git log ^origin/main HEAD
to verifygit show HEAD
to see that you’re pleased with the resultgit push --force
only if you’re happy with everything from the previous steps- This should be the last operation because it’s more-or-less irreversible
If you’re on Windows, your ordinary command line interface (CLI) won’t recognize the &&
s in these commands, so instead use the Git Bash CLI tool that came with this version of Git you likely installed, because it uses a mini-Unix-like CLI called MinGW.