Skip to content

Instantly share code, notes, and snippets.

@ejke
Last active January 10, 2022 09:13
Show Gist options
  • Save ejke/cc14fde7ae0368ba19f2e28af0593405 to your computer and use it in GitHub Desktop.
Save ejke/cc14fde7ae0368ba19f2e28af0593405 to your computer and use it in GitHub Desktop.
Rebase to squash pushed commits into single commit before merge.

Rebase to squash pushed commits into single commit before merge.

  1. Print a pretty log to deteremine number of commits
git log --pretty=format:"%h %s" HEAD~11..HEAD
  1. Rebase to squash commits together in editor
git rebase -i HEAD~11

change pick to f when you want to merge changes to top commit and discard the message of fixup commit; choose squash for commits where you want to combine the messages, and to reword for the topmost one to reword the final commit. If you don't want to rewrite topmost commit message, keep it as is.

  1. Different steps based on master state

a) If master has changes you want to integrate before force pushing, rebase with master first

git rebase -i origin/master

Would be good if there was a local git graph to check that your changes are moved on top of master now.

Then push softly to your branch on remote.

git push origin your-branch-name --force-with-lease

b) Otherwise, if you don't have any updates on master to rebase, you can push to branch with + force

git push origin +yourBranchName
  1. Now create a PR for review and when done, merge to master.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment