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

Viewed 4238

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

3 Answers

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

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.

Related