How to accept pull requests (PR) from Github using command line git
Asked Answered
G

4

7

Problem

I received a pull request and I want to approve it via the command line (i.e., without logging into Github with my browser and using the GUI). This PR makes changes to a branch.

This is what I tried:

# First I go to the correct branch:
git checkout branch-to-be-modified

# Then I pull the changes made by the person who's contributing with the PR:
git pull https://github.com/my-contributor/code.git his-branch

# I then run `git status`, but it shows me nothing
git status
    # Outputs:
    # > On branch branch-to-be-modified
    # > nothing to commit, working tree clean 

Question

How do I accept the Github pull request through the command line?

What else do I need to do, should I just git push origin branch-to-be-modified?

This should be easy, but I can't find this information. Thank you so much for any leads

Goodish answered 6/2, 2021 at 0:35 Comment(1)
You won't find any Github-specific features in Git. Git is independent of Github. You need to look for a Github tool. You can merge the PR branch manually, but accepting a PR does more than that.Montenegro
S
5

You should ask GitHub to merge said pull request: that would be "accepting" it.

You can do so from command-line, using gh, the official GitHub client

See gh pr merge

gh pr merge <number>

But that supposes that you did login first (for instance, using a token like your PAT: gh auth login --with-token < mytoken.txt).
Again, from command-line, without using GitHub web directly.


But what if you want to accept WITHOUT merging, i.e. in the case of having multiple reviewers?

Then you would need to use gh pr review:

# approve the pull request of the current branch
gh pr review --approve 

That way, you can approve the pull request indicating that you are satisfied with the changes, while still leaving it open for other reviewers to provide their feedback before it gets merged.

And, if you are part of a team project on GitHub, you might have protected branches set up which require multiple reviews before a pull request can be merged.
In such setups, individual approvals can be done using the GitHub CLI as shown above, and the pull request will only be able to be merged once it has received the required number of approvals.

Shed answered 6/2, 2021 at 0:41 Comment(5)
Thank you! So if I want to do it only using git, without gh, I'd have to use instead git merge https://github.com/my-contributor/code.git his-branch? Or is this not possible at all without gh?Goodish
And I also have another doubt, I thought pull == fetch + merge, do I need to do something before using git merge? Why is it different from using git pull instead?Goodish
@Goodish This would not be available with git alone, which does not know about "GitHub Pull Request". gh calls the GitHub REST API, specifically the /repos/{owner}/{repo}/pulls/{pull_number}/mergeShed
@Goodish Since gh pr merge xxx is done entirely on the remote side (GitHub), you don't have to do anything locally.Shed
But what if you want to accept WITHOUT merging, i.e. in the case of having multiple reviewers?Sesquioxide
M
4

Pull Requests are a Github-specific feature. Git is independent of Github, you won't find any Github-specific features in Git. If you want to accept PRs without a web browser you need to look for a Github tool such as Github Desktop or Github CLI. These are all wrappers around the Github REST API and Github GraphQL API.


You can merge the PR branch manually by treating it like any other remote branch, and the PR should recognize its branch has been merged. However Pull Requests can be about a lot more than just merging so you probably want to go with a Github tool.

Each project can merge their Pull Requests in different ways: merge, squash merge, or rebase. You would have to emulate the project's merge strategy. Fortunately, a Git PR will tell you how to do that: next to the Merge pull request button there is a link to "view command line instructions".

enter image description here

Click that and follow the instructions.

The general idea is to:

  • Fetch the remote branch.
    • If it's in a forked repository you will need to add that fork as a remote.
  • Make a local branch from their remote branch.
  • Merge master into the local branch.
    • This will result in the same code as if you merged the local branch into master.
  • Test the branch.
  • If it passes, merge the branch into master using the appropriate merge method.
  • Push your local master.

For more read Working With Remotes in Pro Git and Working With Forks on Github.

Montenegro answered 6/2, 2021 at 1:10 Comment(0)
A
0

You can to have 2 remotes, origin and fork. From there you can merge from the fork branch into your branch. Then commit and push to origin.

Alannaalano answered 6/2, 2021 at 0:46 Comment(3)
Oh, so it is possible to do this only using git? (i.e., without using gh)?Goodish
Actually I'm not sure having 2 remotes make a difference, I'm explicitly specifying the remote with https://github.com/my-contributor/code.git in my example aboveGoodish
You could do the no commit option before. But with regards to approving the PR as others said it's a githib featureAlannaalano
R
0

Typically it is not recommended to blindly approve a PR with gh pr review --approve on its own. Therefore, in order to view the source & destination branches (gh pr view) and a diff of the PR (gh pr diff) with an interactive confirmation of Y/n prior to approval, one can use this all-in-one alias for gh (GitHub CLI):

gh alias set --shell approve '!gh pr view $1 && gh pr diff $1 && echo "Approve PR? (y/N)" && read -r CONFIRM && [ "$CONFIRM" = "y" ] && gh pr review --approve $1' --clobber

Usage in terminal: gh approve <url here>

Doc page here: https://cli.github.com/manual/gh_pr_review

Radtke answered 14/3 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.