loading...

How to create a GitHub pull requests from the CLI

mmphego profile image Mpho Mphego ・4 min read

Originally posted on my blog

If like me, you create a lot of pull requests in a day and annoyed of the idea of repeatedly visiting GitHub to create a PR - do not get me wrong the website is great and all, but it can be annoying sometimes having to leave your CLI and open your browser (which has serious implications to your productivity IMHO).

I have a great solution for you, GitHub has a cool tool that you can use to create PR's and other things via CLI which is available to install depending on your architecture.

NOTE: They (GitHub people) say it doesn't store your login details.

Installing Hub

Hub is available on GitHub so you can download binaries, or install it from source. Unfortunately, with Ubuntu you cannot easily apt-get it, you will need to download the binary and install it that way.

In this post, I will detail the installation and usage.

Note: I'm using git version: 1.9.1 and debian 7.9

Setup

It is simple to install, see below:

# Linux x64
cd /tmp
wget https://github.com/github/hub/releases/download/v2.6.0/hub-linux-amd64-2.6.0.tgz -O - | tar -zxf -
sudo prefix=/usr/local hub-linux-amd64-2.6.0/install && rm -rf hub-linux-amd64-2.6.0
# See: https://github.com/github/hub#aliasing
echo "alias git=hub" >> ~/.bashrc
source ~/.bashrc

Usage

After a successful installation, You are Git+Hub ready.
If you hit man hub in your cli you should see something like this.

Testing

From the image below, I established that the tool works 100%.

Tricks

But wait there's more.

Integrating with Git Alias.

The amount of time hub has since saved me by keeping my hands on the keyboard, is priceless. It would be a great idea to push and create pr's automagically - that's where aliases come it.

I'm a big fan of Git aliases, so I decided to create an alias called create-pr that pushes, and creates a pull request on GitHub.

You can create aliases directly from the command line with git. To open your global .gitconfig for editing, hit:

git config --global --edit

This will pop-up an editor of choice, and allow you to edit your .gitconfig.
Find [alias] section and copy below function under [alias].

[alias]

    create-pr="!f() { \
        BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD); \
        git push -u origin "${BRANCH_NAME}"; \
        hub pull-request --no-edit; \
        };f"

This alias uses the slightly more complex script format that creates a function and executes it immediately. In that function, we do three things:

  • BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD);
    • Get the name of the current branch from git and store it in a variable, BRANCH_NAME.
  • git push -u origin "${BRANCH_NAME}";
    • Push the current branch to the remote origin, and associate it with the remote branch of the same name
  • hub pull-request --no-edit
    • Create the pull request using hub and use the message from the first commit on the branch as pull request title and description without opening a text editor

To use the alias, simply check out the branch you wish to create a PR for and run:

git create-pr

This will push the branch if necessary and create the pull request for you, all in one (PR title will be your last commit message).
Note: If you want to read more about Git Aliases, I would advise you read this post by Phil Haack.

Bash Functions

I personally prefer, placing useful tools in my ~/.bash_functions for even much easier access, you can copy the code below and place it in your .bashrc if you do not have a .bash_functions file.

mkpr() {
    BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD);
    git push -u origin "${BRANCH_NAME}" || true;
    hub pull-request --no-edit || true;
    }

If like me you like doing things efficiently, also add the following function to your .bashrc.

function commiter() {
    # Add file, commit and push
    if [ $# -eq 0 ]; then
        # Ensure that file is supplied else exits
        echo "No file supplied"
        exit 1
    fi

    git add -f "$1";
    if [ "$2" == "" ]; then
        git commit -nm"Updated $1";
    else
        git commit -nm"$2";
    fi;
    bash -c "git push -q &"
    }

The function commiter above does three things:

  • git add -f "$1";
    • This command updates the index using the current content found in the working tree, to prepare the content staged for the next commit.
  • git commit -nm"$2";
    • Stores the current contents of the index in a new commit along with a log message from the user describing the changes, if no message is supplied, Updated <filename> is used as a commit message.
  • bash -c "git push -q &";
    • Updates remote reference using local references quietly in the background.

Usage

The following command with commit my file main.py, push and create a pull request - without the need of me opening up a browser.

commiter main.py && mkpr

Conclusion

Hub is a great tool that wraps around Git and simplifies some of the things I do a couple of times a day. check it out on GitHub and enjoy.

Posted on Nov 28 '18 by:

mmphego profile

Mpho Mphego

@mmphego

Software Engineer in Cape Town, South Africa

Discussion

markdown guide
 

This is more a post about the hub (which is awesome). But you don't have to install hub to be able to do PRs via CL, git already has us covered with built-in pull request command:
git-scm.com/docs/git-request-pull

 

Thanks for the comment

If you are talking about git request-pull, I am sorry to burst your bubble (No Offence ;-)) but it doesn't do what you think it does.

Read the man page

Update: I discovered that my git version 1.9.1 did not support making pull requests.

 

That was a wrong link sorry.

Burst my bubble? I'm offended now

Generate a request asking your upstream project to pull changes into their tree. The request, printed to the standard output, begins with the branch description, summarizes the changes and indicates from where they can be pulled.

I suppose my bubble is busted!
I will have to give it a try later.

Update: My git is outdated current version: 1.9.1, hence why it doesn't support creating a pr.

 

Hub has since added a --draft flag for creating draft pull requests. This is my default for creating PRs and is a useful addition to the create-pr function defined in this article.

 

Wow thanks, haven't been keeping an eye on the project progress.
Thanks I will update my dotfiles...