36

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.

fraxture
  • 5,113
  • 4
  • 43
  • 83

5 Answers5

26

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    So, just to b clear, what you are saying is that the danger here is not related to the pull request, but instead to the possibility that other people could be working on this branch? – fraxture Sep 02 '15 at 07:16
  • 2
    This is my feeling. The GitHub pull request doesn't actually "exist" in Git, it is simply a potential operation (merge) which _might_ happen if someone clicks the green button (and if GitHub can't do the merge, the button won't be clickable). On the other hand, if you rewrite the history of the remote branch by force pushing, then anyone who pulls will get an ugly surprise. – Tim Biegeleisen Sep 02 '15 at 07:18
  • For such users, say they have just been viewing/testing the work I've done there on their own machines, if they were to delete the branch locally and then re-pull it, that would settle things, right? – fraxture Sep 02 '15 at 07:54
  • @fraxture Yup, that should be OK. But please tell them what you are doing so there are no surprises. – Tim Biegeleisen Sep 02 '15 at 07:57
  • Nobody else is working on this branch. So my only concern is whether it would corrupt the current pull request, which was made, as I noted, before, and in relation to which there has already been some discussion that I would like to preserve. The changes to the commit history relate to commits made since the initial discussion. But I guess I am curious what will happen to the existing discussion on github regarding the initial commits. Since the whole branch history will be rewritten, does that mean those comments will be flushed? – fraxture Sep 02 '15 at 14:45
  • 1
    When you force push your updated branch, any commits you might inadvertently remove should no longer appear under that branch, since they are no longer associated with that branch. Commits which remain as they were, should also retain the same comments. – Tim Biegeleisen Sep 02 '15 at 14:49
  • Alright, thank you. When I've verified that all this is true for my case, I will mark this as the answer. Thanks for the help. – fraxture Sep 03 '15 at 12:19
7

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.

LarsW
  • 1,514
  • 1
  • 13
  • 25
  • 4
    I was able to get back to the state where the PR could be re-opened by force-pushing the branch at the state when it was closed. To get there I used `git reflog` and `git reflog --date=iso` to find the SHA1 of the last commit before the close, then the syntax `git push origin : --force` where origin is my github repository's fork of the main repo (I closed the pull request on the main repo). [as per my comment on the github issue] – robm Dec 03 '19 at 13:17
4

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.

Maroun
  • 94,125
  • 30
  • 188
  • 241
3

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.

ifma
  • 3,673
  • 4
  • 26
  • 38
0

This didn't work for me. I had to change the branch the PR was merging into and then change it back to the original in order for the Github UI to update the PR to match the new branch commits.

chrowe
  • 762
  • 6
  • 6