如何将 git add patch -p 模式与 diff 的ignore-all-space 结合起来

发布于 2024-11-18 15:16:33 字数 122 浏览 3 评论 0 原文

如何使用补丁模式执行 git add 但忽略空白更改。

该用例适用于您重新格式化文件并对其进行更改的情况。我想首先单独提交真正的代码更改(如 git diff -w 路径所示),然后将重新格式化作为单独的提交提交。

How can I do git add with patch mode but ignoring whitespace changes.

The use case is for when you've reformatted a file and also made changes to it. I want to commit the real code changes separately first (as shown by git diff -w path) and then commit the reformatting as a separate commit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南风几经秋 2024-11-25 15:16:33

以下是相关问题的改编内容。

git diff -w --no-color | git apply --cached --ignore-whitespace

它的优点是您不需要使用stash、临时文件或对工作文件夹执行reset --hard

附录

上面的解决方案仅进行阶段性更改(仅空白编辑除外)。这并没有解决补丁问题,尽管在这种情况下使用 --patch 进行暂存并不直接。

补丁选项 1:在文本编辑器中编辑差异

使用文本编辑器实现此目的的方法有很多种。 Vim 特别适合这一点。

在存储库的根目录中,启动 Vim。

在正常模式下,将差异加载到空缓冲区中...

:r !git diff -w --no-color
:set ft=diff  # if you want syntax highlighting

编辑差异并删除您不想暂存的部分。

要暂存 vim 缓冲区的内容,请运行 vim ex 命令...

:w !git apply --cached --ignore-whitespace

如果您是 Vim 爱好者,您也可以使用可视模式来暂存!

:<',>'w !git apply --cached --ignore-whitespace

您可以使用 ex 命令提交暂存的更改...

:!git commit -m "message"
# or
:!git commit

清除缓冲区,读取未暂存的更改,然后重复

:bd! | set ft=diff | r !git diff -w --no-color

最终,您将只剩下要提交的空白更改。

如果您不使用 Vim,您还可以将 git diff 转储到文件中,编辑该文件,保存,然后将文件输入到 git apply 中。提交并重复直到完成。这有点乏味,但很实用。

补丁选项 2:补丁重置

它是从 git add --patch 向后推出的,但是一旦您使用...暂存非空白更改

git diff -w --no-color | git apply --cached --ignore-whitespace

,您就可以取消暂存块在补丁模式下...

git reset --patch .

请记住,您正在删除要保留的更改。根据需要重复并提交,直到只剩下空白更改。

Here's an adaptation from a related question.

git diff -w --no-color | git apply --cached --ignore-whitespace

It has the benefit that you don't need to use stash, temporary files, or perform a reset --hard on your working folders.

Addendum

The solution above only stages changes except whitespace-only edits. This did not address patch, though using --patch to stage isn't straight forward in this situation.

Patch Option 1: Edit the diff in a text editor

There are many ways to implement this using a text editor. Vim is especially suited to this.

In the root directory of your repository, start Vim.

In normal mode, load the diff into an empty buffer with...

:r !git diff -w --no-color
:set ft=diff  # if you want syntax highlighting

Edit the diff and remove the parts you don't want to stage.

To stage the contents of the vim buffer, run the vim ex command...

:w !git apply --cached --ignore-whitespace

If you're a Vim afficionado, you could use visual mode to stage, too!

:<',>'w !git apply --cached --ignore-whitespace

You can commit the staged changes with the ex command...

:!git commit -m "message"
# or
:!git commit

Clear the buffer, read the unstaged changes, and repeat

:bd! | set ft=diff | r !git diff -w --no-color

Eventually, you'll be left with only whitespace changes to commit.

If you don't use Vim, you could also dump git diff into a file, edit the file, save, then feed the file into git apply. Commit and repeat until done. It's a bit tedious, but functional.

Patch Option 2: Patch reset

It's backwards from git add --patch, but once you've staged non-whitespace changes with...

git diff -w --no-color | git apply --cached --ignore-whitespace

...you can unstage chunks in patch mode with...

git reset --patch .

Keep in mind you're removing the changes that you want to keep staged. Repeat and commit as necessary until you only have whitespace changes left.

油饼 2024-11-25 15:16:33

