DEV Community

Katie
Katie

Posted on • Originally published at katiekodes.com on

A git feature commit squashing approach

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."

  1. git checkout the-feature-branch && git reset --hard origin/the-feature-branch
    • This resets your local branch to what's currently on GitHub
  2. 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.
  3. git commit -m "Meaningful fake first commit message"
  4. git log ^origin/main HEAD to verify
  5. git show HEAD to see that you're pleased with the result
  6. git 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.

Top comments (0)