Checkout GitHub/Gitlab Pull Request Locally

專案目錄底下的 .git/config 檔案內,記錄有 Git 的 remote url,如果是 GitHub,長相類似這樣:

[remote "origin"]
  url = [email protected]:akccakcctw/project.git
  fetch = +refs/heads/*:refs/remotes/origin/*

只要在下面新增一行 fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

[remote "origin"]
  url = [email protected]:akccakcctw/project.git
  fetch = +refs/heads/*:refs/remotes/origin/*
  fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

使用 git fetch origin 時就可以 fetch 到 PR 的 branch:

$ git fetch origin
From github.com:akccakcctw/project
 * [new ref]         refs/pull/1000/head -> origin/pr/1000
 * [new ref]         refs/pull/1002/head -> origin/pr/1002
 * [new ref]         refs/pull/1004/head -> origin/pr/1004
 * [new ref]         refs/pull/1009/head -> origin/pr/1009
...

成功 fetch 後就能 checkout 到該 branch 了:

$ git checkout pr/1002
Branch pr/1002 set up to track remote branch pr/1002 from origin.
Switched to a new branch 'pr/1002'

同理,如果你是用 Gitlab,則把 pull 改為 merge-requests 即可:

[remote "origin"]
  url = https://gitlab.com/akccakcctw/project.git
  fetch = +refs/heads/*:refs/remotes/origin/*
  fetch = +refs/merge-requests/*/head:refs/remotes/origin/pr/*

參考資料

最後更新時間: