Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if you force push to a branch with an existing pull request?

I am working on a git branch that is currently part of a closed pull request on github. Subsequently to the initial pull request, I made some additional commits, pushed those commits to github, and then reverted those commits and recommitted them because I need to revise the commits.

Now if I want to push those commits to github, I'll need to issue a force push: git push --force. But I have the strong feeling that this might cause mayhem with the existing pull request, even though the changed commits occurred after the commits that existed prior to me making the pull request.

Can anyone describe to me how best to handle this situation? I think I may need to just create an entirely new branch and then issue a pull request on that branch instead. But if there's a way to link this to the existing pull request that would be preferable.

like image 567
fraxture Avatar asked Sep 02 '15 07:09

fraxture


People also ask

Can you push to existing pull request?

To make changes to an existing pull request, make the changes to your local branch, add a new commit with those changes, and push those to your fork. GitHub will automatically update the pull request.

Can I push to a branch with a pull request?

In summary: You can push to an existing pull request if you push to the fork/branch that PR is based on. It is often possible depending on repo settings.

What happens if you force push git?

The --force option for git push allows you to override this rule: the commit history on the remote will be forcefully overwritten with your own local history. This is a rather dangerous process, because it's very easy to overwrite (and thereby lose) commits from your colleagues.

Does git push force affect other branches?

git push --force overwrites the remote branch, while git push --force-with-lease only overwrites the remote branch if your local copy is aware of all of the commits on the remote branch. This difference makes it significantly more difficult to destroy someone else's changes on the project.


4 Answers

If my memory serves me correctly, then if you force push or update the branch in question in any way, GitHub will automatically update the pull request. If doing the force push would result in the pull request not being possible, then GitHub will tell you this.

You do not need to worry about updating the pull request as GitHub will take care of this for you.

This being said, doing a git push --force on any remote branch can cause mayhem for your coworkers who are also currently working on this branch. So you are correct to be shying away from doing a force push, but fortunately a GitHub pull request is not your biggest problem.

like image 162
Tim Biegeleisen Avatar answered Oct 08 '22 09:10

Tim Biegeleisen


After using git push --force on a regular but closed GitHub PR today I can't reopen it anymore, as the button is disabled, with the message:

The patch-1 branch was force-pushed or recreated.

However, per isaacs/github#361 (specifically this), this seems to happen on closed PRs only.

like image 21
LarsW Avatar answered Oct 08 '22 09:10

LarsW


Pushing to a branch with existing pull request will update the pull request, it doesn't matter that the commits were done after the pull request.

If you don't want to change the pull request, you should create a new branch and work on it.

like image 4
Maroun Avatar answered Oct 08 '22 09:10

Maroun


The contents of the pull request will be whatever is in the branch that is sent for the pull request. Consequently, when you git push --force, the PR will reflect your revised commit history in the branch that you just pushed to.

like image 3
ifma Avatar answered Oct 08 '22 11:10

ifma