如果您想执行 git add --patch 但像提问者所要求的那样忽略所有空格,您可以用一个命令执行此操作:

git diff -w --no-color | git apply --cached --ignore-whitespace && git checkout -- . && git reset && git add -p

git diff -w --no-color 创建一个 diff

git apply --cached --ignore-whitespace 应用忽略空白的 diff,并为其建立索引

git checkout -- . 删除未索引的“空白”更改

git reset > 将索引重置为非空白更改

git add -p 在补丁模式下添加非空白更改

将其包装在别名中,如下所示:

alias gwap=“git diff -U0 -w --no -颜色| git apply --cached --ignore-whitespace --unidiff-zero && git checkout - . && git 重置 && 或者,如果你像我一样使用基于UNIX

的系统:(

gwap= !git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero && git checkout -- . && git reset && git add -p 

注意我添加了选项 -U0--unidiff-zero 分别解决上下文匹配问题,根据 此评论。

来源:https://til.hashrocket.com/posts/696df00135-remove-whitespace-changes-then-git-add-p

If you want to do git add --patch but ignore all whitespace like the asker is asking, you can do this in one command:

git diff -w --no-color | git apply --cached --ignore-whitespace && git checkout -- . && git reset && git add -p

git diff -w --no-color creates a diff

git apply --cached --ignore-whitespace applies the diff ignoring whitepace, and indexes it

git checkout -- . removes the unindexed “whitespace” changes

git reset resets the index to just the non-whitespace changes

git add -p adds the non-whitespace changes in patch mode

Wrap this up in an alias, like so:

alias gwap=“git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero && git checkout -- . && git reset && git add -p”

Or if you're on a unix based system like I am:

gwap= !git diff -U0 -w --no-color | git apply --cached --ignore-whitespace --unidiff-zero && git checkout -- . && git reset && git add -p 

(Notice I added options -U0, and --unidiff-zero respectively to workaround context matching issues, according to this comment.)

Source: https://til.hashrocket.com/posts/696df00135-remove-whitespace-changes-then-git-add-p

兮颜 2024-11-25 15:16:33

注意:这个答案已经过时了。六年后,贾斯汀的另一个答案要好得多。更喜欢使用 git apply --cached

我建议简单地往返一个 diff

想法:

git diff --ignore-all-space | (git reset --hard && git apply)

警告:这充满了危险,因为那里有 git reset (它不会保留对二进制文件写入的更改)。也许您想要一个类似于

function cleanup_patch()
{
    if [ $# -lt 1 ]; then 
        echo 'Must provide explicit paths (wildcards allowed)'; 
    else
        git diff --ignore-all-space -- "$@" |
            (git checkout HEAD -- "$@" &&
             git apply)
    fi
}

Afaict 的 bash 函数,因为 diff 看似有用的 --binary 选项不支持空白忽略标志

Note: This answer is old. 6 years down the road, the other answer by Justin is much better. Prefer to use git apply --cached

I suggest simply roundtripping a diff

Idea:

git diff --ignore-all-space | (git reset --hard && git apply)

Warning: this is fraught with danger because of the git reset there (it will not preserve changes to binary files as written). Perhaps you'd want a bash function similar to

function cleanup_patch()
{
    if [ $# -lt 1 ]; then 
        echo 'Must provide explicit paths (wildcards allowed)'; 
    else
        git diff --ignore-all-space -- "$@" |
            (git checkout HEAD -- "$@" &&
             git apply)
    fi
}

Afaict the seemingly useful --binary option to diff doesn't honour the whitespace ignore flags

夜雨飘雪 2024-11-25 15:16:33

@“Justin C”的答案的更强大和通用的版本是:

anw = !git diff -U0 -w --no-color -- \"$@\" | git apply --cached --ignore-whitespace --unidiff-zero "#"
  • 不带任何参数 - 添加所有跟踪文件的非空白更改
  • 给定文件/目录 - 仅在这些位置添加非空白更改

请参阅 此答案了解更多信息。

A more robust and versatile version of @"Justin C"s answer is:

anw = !git diff -U0 -w --no-color -- \"$@\" | git apply --cached --ignore-whitespace --unidiff-zero "#"
  • With no argument - adds all tracked files' non-whitespace changes
  • Given files/directories - only adds non-whitespace changes in those locations

See this answer for more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文