Managing your GitHub pull requests from the command line

Managing your GitHub pull requests from the command line

At Mergify, we love providing the best tools for developers. Like many software engineers out there, we leverage GitHub to collaborate. After all, that?s how we started this!

Image for post

The GitHub pull request model is nice, but usually requires a lot of manual steps:

  1. Forking the repository by clicking in the UI
  2. Adding your forked repository as a remote with the git command line
  3. Creating a new local branch with the git command line
  4. Committing to your new local branch with the git command line
  5. Pushing that new local branch to your repository with the git command line
  6. Creating a pull request by clicking in the UI

Once you did all of that, then the workflow is different if some of the steps are already done. If this isn?t the first time you contribute to this repository, there might be no need to fork the repository.

Maybe your pull request has already been created, so pushing to your existing branch and repository will update the pull request. Who knows?

This clumsy workflow mixing command line and browser interactions was not working for us, so we build something else.

Image for post

git-pull-request to the rescue

We started git-pull-request a while ago in order to simplify the workflow around pull request.

You can install it by using pip, the Python package manager: python3 -m pip install git-pull-request. Be sure to use pip from Python 3!

You now have access to a new git command, named git pull-request. You don?t have to interact with GitHub anymore:

$ git branch new-feature$ <edit your files>$ git commit -a -m ‘Here is a new feature’$ git pull-requestForked repository: https://github.com/user/repoFetching originremote: Enumerating objects: 17, done.remote: Counting objects: 100% (17/17), done.remote: Compressing objects: 100% (9/9), done.remote: Total 17 (delta 8), reused 11 (delta 7), pack-reused 0Unpacking objects: 100% (17/17), done.From https://github.com/user/repo a2d57a3f..0a6bdbe8 master -> origin/masterForce-pushing branch `new-feature’ to remote `github’Enumerating objects: 38, done.Counting objects: 100% (38/38), done.Delta compression using up to 8 threadsCompressing objects: 100% (24/24), done.Writing objects: 100% (24/24), 4.72 KiB | 4.72 MiB/s, done.Total 24 (delta 18), reused 0 (delta 0)remote: Resolving deltas: 100% (18/18), completed with 13 local objects.remote:remote: Create a pull request for ‘new-feature’ on GitHub by visiting:remote: https://github.com/user/repo/pull/new/new-featureremote:To https://github.com/me/repo.git * [new branch] new-feature -> new-featurePull-request created: https://github.com/user/repo/pull/12345

In a simple command, the repository has been forked, your fork has been configured locally as a Git remote, the branch has been pushed and the pull request has been created.

The content of the pull request is taken from the git log command, though you can ask to run$EDITOR to change that.

Updating pull requests

We just covered creating pull requests, but what about updating them?

Most people just keep committing to their branch and then type git push. That?ll still work. If you?re like us, and prefer to have clean commits, you?ll probably amend an existing commit or play with git rebase.

Whatever your workflow is, you can update your pull request by typing git pull-request again while your branch is checked out:

$ git pull-requestForked repository: https://github.com/user/repoFetching originRebasing branch `new-feature’ on branch `origin/master’Current branch new-feature is up to date.Force-pushing branch `new-feature’ to remote `github’Enumerating objects: 25, done.Counting objects: 100% (19/19), done.Delta compression using up to 8 threadsCompressing objects: 100% (11/11), done.Writing objects: 100% (11/11), 3.18 KiB | 3.18 MiB/s, done.Total 11 (delta 7), reused 0 (delta 0)remote: Resolving deltas: 100% (7/7), completed with 7 local objects.To https://github.com/me/repo.git + b15d766c…ebfd2b87 new-feature-> new-feature (forced update)Pull-request updated: https://github.com/user/repo/pull/12345

This is something that we love with git-pull-request, is that there?s only one command to remember to create or update your pull request. It makes it really easy and fast to work with pull requests.

Last tip!

Use a Git alias to save a few keystrokes:

git config –global alias.pr pull-request

Then you?ll be able to just type git pr to create or update your pull request!

16