为什么`git blame -w`不会忽略换行符?

时间:2017-05-26 16:15:42

标签: git

所以,如果用换行符进行更改 changeset

$ git blame -w -L70,71 file

$ git blame -w -M -L70,71 file

上面的这两个命令给出了完全相同的输出:

523cdb67 (Nick Mikhno 2017-05-26 18:00:13 +0300 70)                     <span id="contactPhoneNumberError"
523cdb67 (Nick Mikhno 2017-05-26 18:00:13 +0300 71)                     class="errorSpan"></span>

例如,git责备我更改行但我需要忽略git注释中的换行符 我希望看到以前的用户实际更改了代码并忽略代码重新格式化

根据$ git blame --help

-w
    Ignore whitespace when comparing the parent’s version and the child’s to find where the lines came from.


-M|<num>|
    Detect moved or copied lines within a file. When a commit moves or copies a block of lines (e.g. the original file has A and then B, and the commit changes it to B and then A), the traditional blame algorithm notices only half of the movement and typically blames the lines that were moved up (i.e. B) to the parent and assigns blame to the lines that were moved down (i.e. A) to the child commit. With this option, both groups of lines are blamed on the parent by running extra passes of inspection.

    <num> is optional but it is the lower bound on the number of alphanumeric characters that Git must detect as moving/copying within a file for it to associate those lines with the parent commit. The default value is 20.

请告诉我,我错了什么?

2 个答案:

答案 0 :(得分:1)

让我们说你有文件:

Hello world!

然后运行

> git blame hello.txt
^dd5c453 (User1 2017-05-26 13:50:54 -0700 1) Hello world!

现在User2出现并添加了额外的空格

> git blame hello.txt
fdd9abbe (User2 2017-05-26 13:51:50 -0700 1) Hello     world!

> git blame -w hello.txt
^dd5c453 (User1 2017-05-26 13:50:54 -0700 1) Hello     world!

您可以看到使用-w标志忽略User2添加的空格。问题是每条线都是针对blame命令单独评估的。这意味着即使我们使用-w,拆分或连接行也会改变被指责的用户。

> git blame -w hello.txt
c0c338a5 (User3 2017-05-26 13:55:52 -0700 1) Hello
c0c338a5 (User3 2017-05-26 13:55:52 -0700 2) world!

-M标志的情况相同。分割线不被认为是移动,因为git将组合线看作来自两条分割线的不同数据。您可以将其视为删除组合线并添加两个新线。

所以如果责备不起作用,你如何找到原作者?我经常使用git log --follow -p hello.txt。这将显示每次更改hello.txt的提交以及每次更改的完整差异。

commit c0c338a5bdd99c2edc24541da0a9262d654e9962
Author: User3 <user3@example.com>
Date:   Fri May 26 13:55:52 2017 -0700

    commit3

diff --git a/hello.txt b/hello.txt
index 84a0ce0..b85a64c 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1,2 @@
-Hello     world!
+Hello
+world!

commit fdd9abbe78fdcbbea3f4acf812e2f5e9867e33f1
Author: User2 <user2@example.com>
Date:   Fri May 26 13:51:50 2017 -0700

    commit 2

diff --git a/hello.txt b/hello.txt
index cd08755..84a0ce0 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1 +1 @@
-Hello world!
+Hello     world!

commit dd5c453d7788bedc05eb5665b2dc5454d7a83291
Author: User1 <user1@example.com>
Date:   Fri May 26 13:50:54 2017 -0700

    Commit 1

diff --git a/hello.txt b/hello.txt
new file mode 100644
index 0000000..cd08755
--- /dev/null
+++ b/hello.txt
@@ -0,0 +1 @@
+Hello world!

答案 1 :(得分:0)

看起来不像“代码重新格式化”。你有一行,比如2个单词,在修订版X后你有两行,一行有第一个词,第二行有第二个词。它是两条与原始行不同(内容)的行。因此,即使使用-w,您也可以在同一版本中添加这两行。 -w并不意味着不会考虑新的换行符。这与EOL格式的变化(windows,UNIX)并没有被视为差异。

相关问题