如何将 git add patch -p 模式与 diff 的ignore-all-space 结合起来
如何使用补丁模式执行 git add 但忽略空白更改。
该用例适用于您重新格式化文件并对其进行更改的情况。我想首先单独提交真正的代码更改(如 git diff -w 路径所示),然后将重新格式化作为单独的提交提交。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下是相关问题的改编内容。
它的优点是您不需要使用
stash
、临时文件或对工作文件夹执行reset --hard
。附录
上面的解决方案仅进行阶段性更改(仅空白编辑除外)。这并没有解决补丁问题,尽管在这种情况下使用
--patch
进行暂存并不直接。补丁选项 1:在文本编辑器中编辑差异
使用文本编辑器实现此目的的方法有很多种。 Vim 特别适合这一点。
在存储库的根目录中,启动 Vim。
在正常模式下,将差异加载到空缓冲区中...
编辑差异并删除您不想暂存的部分。
要暂存 vim 缓冲区的内容,请运行 vim ex 命令...
如果您是 Vim 爱好者,您也可以使用可视模式来暂存!
您可以使用 ex 命令提交暂存的更改...
清除缓冲区,读取未暂存的更改,然后重复
最终,您将只剩下要提交的空白更改。
如果您不使用 Vim,您还可以将 git diff 转储到文件中,编辑该文件,保存,然后将文件输入到 git apply 中。提交并重复直到完成。这有点乏味,但很实用。
补丁选项 2:补丁重置
它是从
git add --patch
向后推出的,但是一旦您使用...暂存非空白更改,您就可以取消暂存块在补丁模式下...
请记住,您正在删除要保留的更改。根据需要重复并提交,直到只剩下空白更改。
Here's an adaptation from a related question.
It has the benefit that you don't need to use
stash
, temporary files, or perform areset --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...
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...
If you're a Vim afficionado, you could use visual mode to stage, too!
You can commit the staged changes with the ex command...
Clear the buffer, read the unstaged changes, and repeat
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 intogit 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......you can unstage chunks in patch mode with...
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.
如果您想执行 git add --patch 但像提问者所要求的那样忽略所有空格,您可以用一个命令执行此操作:
git diff -w --no-color
创建一个 diffgit 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
的系统:(
注意我添加了选项
-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
creates a diffgit apply --cached --ignore-whitespace
applies the diff ignoring whitepace, and indexes itgit checkout -- .
removes the unindexed “whitespace” changesgit reset
resets the index to just the non-whitespace changesgit add -p
adds the non-whitespace changes in patch modeWrap 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:
(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
我建议简单地往返一个 diff
想法:
警告:这充满了危险,因为那里有 git reset (它不会保留对二进制文件写入的更改)。也许您想要一个类似于
Afaict 的 bash 函数,因为 diff 看似有用的
--binary
选项不支持空白忽略标志I suggest simply roundtripping a diff
Idea:
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 toAfaict the seemingly useful
--binary
option to diff doesn't honour the whitespace ignore flags@“Justin C”的答案的更强大和通用的版本是:
请参阅 此答案了解更多信息。
A more robust and versatile version of @"Justin C"s answer is:
See this answer for more.