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

12

我该如何应用来自github的补丁?

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

第一个问题在这个Github提交中得到解决,它是从原始github分叉出来的。由于更改很小,我可以手动修补代码以使其正常工作。

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

3个回答

23

GitHub提供个别提交和拉取请求的补丁(虽然我找不到相关文档)。

你可以通过在原始URL末尾简单地添加.patch来生成补丁URL。

因此,对于第一个,请使用https://github.com/JWalker1995/minisat/commit/a8cef9d932552b2ec155d5e0d44d8fe0efa3a235.patch,对于第二个,请使用https://github.com/niklasso/minisat/pull/17.patch

一般格式为github.com/原始URL/id,生成补丁的URL为github.com/原始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
  • 应用补丁

  • git apply issue1.patch
    

    检查更改,添加并提交。对于补丁2重复相同的步骤。

    您可以查看这篇博客文章,了解有关创建和应用补丁的不错教程。


    3
    请前往https://github.com/JWalker1995/minisat/commit/a8cef9d932552b2ec155d5e0d44d8fe0efa3a235.patch下载文件并将其保存为“1.patch”,然后前往https://github.com/niklasso/minisat/pull/17.patch下载文件并将其保存为“2.patch”。之后运行命令git apply --check 1.patchgit apply --check 2.patch查看是否有错误。如果没有错误,运行命令git apply 1.patchgit apply 2.patch对你的文件进行补丁更改。 - Serban Constantin
    注意 git apply 也可以从标准输入读取。因此,可以在终端中发出以下命令:curl -sL -o - https://github.com/org/repo/pull/92.patch | git apply - - bric3

    0
    除了另一个答案中提到的命令 git apply 1.patch,你还可以使用 patch 命令来执行它:patch -p1 < 1.patch
    此外,还有来自 GitHub CLI 的 gh 命令,可以像这样使用:gh pr checkout 1

    0
    你可以fork这个项目并将第二个作为第二个远程仓库添加, 然后可以将所需分支合并到您的项目中。
    git remote add remote2 git@github.com:niklasso/minisat.git git fetch remote2 git merge remote2 master 然后更新的代码将被合并到您的项目中。 一旦pull request被应用到原始repo中(已合并的pull request),您将不会看到任何更改,因为您的副本中已经有提交ID。

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接