git - 如何在 git add 交互式补丁模式中获得更好的帅哥?

标签 git diff patch interactive git-add

我经常用 git add -p somefile以交互方式仅暂存给定文件中的部分。但是,如果文件有更复杂的更改,则默认差异会出错,交互式补丁命令提供的大块也会出错。
git diff命令有许多选项来改进或自定义差异输出,包括非常有用的 --anchored=<text> ,但是有什么办法可以从 git add -p 获得更好的帅哥吗? ?

最佳答案

您可以尝试以下操作:

  • 定义一个 custom hunk header如上所述适合您的情况 here
  • 尝试不同的 diff algorithm通过将其作为独立配置参数传递给 git add -p如前所述 here
    git -c diff.algorithm=<algo-name> add -p
    
    git docs 中可用的差异算法,

  • -diff-algorithm={patience|minimal|histogram|myers}


    Choose a diff algorithm. The variants are as follows:

    default, myers The basic greedy diff algorithm. Currently, this is the default.

    minimal Spend extra time to make sure the smallest possible diff is produced.

    patience Use "patience diff" algorithm when generating patches.

    histogram This algorithm extends the patience algorithm to "support low-occurrence common elements".


  • 使用 git-gui手动选择您希望提交的行/大块,如前所述 herehere (见下方工具截图)

  • git-gui
  • Modify the hunk's size本身
  • 设置这个 diff.indentHeuristic 范围
    git -c diff.indentHeuristic=true add -p
    

  • 来自 git docs ,

    diff.indentHeuristic
    Set this option to true to enable experimental heuristics that shift diff hunk boundaries to make patches easier to read.


    但是,基于 this

    With Git 2.25 (Q1 2020), you don't even have to specify --indent-heuristic anymore (since it is the default for quite some times now).


    , 这个
    参数默认设置(为真)。所以可能,尝试将其设置为 false 如果
    完全有帮助。

    关于git - 如何在 git add 交互式补丁模式中获得更好的帅哥?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59270198/

    相关文章:

    两个文件内容之间的差异

    arrays - kustomize 修补特定容器而不是通过数组 (/containers/0)

    mysql - 我如何在一个 Joomla 项目上与多个开发人员协作?

    Git blame 文件中的作者列表

    linux - 使用md5从目录中删除相同的二进制文件

    tortoisesvn - 使用Meld作为Tortoise SVN的外部差异工具

    python - 使用 matplotlib 中的多个补丁裁剪图像

    http - 在 Nativescript 应用程序中向服务器发送 http.patch 请求

    git - 我可以从 git 中检索给定日期的文件版本吗?

    通过 SAML SSO 进行 Git CLI 验证