如果Git工具只包含空格中的更改,则可以从分段中删除行 [英] Git tool to remove lines from staging if they consist only of changes in whitespace

查看:77
本文介绍了如果Git工具只包含空格中的更改,则可以从分段中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

消除尾随空白的一点是,如果每个人都这样做,那么你最终会得到一个最小的差异,即。它只包含代码更改,而不是空白的更改。

The point in removing trailing whitespace is that if everyone does it always then you end up with a diff that is minimal, ie. it consists only of code changes and not whitespace changes.

然而,当与其他人不练习此操作时,使用编辑器或预先提交删除所有尾随空白钩子导致一个更糟糕差异。你正在做你的意图的相反。

However when working with other people who do not practice this, removing all trailing whitespace with your editor or a pre-commit hook results in an even worse diff. You are doing the opposite of your intention.

因此,我在这里问是否有一个工具,我可以手动运行之前,我提交暂存,

So I am asking here if there is a tool that I can run manually before I commit that unstages lines from staging that are only changes in whitespace.

另外一个好处是可以改变暂存的行,以便为有代码更改的行删除尾随空白。

Also a bonus would be to change the staged line to have trailing whitespace removed for lines that have code changes.

另外一个好处就是不要对Markdown文件这样做(因为在Markdown中尾随空格有意义)。

Also a bonus would be to not do this to Markdown files (as trailing space has meaning in Markdown).

打算写这个工具,如果它还没有存在的话。

I am asking here as I fully intend to write this tool if it doesn't already exist.

推荐答案

以下将为您介绍大部分的方法:

The following will get you most of the way there:

$ clean=`git diff --cached -b`; \
  git apply --cached <(git diff --cached -R); \
  echo "$clean" | git apply --cached -; \
  clean=

对于1.7.0之前的git版本,如果一个或多个文件发生全空白更改,则失败。例如

For releases of git prior to 1.7.0, it fails if one or more files have all-whitespace changes. For example

$ git diff --cached -b
diff --git a/file1 b/file1
index b2bd1a5..3b18e51 100644
diff --git a/file2 b/file2
new file mode 100644
index 0000000..092bfb9
--- /dev/null
+++ b/file2
[...]

空的delta( file1 上面,这真的应该被压制)使得 git-apply 不高兴:

The empty delta (of file1 above, which really ought to be suppressed) makes git-apply unhappy:

fatal: patch with only garbage at line 3

更新: 1.7.0版本的git 解决了这个问题

UPDATE: The 1.7.0 release of git fixes this issue.

说我们的仓库处于以下状态:

Say our repository is in the following state:

$ git diff --cached
diff --git a/foo b/foo
index 3b18e51..a75018e 100644
--- a/foo
+++ b/foo
@@ -1 +1,2 @@
-hello world
+hello  world
+howdy also

然后我们可以运行上面的命令分叉索引和工作树:

We could then run the above commands to fork the index and work tree:

$ git diff --cached
diff --git a/foo b/foo
index 3b18e51..1715a9b 100644
--- a/foo
+++ b/foo
@@ -1 +1,2 @@
 hello world
+howdy also

$ git diff 
diff --git a/foo b/foo
index 1715a9b..a75018e 100644
--- a/foo
+++ b/foo
@@ -1,2 +1,2 @@
-hello world
+hello  world
 howdy also

如果所有更改均为空格,则会看到

If all changes are whitespace-only, you'll see

error: No changes

我怀疑修复索引并在工作树中留下不希望的更改会让大多数用户感到惊讶甚至恼怒,但这就是问题所要求的行为。

I suspect fixing the index and leaving undesired changes in the work tree would surprise or even irritate most users, but that's the behavior the question asked for.

这篇关于如果Git工具只包含空格中的更改,则可以从分段中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