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

后端 未结 13 1587
北荒
北荒 2020-11-27 10:55

I need to pull in a specific pull request (that hasn\'t been processed into the main stream yet) in the NServiceBus repo:

https://github.com/johnsimons/NServiceBus/c

13条回答
  •  忘掉有多难
    2020-11-27 11:17

    You can do this:

    1) Add the upstream remote:

    git remote add upstream git@github.com: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
    

提交回复
热议问题