# Update branch without deleting PR

**URL:** <https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964>\
**Category:** General\
**Created:** [October 10, 2020, 12:09pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964 "2020-10-10T12:09:57Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nibble](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nibble/32/123681_2.png) [@nibble](https://forum.freecodecamp.org/u/nibble)\
**Post date:** [October 10, 2020, 12:09pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/1 "2020-10-10T12:09:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [October 10, 2020, 12:48pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/2 "2020-10-10T12:48:54Z")

</div>

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:

```auto
git rebase upstream/master

```

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

> **[freeCodeCamp.org](https://contribute.freecodecamp.org/#/how-to-open-a-pull-request?id=for-usual-bug-fixes-and-features)**
>
> Learn to code at home. Build projects. Earn certifications.

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

---

<div class="post-metadata">

**Author:** ![nibble](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nibble/32/123681_2.png) [@nibble](https://forum.freecodecamp.org/u/nibble)\
**Post date:** [October 12, 2020, 3:44pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/3 "2020-10-12T15:44:22Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [October 12, 2020, 3:58pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/4 "2020-10-12T15:58:10Z")

</div>

> [@nibble](#):
>
> I know my PR will be closed automatically.

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.

---

<div class="post-metadata">

**Author:** ![chuckadams](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/chuckadams/32/109651_2.png) [@chuckadams](https://forum.freecodecamp.org/u/chuckadams)\
**Post date:** [October 12, 2020, 3:58pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/5 "2020-10-12T15:58:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nibble](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nibble/32/123681_2.png) [@nibble](https://forum.freecodecamp.org/u/nibble)\
**Post date:** [October 12, 2020, 4:06pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/6 "2020-10-12T16:06:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nibble](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nibble/32/123681_2.png) [@nibble](https://forum.freecodecamp.org/u/nibble)\
**Post date:** [October 12, 2020, 4:12pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/7 "2020-10-12T16:12:52Z")

</div>

> [@chuckadams](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [October 12, 2020, 4:13pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/8 "2020-10-12T16:13:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chuckadams](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/chuckadams/32/109651_2.png) [@chuckadams](https://forum.freecodecamp.org/u/chuckadams)\
**Post date:** [October 12, 2020, 4:19pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/9 "2020-10-12T16:19:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nibble](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nibble/32/123681_2.png) [@nibble](https://forum.freecodecamp.org/u/nibble)\
**Post date:** [October 12, 2020, 4:20pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/10 "2020-10-12T16:20:28Z")

</div>

Thanks @chuckadams and @Sky020 for your time.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [October 12, 2020, 4:35pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/11 "2020-10-12T16:35:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sky020](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sky020/32/128093_2.png) [@Sky020](https://forum.freecodecamp.org/u/Sky020)\
**Post date:** [October 12, 2020, 4:41pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/12 "2020-10-12T16:41:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:38pm UTC](https://forum.freecodecamp.org/t/update-branch-without-deleting-pr/424964/13 "2021-06-01T18:38:17Z")

</div>

Closed on June 1, 2021, 6:38pm UTC


