Iman Tumorang
Iman Tumorang
Software Engineer - Writer - Open Source Enthusiast - Startup Enthusiast
Jan 23, 2019 3 min read

Today I Learned - Checkout to Pull Request in GitHub Public Repository

How to checkout to the incoming pull request at a public repository in GitHub.

thumbnail for this post

So today I learned a really important thing in Github and open-source world :D. Started from a few months ago, I make a simple library here: This library is helping the Go engineer to create a dummy data based on their structs.

So for example:

In Go, let’s say I have the struct like this below:

type Sample struct {
 Name string 
 Age int

With my faker library, I can generate a dummy data based on that’s struct.

sample:= Sample{}
fmt.Printf("%+v", sample)
//will print: {Name: "Some-random-string", Age: 17 /*random num*/}

This library will very useful for doing testing (unit testing or anything that need a dummy data).

So, a few days ago, someone creates a Pull Request to my library. Usually, if the PR looks good to me, I always try to pull it into my local, and test it directly. To do this, usually, I just clone his forked repository then test it in my local.

But for this time, it’s a bit different. He creates a branch in his forked repo, then creates a direct PR to my repository. So, if I clone his forked repo, this branch does not exist. The same as for my repository, if I fetch all the branch, his PR’s branch does not exist.

So how to checkout to a PR branch?

Checkout to Pull Request in Github

To do this, we only need to change the config file in our .git folder. For my cases, in my project repository there will be hidden folder named .git

$ pwd
$ ls -lah
drwxr-xr-x  33 iman  staff   1.0K Nov 22 11:36 .
drwxr-xr-x   7 iman  staff   224B Nov 12 11:33 .. 
drwxr-xr-x  15 iman  staff   480B Nov 22 11:36 .git 

In the .git folder there will be a file named:config. It more look like this.

$ cat .git/config
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url =
        fetch = +refs/heads/*:refs/remotes/origin/* 
[branch "master"]
        remote = origin
        merge = refs/heads/master

So, to enabled us to checkout to any incoming PR, we need to add:
fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
to the remote origin section.


[remote "origin"]
        url =
        fetch = +refs/heads/*:refs/remotes/origin/*


[remote "origin"]
        url =
        fetch = +refs/heads/*:refs/remotes/origin/*
        fetch = +refs/pull/*/head:refs/remotes/origin/pr/* #look for this added line

Then after doing this, we can fetch all incoming Pull Request to our repository.

$ git fetch origin
 * [new ref]         refs/pull/10/head -> origin/pr/10
 * [new ref]         refs/pull/11/head -> origin/pr/11
 * [new ref]         refs/pull/12/head -> origin/pr/12
 * [new ref]         refs/pull/13/head -> origin/pr/13

Later, let’s just checkout to the PR branch

$ git checkout pr/10 

Anyway, this is also worked in Gitlab and maybe for another Git host out there.

Even a bit different, but the steps is similar.

Checkout to Merge Request (Pull Request) in Gitlab

The steps is just same with in Github, the difference is only the term and the keywords. In Gitlab, instead of Pull Request, they call it Merge Request. So because of this different term some of url will also different.

So, for Gitlab cases, we need to add this line in the same section.

fetch = +refs/merge-requests/*/head:refs/remotes/origin/merge-requests/*

Then fetch it.

$ git fetch origin

Later checkout to the merge request number.

$ git checkout merge-requests/10


Originally posted at:

comments powered by Disqus