Things I've learned

Rebasing a branch with main

Git Version control

I have a forked project to which I have submitted a pull request (PR). The review is complete, my work is ready to be merged. We’re all excited to have the fix or feature become available.

Alas, my branch is behind some other commits in main and I need to rebase my branch before the PR can be merged.

I used to dread rebasing until a friend showed me what to do.

Preconditions

The git pull fast-forward and rebase settings are in place as follows. This is a one-time global configuration and is a good default for my circumstances.

git config --global --add pull.rebase true
git config --global --add pull.ff only

Rebase

Make sure the main branch (or master in some projects) up-to-date:

git checkout main
git pull

Switch back to the PR’s branch and rebase:

git rebase -i main
git push -f

I have seen others somtimes use git rebase -i HEAD~N for some number of past N commits. And then force push.