Update branch without deleting PR

Hi campers,

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?

Hey nibble,

If you need to update a branch with what has been committed to upstream, this is called a rebase.

Generally, you will use this command on the branch you want to update:

git rebase upstream/master

If this has at all to do with fCC, the docs detail the workflow quite well:

You will likely need to resolve any merge conflicts, then push the updates. There is a lot more you can learn about this, by Googling git rebase

Hope this helps

3 Likes

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

I can see no reason why this would happen?

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.

Maybe others can provide more insight, though.

1 Like

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.

1 Like

@Sky020. It has happened to me before. Actually doing git push origin branch-name --force will close any PR you have made from that branch.

@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.

Well, sorry about that nibble. I might be out of my depth here.

I do this on an almost daily basis, and have never had a PR automatically close.

1 Like

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.

1 Like

Thanks @chuckadams and @Sky020 for your time.

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.

1 Like

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.

2 Likes