github: Adding commits to existing pull request
Asked Answered
N

7

133

I opened a pull request to rails repo on github by using Fork & Edit this file file button.

Now, After getting feedback on my PR, I wanted to add some more commits. so here is what I ended by doing

$ git clone [email protected]:gaurish/rails.git #my forked repo
$ git rebase -i 785a2e5 #commit hash of my commit using which PR was opened
$ git checkout patch-3 #branch name I had to send my commits under to be shown in that PR
$ git commit -am "Changes done as per feedback"
$ git push origin patch-3

This worked fine but seems quite a complex workflow. Maybe I am wrong something wrong here?

my question is: Am I doing this the correct way? if not, then what is the proper way to do this?

Nutty answered 13/4, 2012 at 19:53 Comment(2)
Some coming here may find this fits their scenario better: #9790948Seven
I also found this version of the question/answer clearer: #7947822Cosy
H
87

Since you're using GitHub's tools and just changing one file, you could also browse to the file on GitHub, select the proper branch from the upper left corner under the "tree:" dropdown (patch-3 in your case), and now choose "Edit this file". Now your changes will be committed to this branch and will show up in your pull request

Hillinck answered 13/4, 2012 at 20:20 Comment(2)
Note this won't work if the branch is protected, the edit button will be greyed outDisharoon
You also must be on the repo the PR is originating from, e.g. if you're merging into an upstream repo, you can only do the above if you go to your fork.Godparent
G
69

Yeah - you are doing a lot more work than you need to. Just make an additional commit and then force push it. You'll see the original commit as well as then newly pushed one when you refresh github in your browser.

$ git commit -m "These changes are in response to PR comments"
$ git push -f origin HEAD
Gatehouse answered 21/5, 2020 at 15:31 Comment(3)
this is the easiest and most straight forward way, not sure why its not top answerLookthrough
I second this; this is indeed the most straight-forward solution without having to use the GitHub interface.Moina
Definitely the most straightforward way to solve the problem, should be top answer.Fissi
S
11

I just recently blogged on this topic:

How do we keep this feature branch up to date? Merging the newest upstream commits is easy, but you want to avoid creating a merge commit, as that won’t be appreciated when pushed to upstream: you are then effectively re-committing upstream changes, and those upstream commits will get a new hash (as they get a new parent). This is especially important, as those merged commits would be reflected in your GitHub pull request when you push those updates to your personal GitHub feature branch (even if you do that after you issued the pull request.)

That’s why we need to rebase instead of merging:

git co devel #devel is ansible's HEAD aka "master" branch
git pull --rebase upstream devel
git co user-non-unique
git rebase devel

Both the rebase option and rebase command to git will keep your tree clean, and avoid having merge commits. But keep in mind that those are your first commits (with which you issued your first pull request) that are being rebased, and which now have a new commit hash, which is different from the original hashes that are still in your remote github repo branch.

Now, pushing those updates out to your personal GitHub feature branch will fail here, as both branches differ: the local branch tree and the remote branch tree are “out of sync”, because of those different commit hashes. Git will tell you to first git pull --rebase, then push again, but this won’t be a simple fast-forward push, as your history got rewritten. Don’t do that!

The problem here is that you would again fetch your first changed commits as they were originally, and those will get merged on top of your local branch. Because of the out-of-sync state, this pull does not apply cleanly. You’ll get a broken history where your commits appear two times. When you push all of this to your GitHub feature branch, those changes will get reflected on the original pull request, which will get very, very ugly.

AFAIK, there is actually no totally clean solution to this. The best solution I found is to force push your local branch to your GitHub branch (actually forcing a non-fast-forward update):

As per git-push(1):

Update the origin repository’s remote branch with local branch, allowing non-fast-forward updates. This can leave unreferenced commits dangling in the origin repository.

So don’t pull, just force push like this:

git push svg +user-non-unique

or:

git push svg user-non-unique --force

This will actually plainly overwrite your remote branch, with everything in your local branch. The commits which are in the remote stream (and caused the failure) will remain there, but will be dangling commits, which will eventually get deleted by git-gc(1). No big deal.

As I said, this is AFAICS the cleanest solution. The downside of this, is that your PR will be updated with those newest commits, which will get a later date, and could appear out of sync in the comment history of the PR. No big problem, but it could potentially be confusing.

Stratiform answered 13/4, 2013 at 18:41 Comment(0)
P
7

You could also create a new pull request which is bound to master instead of a specific abc1234 revision.

That way, any new commit/push to your repository will be added to the pull request.

Perissodactyl answered 13/4, 2012 at 20:9 Comment(0)
F
3

TL;DR; To add commits to an existing pull request you simply add them to the branch used to contribute the code for the pull request. The PR is updated automatically.

Simple changes can be done on the GitHub web pages either in the forked repo (see Abe's reply) or, better, from the PR: in the "Files changed" tab, pick the file to edit and in the upper right corner of its box the "..." menu gives you "Edit file" that allows to save and commit the changes. Either way the changes are in the forked repo and reflected in the PR.

For more complex changes the code in the question is correct (the rebase is not necessary):

git clone [email protected]:gaurish/rails.git  # you forked repo
git checkout patch-3  # branch name used to create the PR
# do as many commit or rebase as needed to bring the code to the desired state
git commit -am "Changes done as per feedback"
git push origin patch-3
# if history was changed (by a rebase, amend, or squash, ... ) a forced push is needed
git push -f origin patch-3

After the push, the PR is updated with the new code. The person merging the PR can use a rebase or squash, so you can skip it in the PR.

Similar actions are possible also if you are the reviewer (the use case I was looking for), want to make a change before merging, and the PR author gave you write access (the default). For that see this question for an in-browser solution and this question for a command line solution.

Fanny answered 29/12, 2023 at 6:6 Comment(0)
S
2

I was able to add more commits to an existing pull request by following these steps:

  1. Push the newest commits to your feature branch
  2. In GitHub, go to the feature branch and click 'New Pull Request'
  3. You should be able to see the previously open pull request in the following screen. Now click on 'View Pull Request': enter image description here
  4. Your newest commits should be added to your existing pull request now. Leave a comment if necessary
Spradling answered 1/5, 2022 at 18:0 Comment(1)
Doesn't work. You indeed see the previous MR, but no more. The new commits aren't added 'magically'.Peroxidize
L
2

If you already have your pull request for the branch, your new commits should be part of this pull request for this branch.

You can check the changed files in your PR, and verify it.

Laurice answered 12/4, 2023 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.