Unable to update a Pull Request on Github if the main branch has new commits

Viewed 59

My team is running into an issue with a workflow that used to work fine for me. Or at least I remember it working fine.

// TL;DR

After rebasing the local version of feature-branch with the local main branch, I'm unable to push the local feature-branch to the existing remote feature-branch. How does one deal with this? Or is there a better workflow when creating pull requests?

// Here's the full scenario:

  1. To start building my feature I run git checkout -b feature-branch while on the main branch.
  2. The above puts me in the feature-branch. I build my feature and then run git push -u origin feature-branch to push the branch to github.
  3. I create a pull request on github and have others make comments. I make some changes to local feature-branch and run git push to update the remote branch.
  4. My pull request is approved and I'm ready to rebase and merge w/ main branch, BUT there have been other commits to the main branch since my pull request went live. Github is unable to do the rebase and merge as there are conflicts. Hence I need to rebase my changes on top of the main branch locally, fix and then push. The way I do this is:
    1. Locally, I switch to main branch and run git pull. This pulls in all the latest changes from main.
    2. Locally I switch to my feature-branch and then run git rebase main. This rebases my changes on top of the new changes in the main branch. I fix conflicts during the rebase.
    3. I try to run git push to update the remote feature-branch but this errors saying the remote and local branch have diverged. I thought I used to do this all the time but maybe I'm mistaken. What is the typical/good/correct workflow for this?

Thank you in advance for your help!

1 Answers

I don't know what the rebase is supposed to be for. Either the pull request can merge to main or it can't. If it can, merge it. Done! If it can't because there are merge conflicts, then reverse merge:

  • fetch
  • merge main into feature branch
  • resolve any conflicts
  • push

Now the pull request will merge cleanly into main.

If your feature branch is long lived (it takes you days to finish) you should be reverse merging every day to reduce the pain.

Related