Skip to content

Instantly share code, notes, and snippets.

@silvioramalho
Created April 17, 2025 18:13
Show Gist options
  • Save silvioramalho/ee94fcd0fedb067b30175592b2f1a07e to your computer and use it in GitHub Desktop.
Save silvioramalho/ee94fcd0fedb067b30175592b2f1a07e to your computer and use it in GitHub Desktop.
Squash multiple commits into a single signed commit

Squash and Sign Commits in Git

This guide shows how to squash multiple commits into a single signed commit. It's useful when you've made several WIP commits and want to clean up your branch history before pushing or creating a pull request.

Prerequisites

  • A feature branch created from main (e.g., feature/add-auth)
  • At least two commits to be squashed
  • Git commit signing (GPG/SSH/S/MIME) is already configured

Step-by-step

1. Checkout the branch

git checkout feature/add-auth

2. Start an interactive rebase of the last 2 commits

git rebase -i HEAD~2

The editor will open showing something like:

pick abc123 WIP
pick def456 WIP 2

Change it to:

pick abc123 WIP
squash def456 WIP 2

Save and close the editor.


3. Edit the final commit message

Git will open another editor with both commit messages. Edit and keep only what you want, for example:

feat: implement feature/add-auth feature

Save and close the editor again.


4. Sign the final commit using amend

git commit --amend -S

This will reopen the editor with the commit message. Just save and close it to apply the signature.


5. Force push the branch to GitHub

git push --force

Result

The branch feature/add-auth will have a single, signed commit with your final message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment