Search code examples
gitgithubcommand-linepull-request

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


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


Solution

  • 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.