【问题标题】:git format-patch for all the commits to a filegit format-patch 用于对文件的所有提交
【发布时间】:2018-07-02 04:31:12
【问题描述】:

我想为文件/文件夹的所有提交获取补丁。

我可以通过git log --format="%H"-- path/to/folder获取commit-id列表

有没有办法从这个列表中获取生成补丁。

[编辑]:

下面的代码部分解决了我的问题

for c in `git log --format="%H" -- path/to/file`;do git format-patch "$c^1".."$c" -o patches ; done

因为 format-patch 是单独调用的,所以我会得到所有编号为 0001-commit-text.patch 的补丁,我将丢失补丁的顺序。有没有更好的解决方案

【问题讨论】:

    标签: git format-patch


    【解决方案1】:

    想出下面的shell脚本来实现这一点。欢迎任何其他答案。

    #!/bin/bash
    COUNTER=1;
    for c in `git log --format="%H" --reverse -- path/to/file`;do
        git format-patch --start-number=$COUNTER "$c^1".."$c" -o patches
        let COUNTER=COUNTER+1
    done
    

    【讨论】:

      【解决方案2】:

      你可以试试 git diff initial_commit_id..latest_commit_id > changes.patch

      这是基于这篇文章:https://www.lullabot.com/articles/git-best-practices-upgrading-the-patch-process

      例如:

      git diff ab0b3a55de5..6f5dbbc4187b97
      

      更新: 如果您提供特定文件的路径作为 format-patch 的参数,它只会为对该文件进行更改的提交创建补丁。所以不需要循环和格式补丁的一一调用。 请参阅format-patch for a single file中的示例

      所以试试这个:

      get format-patch from..to -o patches /path/to/file 
      

      【讨论】:

      • 这将给出一个补丁。我想要单独的补丁。
      • 那我猜这里已经回答了:stackoverflow.com/questions/6658313/…
      • 只有当我想要一系列提交时,所有答案才适用。如果我的提交是在日志中的随机点,则不会。
      • 如果您提供特定文件的路径作为 format-patch 的参数,它只会为对该文件进行更改的提交创建补丁。所以不需要循环和格式补丁的一一调用。请参阅stackoverflow.com/questions/7885075/… 中的示例
      【解决方案3】:

      仅使用git,我的轻微即兴创作(只是作曲):

      git format-patch $(git rev-list --max-parents=0 HEAD)..HEAD -- path/to/file
      

      如果rev-list --max-parents 返回多个提交,则可能不起作用。使用该帖子中的想法:

      How to show first commit by 'git log'?

      【讨论】:

        猜你喜欢
        • 2011-12-11
        • 2012-06-27
        • 1970-01-01
        • 2013-05-17
        • 2011-06-02
        • 2023-03-31
        • 2011-06-03
        • 2011-10-03
        相关资源
        最近更新 更多