How to handle review a pull-request, modify code, and merge?
Asked Answered
K

3

4

(100% in-browser solution: Received a pull-request from someone else, how to edit it before merging directly on github?)


Let's say I have a repository myproject on GitHub.

A user named userblah proposed a pull-request.

I tried the following, in order to test his modifications :

git checkout -b userblah-test    
git pull https://github.com/userblah/myproject.git  

Then I had access to his version in my folder, this is ok.

Then I decided to modify a few things in his code : I opened the code in text editor, did some modifications and saved.

Then I wanted to switch to my master branch again, but I an error saying that I cannot switch to master because non-committed modifications had been done on the current branch userblah-test.

What are the correct commands to handle of a pull-request properly?

I mean :

  1. Pull the user's code into a new branch
  2. Modify his code a little bit according to my tastes
  3. Push this in my master, such that the user name userblah will be registred as contributor of the file
Kuenlun answered 8/12, 2014 at 9:52 Comment(1)
After you made your changes on the new branch did you commit them? If not, this will be why you were getting the error about not being able to checkout master.Henceforward
H
4

I think you're probably looking to perform a merge, which will take changes from one branch, and 'merge' (combine) them with changes on another.

Assuming you want to make changes to the proposed code:

  1. git checkout -b userblah-test (checks out a new branch named userblah-test)
  2. git pull https://github.com/userblah/myproject.git (pull in proposed changes from userblah)
  3. Make your changes
  4. git add . (the . adds all modified files)
  5. git commit (commits the changes - it's important to note that you are committing your changes to the current branch only, i.e userblah-test)
  6. git checkout master (checks out master branch before merging)
  7. git merge userblah-test (merges all changes made on userblah-test with current branch)

However, if the pull request has been submitted through GitHub, I suggest you inform the original author about changes that you would like to make in the pull request comments. The author should be willing to make these changes themselves and add them to the pull request. You can then merge his PR in using GitHub's own web interface - see here for more info.

Pull requests are intended to promote discussion around code, if you're not 100% happy with the changes this person has proposed - tell them!

Henceforward answered 8/12, 2014 at 10:13 Comment(3)
You've basically got the idea down: Checkout a new local branch => Pull in changes from pull request => Review => If happy, checkout master => Merge new local branch. Everything you need to know should be covered in GitHub's help section here. In particular, be sure to read 'Checking out pull requests locally'.Henceforward
the part for which I'm not sure : "Review" : in this part, I would like to modify the code of the proposed-pull-request myself. What to do? Open the editor, save the text file. And then? Commit before switching? then what do to when checkout to master again? (Could you paste these steps to your answer, for future reference) ? Thanks again @rmorrin!Kuenlun
@Kuenlun No problem, I've updated my answer accordingly! It is worth mentioning though that this is not usual practice with pull requests - if you are not happy with someone's proposed changes, you would usually discuss this and have the author add the changes before merging the PR rather than changing their code yourself. This is why all pull requests have a comment thread! :-)Henceforward
K
0

Slight modification to @rmorrin's answer, here is how I tried to handle a Pull Request from userblah:

  1. git checkout -b userblah-test master (added master here and in the next line)
  2. git pull https://github.com/userblah/myproject.git master
  3. Make changes
  4. git add .
  5. git commit -m "test"
  6. git checkout master
  7. git merge userblah-test
  8. git push

The only problem is that then userblah doesn't appear in Github's contributor list, it's like userblah hasn't done anything...

Kuenlun answered 15/11, 2016 at 14:47 Comment(0)
J
0

Go to the pull request Web page and note the PR number at the end of the title, PR_NUMBER, the contributor GitHub name and branch of the contribution, and at the end of the line right after the title check the destination and contribution in the link. "... into DEST_BRANCH from CONTRIBUTOR:CONTRIB_BRANCH". Last, note the contribution forked repo, visible by clicking on the link, e.g. https://github.com/CONTRIBUTOR/CONTRIB_REPO.git. Then use the following commands:

# Get the PR
git clone https://github.com/ORGANIZATION/REPO.git
git checkout -b CONTRIBUTOR-CONTRIB_BRANCH DEST_BRANCH
git pull https://github.com/CONTRIBUTOR/CONTRIB_REPO.git CONTRIB_BRANCH
# Make all your changes, also with multiple commits, e.g.
git add .
git commit -m 'All my changes'
git push https://github.com/CONTRIBUTOR/CONTRIB_REPO.git CONTRIBUTOR-CONTRIB_BRANCH:CONTRIB_BRANCH

This requires that you have write access to the contributor branch (default if you are a reviewer) and will update automatically the PR that you can merge via Web or GitHub CLI. See below if the push fails because you are not authorized to write. The code uses HTTPS URLs, change them if you use SSH.

NOTES

When contributing code it is recommended to use a feature branch in the fork, not main/master, to allow multiple contributions and avoid confusion.

The above code modifies the contributor's branch. As noted in @rmorrin's answer, it is closer to the PR spirit to review and ask the contributor to make the changes. An alternative is to start from the contribution, make the changes, but save them in a new branch. This requires a new PR:

git clone https://github.com/ORGANIZATION/REPO.git
git fetch origin pull/PR_NUMBER/head:pr-PR_NUMBER
git checkout pr-PR_NUMBER
# Make all your changes, also with multiple commits, e.g.
git add .
git commit -m 'All my changes'
# Push your changes
git push origin pr-PR_NUMBER

Then create the new PR from the pr-PR_NUMBER branch (could be on your forked repo if desired), copy the information from the original PR discussion, merge the new PR, and close the original one referencing the new PR. All these solutions preserve the contributor's commit and attribution.

GitHub CLI

If you have the GitHub CLI, many commands can be simplified:

# Get the PR
gh repo clone ORGANIZATION/REPO
gh pr checkout PR_NUMBER
# Make all your changes, also with multiple commits, e.g.
git add .
git commit -m 'All my changes'
# Push your changes
gh pr push

This question and answer are for a reviewer interested in modifying the PR's code. If you are the contributor you can simply update and push your branch, see this question.

Junie answered 30/12, 2023 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.