如何在本地端取出和送出 Github 的 Pull Requests

最近意識到 Github 上的 pull requests 其實也是 branches 參照,所以你也可以根據 pull request 編號在本地端直接取出。對於常用 pull request 功能來作 code review 的團隊來說,是蠻方便的小技巧。

首先編輯專案下的 .git/config,加上 fetch = +refs/pull/*/head:refs/remotes/origin/pr/* 這一行,例如:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@github.com:ihower/sandbox.git
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

接著打 git fetch origin 就會抓下來這些 remote branches:

From github.com:eduvo/keybridge
 * [new ref]         refs/pull/1/head -> origin/pr/1
 * [new ref]         refs/pull/2/head -> origin/pr/2
 * [new ref]         refs/pull/3/head -> origin/pr/3

接著用 checkout 取出,例如 git checkout pr/3 即可。

喜歡的話,也可以設成 git 全域設定,直接套用所有專案:

git config --global --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"

參考資料: Checkout Pull Requests Locally

本地端送 Pull request

舉一反三,那要怎麼在本地端送 Github 的 Pull Request? 參考 HSATAC 的這篇:用 Commandline 發 Github Pull Request

首先安裝 github 的 hub 工具:

brew install hub # 或
gem install hub

編輯 ~/.bash_profile

# https://gist.github.com/hSATAC/5591270#file-gistfile1-sh
# Usage: pr (pull request current branch into develop)
# Usage 2: pr stable (pull request current branch into stable)
# Notice: replace "team" with your github team account.
 
function pr() {
    base=$1;
    if [ "$1" == "" ]; then
        base="develop"
    fi
    hub pull-request -b team:"$base" -h team:`git rev-parse --abbrev-ref HEAD`;
}

One thought on “如何在本地端取出和送出 Github 的 Pull Requests

Leave a Reply