你如何让Git忽略空格和制表符? [英] How do you make Git ignore spaces and tabs?

查看:1497
本文介绍了你如何让Git忽略空格和制表符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小型脚本项目,由五个不同的源文件组成,名为Droid XX-XX-XX。每次创建源目录的新备份副本时,我都会将日期放在X中。所以在不同的日期有大约15个不同的版本。我想从最早开始就将这些添加到我的裸新版Git仓库中。



然而,我遇到了几个问题。


  1. 一个问题是,某些文件使用制表符缩进,而另一些文件使用空格 - 但Git将整行因为即使唯一的区别是标签与空间问题不同。如何让Git忽略缩进格式?

  2. 另外一个问题是,有些文件名不会有空格,而另外一些文字之间有空格 - 但Git会将它们对待作为不同的文件。更糟糕的是,有时文件名被改成了不同的东西(比如PatrolPlan改成了Patrol),没有任何真正的原因。当我添加一组新文件时,如何告诉Git即使文件名不同,它实际上只是某个较旧文件的新版本?或者更好的办法是,我可以将它设置为自动检测吗?合并为一个,或者将一个合并为两个 - 但Git不会自动检测相似之处并推断发生了什么。我怎么能告诉Git发生了什么?或者更好的是,如何将它设置为在两个源文件合并或分裂时自动检测?

我意识到问题(2)和(3)高度相关。感谢您的任何帮助!

解决方案

听起来你需要更多的控制和开发过程的标准化。提交更改的人应该是修改文件的同一个人。或者至少提交者应该知道发生了什么变化。

仔细检查 git diff 的输出,并使用 -w 标志忽略空格。还可以选择显示一行内的差异。请参阅下面的行内差异



请注意,您不能告诉git在提交时跳过空间更改。我建议使用GitX(我更喜欢brotherbardfork),它允许您在提交之前交互地丢弃hunk。



提交时使用描述性消息。例如,如果一个文件被分割,就这样说。让你的承诺很小。如果你发现你自己写了很长的提交消息,把提交分成更小的部分。通过这种方式,很长一段时间后检查日志时,它会更有意义地发生更改。



行内差异



Git有能力在单行中显示单词差异。最简单的方法是使用 git diff --color-words

然而,我喜欢自定义含义使用 diff.wordRegex config的单词。我还喜欢 plain word-diff格式,因为它更清楚地显示了差异所在的位置(除了使用颜色外,还在括号内插入了更改)。



命令:

  git diff --word-diff = plain 
code>

以及这个在我的配置中:

  [diff] 
wordRegex = [[:alnum:] _] + | [^ [:alnum:] _ [:space:]] +

这个正则表达式将它们当作单词:


  • 连续字符串字母数字和下划线连接字符串
  • 非字母数字,非下划线和非空格的连续字符串(适用于检测运算符)


您必须使用最近版本的 git 才能使用 wordRegex 。查看您的 git-config 手册页,查看是否列出了该选项。 >



如果您使用 git mv 重命名文件(这比使用其他工具或操作系统更适合重命名),你可以看到git检测到重命名。我强烈建议独立于文件内容的任何编辑提交重命名。这是因为git实际上并不存储你重命名的事实 - 它使用基于文件已经改变多少的启发式来猜测它是否是相同的文件。您在重命名提交期间更改的越少越好。



如果您确实稍微更改了文件内容,则可以使用 -C param to git diff git log 可以更加努力地检测副本和重命名。添加一个百分比(例如 -C75%)使git对差异更加宽大。百分比表示内容与匹配的相似程度。

I have a small scripting project that consists of five different source files in one directory called "Droid XX-XX-XX". Each time I created a new backup copy of the source directory, I put the date in the X's. So there are about 15 different versions from different dates. I want to add each of these to my bare new Git repository starting from the earliest.

However I have run into several problems.

  1. One problem is that some of the files use tabs for indentation, while others use spaces -- but Git treats a whole line as different even when the only difference is the tab vs. space issue. How can I make Git ignore indentation formatting?

  2. Another problem is that some filenames would have no spaces while others had spaces between the words -- but Git treats them as different files. Worse, sometimes the filename was changed to something different (like "PatrolPlan" changed to just "Patrol") for no real reason. When I'm adding a new set of files, how can I tell Git that even though the filename is different, it's really just a new version of a certain older file? Or better yet, can I set it to auto-detect when this happens?

  3. The last problem is that at certain points during development, we merged two source files into one, or split one into two -- but Git doesn't automatically detect the similarities and deduce what happened. How can I tell Git what happened? Or better yet, how can I set it to auto-detect when two source files were combined or when one was split up?

I realize questions (2) and (3) are highly related. Thanks for any assistance!

解决方案

It's sounding like you need more control and standardization of the development process. The one who commits changes should be the same person who modifies the files. Or at least the committer should know exactly what changed.

Examine carefully the output of git diff, and use the -w flag to ignore spaces. There's also options to show differences within a line. See Diffs within a line below.

Note that you won't be able to tell git to skip the space changes when committing. I suggest using GitX (I prefer the "brotherbard" fork), which allows you to interactively discard hunks before committing.

Use descriptive messages when committing. For example, if a file was split, say so. Make your commits small. If you find yourself writing long commit messages, break up the commit into smaller parts. That way when you examine the logs a long time later, it will make more sense what changed.

Diffs within a line

Git has some ability to show "word" differences in a single line. The simplest way is to just use git diff --color-words.

However, I like customizing the meaning of a "word" using the diff.wordRegex config. I also like the plain word-diff format because it more clearly shows where the differences are (inserts brackets around the changes in addition to using color).

Command:

git diff --word-diff=plain

along with this in my config:

[diff]
        wordRegex = [[:alnum:]_]+|[^[:alnum:]_[:space:]]+

This regex treats these as "words":

  • consecutive strings of alphanumerics and underscores
  • consecutive strings of non-alphanumerics, non-underscores, and non-spaces (good for detecting operators)

You must have a recent version of git to use wordRegex. See your git-config man page to see if the option is listed.

UPDATE

If you use git mv to rename a file (which is preferable to using another tool or the OS to rename), you can see git detecting the rename. I highly recommend committing a rename independently of any edits to the contents of the file. That's because git doesn't actually store the fact that you renamed - it uses a heuristic based on how much the file has changed to guess whether it was the same file. The less you change it during the rename-commit, the better.

If you did change the file contents slightly, you can use -C param to git diff and git log to try harder to detect copies and renames. Add a percentage (e.g. -C75%) to make git more lenient about differences. The percent represents how similar the contents have to be to be considered a match.

这篇关于你如何让Git忽略空格和制表符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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