When working on a feature branch you should keep the branch up to date by rebasing on to your main branch.

You should also open a PR early, keeping it in a β€œDraft” state.

But what happens if there is a force push to main while your branch is open. Well, your PR starts to look very busy, showing all the commits that are different to main. These often include commits that were not made by you.

So how can you fix it?

  1. Take a backup of your branch by renaming the feature branch to something like backup/feature-name
  2. Create a new feature-name branch, branching from origin/main
  3. Cherry-pick the relevant commits from backup/feature-name onto the new feature-name branch.
  4. Finally force push your feature-name branch