应用 github 提交/拉取请求作为补丁

Apply github commit / pull request as a patch

如何应用来自 github 的补丁?

我尝试编译 minisat,但我在使用 clang 编译时遇到了两个问题。

第一个问题已在 this github commit 中解决,它是从原始 github 派生出来的。由于变化很小,我可以轻松地修补代码以手动工作。

第二个问题在这个github(https://github.com/niklasso/minisat/pull/17)中解决了,但是补丁没有应用到原始源。我可以通过复制修改后的文件来手动更新代码,但如果我能将这个补丁拉到我的本地目录中会更好。 github 可以做到吗?如果可以,怎么做?

您可以 fork 项目并将第二个添加为第二个远程, 然后您可以将所需的分支合并到您的项目中。

git remote add remote2 git@github.com:niklasso/minisat.git git fetch remote2 git merge remote2 master

然后更新的代码将合并到您的项目中。 一旦将拉取请求应用于原始存储库(合并拉取请求),您将看不到任何更改,因为您的副本中已经有提交 ID。

github 为单个提交和拉取请求提供补丁(尽管我找不到这方面的文档)。

您可以通过简单地将 .patch 附加到原始 url 的末尾来生成补丁 url。

所以,首先使用 https://github.com/JWalker1995/minisat/commit/a8cef9d932552b2ec155d5e0d44d8fe0efa3a235.patch, 第二个 https://github.com/niklasso/minisat/pull/17.patch

生成补丁一般urlgithub.com/original/url/id会变成github.com/original/url/id.patch

就 运行 的命令而言,这变为

  1. 将补丁下载到您的 git 存储库

    wget --output-document=issue1.patch https://github.com/JWalker1995/minisat/commit/a8cef9d932552b2ec155d5e0d44d8fe0efa3a235.patch
    wget --output-document=issue2.patch https://github.com/niklasso/minisat/pull/17.patch
    
  2. 应用补丁

    git apply issue1.patch
    

    检查更改、添加并提交。对补丁 2 重复相同的操作。

您可以查看 this blog post 以获取有关创建和应用补丁的精彩教程。

除了另一个答案中提到的git apply 1.patch之类的命令外,您还可以使用patch命令来完成:patch -p1 < 1.patch.

还有来自 GitHub CLI 的 gh 命令,可以像 gh pr checkout 1 一样使用。