Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 452
flen Avatar asked Jun 20 '26 02:06

flen


2 Answers

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.

like image 131
VonC Avatar answered Jun 21 '26 19:06

VonC


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.

like image 29
Schwern Avatar answered Jun 21 '26 19:06

Schwern