Let us say I fork a repo and cloned it to my machine. Created branch fix-a-certain-issue. I work on this branch and push changes back to origin/fix-a-certain-issue and then opened a PR. Tow days later, the PR is reviewed and I am supposed to make changes to it. The problem is fix-a-certain-issue is behind upstream/master by x commits. How do I update fix-a-certain-issue so that it is even with upstream/master without deleting/closing my PR?
This updates the local branch but prevents me from pushing the changes to my remote branch. For example if I run git rebase upstream/master on my local branch (let us call it fix-a-certain-issue), my local branch will be even with upstream but its remote version isn’t. Actually if I check fix-a-certain-issue on GitHub it is x commits behind upstream/master and y commits ahead of upstream/master. And this is where the problem is. Doing git push origin fix-a-certain-issue leads to this annoying error:
error: failed to push some refs to NAME-OF-REPO
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
If I use git push origin fix-a-certain-issue --force I know my PR will be closed automatically. I don’t want to do that. What is the solution to this problem. I really need this ASAP guys otherwise my head will explode. @Sky020@chuckadams@Roma
To be fair, after I rebase my local branch, I run git push --force. Provided, I am on my local branch, and have set the upstream, everything usually goes ok.
You can’t rebase a branch you’ve already pushed to a remote, because rebase undoes all the history of your commits and replays them on top of the new base (usually the head of another branch like master). The remote will refuse to undo its history to match yours unless you use --force, and while that actually is the appropriate thing to do in this case, you can make any change happen with --force, and repo owners are right to be paranoid about it.
In short, you’re going to have to close your PR, delete the remote branch, push your rebased branch back up, and open a new PR. You don’t have to delete anything on your local repo, just deleting the remote branch on github then pushing it back up will do the trick.
BTW, this is why I always use merge commits. Both github and git itself handle merges intelligently, but a lot of people have this knee-jerk cargo-cult opposition to them because Linus went off on some tantrum about merges once upon a time, and thus merges are always bad and rebases are always “kewl” . Whatever. Git may be a godsend of a tool, but it’s still a goddam disaster in terms of its design and workflow.
@chuckadams That can really be annoying. If I have to make 5 revisions to a PR, essentially I have to delete the branch and re-open the PR 5 times. Each time I do that there are useful reviews and feedback which I have to delete. I can’t believe I have to go that route. Thanks for taking time to answer.
Actually, github should let the owners be able to merge your PR using a rebase and/or squashed commits without you needing to do anything yourself. I’d ask the upstream owners how to proceed.
GitHub should only automatically close a PR if the branch no longer differs from master. So long as the force push-ed branch contains at least one commit that differs from master, then you should be ok.
Nibble, if you are using VS Code, I recommend spending some time setting up the Git integrations, because rebasing and dealing with merge conflicts becomes a breeze with its interface and diff checking.
@JeremyLT Thanks for that. I was unaware such would happen.