Git fetch pull/merge requests

I can never remember the nuance between Github and Gitlab but often want to fetch and checkout a specific pull/merge request when reviewing code. Why they need to implement this feature subtly different is beyond me, but alas.

On Github, to fetch a specific pull request, do:

git fetch $REMOTE +refs/pull/$PR_NUM/merge

On Gitlab, to fetch a specific merge request, do:

git fetch $REMOTE +refs/merge-requests/$MR_NUM/head

Then to checkout the just fetched ref, do:

git checkout FETCH_HEAD

You’ll end up in a detached HEAD state but the HEAD will be at the tip of the pull/merge request.

1 Like

With yoe distribution being on github I have bunch of repos to maintain on github. Here is what I use with github repos

in .git/config this is what I have

[remote "upstream"]
        url = https://github.com/openembedded/meta-openembedded
        fetch = +refs/heads/*:refs/remotes/upstream/*
        fetch = +refs/pull/*/head:refs/remotes/upstream/pr/*

so when I fetch I get them pulled under a namespace called /pr

% git fetch --all
Fetching upstream
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 10 (delta 4), reused 10 (delta 4), pack-reused 0
Unpacking objects: 100% (10/10), 5.15 KiB | 1.72 MiB/s, done.
From https://github.com/openembedded/meta-openembedded
 + 03cb305dee...6d54ec022d master-next        -> upstream/master-next  (forced update)
 * [new ref]               refs/pull/596/head -> upstream/pr/596

Then I can use this reference to stuff with PR e.g.

git checkout -b pr/596 upstream/pr/596

will create a local branch

git cherry-pick upstream/pr/596

is handy for single commits it will bring the patch from PR to my local integration branch
and good thing is when I push my integration up into github it knows about the PR commits and can cross reference them.

2 Likes