🌞


Note that this blog post has been archived. Information may be out of date or incorrect.

Quickly check out GitHub pull-request branches

There’s a guide on checking out a GitHub pull request locally, but since I’m a lazy person I wrote a small bash script that does it for you:

    #!/bin/bash
    REMOTE="origin"
    PULLREQUEST=""

    case "$#" in
        "1")
            PULLREQUEST="$1"
            ;;
        "2")
            REMOTE="$1"
            PULLREQUEST="$2"
            ;;
        *)
            echo "Usage: git pr [remote] <pull-request-number>"
            echo "  [remote] ist optional."
            exit 1
    esac

    git status > /dev/null
    if (( $? != 0 )); then
        # No detailed error message because git status
        # should print an error
        echo "Git doesn't seem to be working :("
        exit 1
    fi

    BRANCH="pr-$PULLREQUEST"
    echo "Fetching 'pull/$PULLREQUEST/head'"
    git fetch $REMOTE "pull/$PULLREQUEST/head:$BRANCH" &&
        git checkout $BRANCH

The usage is very simple. Just put the script anywhere in your PATH, name it git-pr and run the following within a git repository cloned from GitHub:

    git pr id-of-pullrequest # If remote = origin
    git pr some-remote id-of-pullrequest # otherwise

Troubleshooting

If for some reason git should not be able to find the subcommand, you might want to check if the directory in the path where the script lives is absolute (see Stackoverflow).

Thanks to @badboy_ for the suggestion to make it a git subcommand!