Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fetch an unmerged pull request for a branch I don't own?

People also ask

How do I fetch from another branch?

just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.

Can you create a pull request without a branch?

You can create pull request but you instead fork someone project then upload your own and in your fork make changes. Select options to compare branches. This is one of method.


To fetch a pull into your repository:

git fetch [email protected]:jboss/jboss-common-beans.git refs/pull/4/head

Then do whatever you want with FETCH_HEAD:

git checkout -b new-branch FETCH_HEAD

git pull origin pull/28/head

Or

git fetch origin pull/28/head:28
git checkout 28

Can I pull a not-yet-merged pull request?


You can do this:

1) Add the upstream remote:

git remote add upstream [email protected]:Particular/NServiceBus.git

2) After that, you can checkout any pull request to a new branch per its ID:

git fetch upstream pull/PULL_REQUEST_ID/head:NEW_BRANCH_NAME

Then you'll have a branch named NEW_BRANCH_NAME containing the PR code.

Adding an alias:

If you do this as often as me, you may want to setup some aliases for it. I have this in my .gitconfig:

[alias]
    fetch-pr = "!f(){\
        [ -z \"$1\" ] && { echo Usage: git fetch-pr PULL_REQUEST_ID [REMOTE_NAME] [NEW_BRANCH_NAME]; exit 1; }; \
        remote=${2:-origin}; \
        branch=${3:-pr-$1}; \
        git fetch $remote \"pull/$1/head:$branch\"; \
        }; f "
    pr = "!f(){\
        branch=${3:-pr-$1}; \
        git fetch-pr \"$@\"; \
        git switch $branch; \
        }; f "

With the above, I can do:

git fetch-pr 123              # fetch PR #123 into branch pr-123
git fetch-pr 123 some-branch  # fetch PR #123 into some-branch
git pr 123                    # fetch and switch to the branch

For difficult situations (especially if you have not a checked out git-repo), I think the simplest way is to apply a patch. For this just open the pull-request on github and add a ".patch" to the URL, download it and apply the patch.

Example:

cd cordova-plugin-media
wget https://github.com/apache/cordova-plugin-media/pull/120.patch
patch -p1 < 120.patch

See this help article from GitHub: https://help.github.com/articles/checking-out-pull-requests-locally


github/hub

https://github.com/github/hub is a GitHub CLI helper that deals with this and other use cases beautifully using extra information from the GitHub API. E.g.:

git clone https://github.com/github/hub
# Just copy paste the URL.
hub checkout https://github.com/github/hub/pull/970

Result:

  • we are now on a branch called <USERID>-<BRANCH_NAME> that contains the PR.

    Note the good branch name which was automatically set for us.

  • that branch is set to track the original branch on the fork, i.e. .git/config contains:

    [branch "<USERID>-<BRANCH_NAME>"]
        remote = retronym
        merge = refs/heads/ticket/969
        rebase = true
    

    So if further commits get pushed, we can git fetch them directly.

Installing hub on Linux is currently a pain if you're not familiar with Go, but worth it. On Ubuntu 14.04, the Go on the repositories is too old, so GVM is the best option:

bash < <(curl -LSs 'https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer')
. "$HOME/.gvm/scripts/gvm"
gvm install 'go1.4'
gvm use 'go1.4' --default
go get github.com/github/hub

I have also asked GitHub to give us a copy paste cheatsheet on the web UI at: https://github.com/isaacs/github/issues/449