Git Tips Slides
PageUp x2: previous · PageDown x2: next · Home: index

Squash all commits into a single commit

Unfortunately, in Kubernetes-related projects, squash via GitHub as described in Squash PRs is disabled.

See PR Guidelines.

git rebase -i HEAD~N works fine, except when you merged the main branch into your branch after creating the branch. Then your branch will contain merge commits, and the normal procedure won't work.

You can use git reset --soft and then create a new commit that contains all the changes between origin/main and your-pr-branch.

git fetch
git switch your-pr-branch

# create a backup, just in case something goes wrong
git switch -c your-pr-branch-backup

# switch back to your-pr-branch
git switch -

# If you want to copy commit messages from your branch,
# then copy them now. After the following command, you need
# to look into your backup branch.
git reset --soft origin/main
git commit
git push --force-with-lease