我如何从GitHub PR中签出已被压缩合并到main中的提交?



在$WORK我们使用压缩合并策略与GitHub,然而这使得git责备几乎无用。它只显示压缩的合并提交,其中可能包含许多没有帮助的更改。

我不能改变我们的合并策略,但是我希望能够检出合并PR的提交(在它被压缩到main之后)本地。有办法做到这一点吗?

你可以使用GitHub发布的ref的refs/pull/命名空间

例如:

git fetch origin refs/pull/42/head
git checkout FETCH_HEAD

或取到本地分支:

git fetch origin refs/pull/42/head:branch_name
git switch branch_name

即使合并了pull request,这个ref也是可用的。

您还可以使用命令git ls-remote:

列出当前获取到本地存储库的所有此类refs。
$ git ls-remote origin 'refs/pull/*/head'
3a12346f1d58b3be4742392d9924043a999428a3        refs/pull/1/head
023496783fab0589e7adf59ada4afc5343d7a252        refs/pull/2/head
ee06715454cacde0c5728be56158dadc30da5cac        refs/pull/3/head

  • GitHub官方文档
  • Section "Pull Request "在Pro Git书
  • Bitbucket中相同功能的描述在一个支持问答a中