newline

Table of Contents

  1. Listing all pull requests

TIL: merge a Github PR entirely from your terminal

TIL, Git

September 07, 2020

Let’s say someone makes a pull request (PR) to your repository, but you want to handle it from the commandline, without using the browser. Perhaps it’s the middle of the night and you don’t want to blind yourself with a white screen. Whatever your reasons may be — all you need is a git fetch.

Take note of the PR’s number — for example, if you have email notifications set up, the number will be mentioned in the email notifying you of the PR. It’s the same number that you see in the GitHub URL: e.g. for a URL of the form https://github.com/user/repository/pull/4, the PR number will be 4.

Then type the command

git fetch <origin> pull/<ID>/head:<target-branch>

where <origin> is the name of the remote, <ID> is the PR number, and <target-branch> is the name you want to give the local branch you’ll check out (can be omitted, in that case it will be auto-assigned).

If it was successful, the PR will be checked out at <target-branch>. Now you can switch to the branch, view diffs, etc.

You can also merge the branch, which will mark the corresponding PR as merged on GitHub. I recommend the --no-ff flag, which will create a separate commit for the merge, even if it’s not necessary; this clarifies that it’s a merge from a different branch. Then, you can push the merge as you’d push any other commit, and delete <target-branch>.

Listing all pull requests

You can also list all of the opened pull requests in a repository. Run the command

git ls-remote <repo>

where <repo> can be a remote’s name or URL (the repository doesn’t have to be cloned locally). This command gives you all of the references in the repository. From there, you can pipe to e.g. grep and look for refs including the word pull (for Github) or merge-requests (for Gitlab).