【问题标题】:How to apply a git patch when given a pull number给定拉号时如何应用 git 补丁
【发布时间】:2011-10-19 19:30:26
【问题描述】:

我从 git 下载了一个主干版本的代码库,但存在构建错误。 Aparently 一个补丁现在可用,我收到了一封电子邮件:

补丁见 https://github.com/JustinTulloss/zeromq.node/pull/47

我是 git 新手,所以我不太确定如何处理这个“补丁”,尤其是因为该页面看起来更像是一个讨论线程。

有谁知道我如何获取/应用这个补丁到我本地克隆的 git 存储库?

【问题讨论】:

    标签: git github pull-request


    【解决方案1】:

    将补丁保存在某处。如果您使用的是 linux,则可以使用 curl:

    curl -L https://github.com/JustinTulloss/zeromq.node/pull/47.patch > /tmp/47.patch
    

    要应用补丁,请使用git apply。您可以使用check 选项查看补丁是否会干净地应用。切换到你的 git 目录并运行:

    git apply --check /tmp/47.patch
    

    如果您想应用补丁,请删除检查选项

    git apply /tmp/47.patch
    

    【讨论】:

    • 非常感谢您的指导。我完全按照你说的做了。看来补丁是错误的。 FML
    • 如果存储库是私有的怎么办?在这种情况下,当我 curl https://github.com/username/private_repo/pull/42.patch 时,我得到了一个(预期的,但没有帮助的)404。我试图从我的浏览器中打开补丁并保存它(太可怕了!)
    • 要将提交(包括作者姓名、提交评论等)放入您的历史记录中,请使用git am 而不是git apply
    • @Josh,你真的让我很困惑,直到我意识到你回答的是伪经作者,而不是我。
    • 仅供参考,.patch URL 连接了 PR 分支中的所有单个提交(相对于当时的先前提交),因此可能无法完全适用于当前 master。您可以使用.diff 获取与当前主服务器的差异。
    【解决方案2】:

    只需在末尾添加.patch 即可获取补丁:

    https://github.com/JustinTulloss/zeromq.node/pull/47.patch

    您可以执行以下操作:

    $ git checkout master
    $ curl http://github.com/JustinTulloss/zeromq.node/pull/47.patch | git am
    $ git push origin master
    

    http://help.github.com/send-pull-requests/

    【讨论】:

      【解决方案3】:

      规则似乎最近改变了。

      之前我们做了一个 PR 并在末尾添加了.patch 以获取补丁

      http://github.com/[group]/[project]/pull/30583.patch
      

      但现在链接重定向(301)到

      https://patch-diff.githubusercontent.com/raw/[group]/[project]/pull/30583.patch
      

      所以如果你使用curl,你可以使用git apply 命令从Pull Request 中应用一个git 补丁

      curl https://patch-diff.githubusercontent.com/raw/[group]/[project]/pull/30583.patch | git apply
      

      如果补丁现在不适合您,请使用git apply -R 命令回滚更改。

      【讨论】:

      • -L 参数添加到 curl 以使 curl 遵循 301/302 重定向。
      【解决方案4】:

      要让 git 下载 pull request 47 并将其修补到本地 mylocalbranch,请运行:

      git checkout -b mylocalbranch
      git pull origin pull/47/head
      

      如果拉取请求不在原始仓库中,请运行

      git remote add patchremote https://github.com/JustinTulloss/zeromq.node
      git pull patchremote pull/47/head
      

      【讨论】:

        【解决方案5】:
        git fetch -q origin +refs/pull/47/merge:
        git checkout -qf FETCH_HEAD
        

        【讨论】:

        • 这里你只是获取和签出 PR。 OP 还需要将 PR 应用(合并)到他自己的分支中。无论如何我都赞成,因为 fetch 命令行非常方便。
        猜你喜欢
        • 1970-01-01
        • 2015-05-21
        • 2017-07-12
        • 2012-10-22
        • 1970-01-01
        • 2013-08-31
        • 1970-01-01
        • 2010-12-07
        • 2020-08-13
        相关资源
        最近更新 更多