首页 > 解决方案 > 让 git 忽略连续的换行符

问题描述

我想在 git 中只跟踪代码(java),我想让它忽略从先前提交中添加的多个换行符(如果没有其他代码已更改)例如:

我想要这个

class MyApp{
    public static void main(String[] args) {
        if (args.length > 0) {
            for(String s : args){
                system.err.println("arg is "+s+"\n");
                }//end-for
            }//end-if
        }//end-met
    }//end-class

和这个:

class MyApp{


    public static void main(String[] args) {


        if (args.length > 0) {
            for(String s : args){

                system.err.println("arg is "+s+"\n");

                }//end-for
            }//end-if


        }//end-met



}//end-class
    

被视为相同(仅当添加了非换行符时,代码才应视为不同)

这样做的目的是避免在仅添加噪声时复制同一文件(如果我在 git 中搜索文件,我只想在发生“实际”代码更改时获取)

标签: javagitnewlinegit-commit

解决方案


Git 没有提供忽略对跟踪文件的更改的方法。

如果你想在提交之前格式化文本,你可以使用一个干净的过滤器(在属性中指定.gitattributesfilter以你想要的任何方式自动格式化它。gitattributes(7)您可以在手册页中查看如何执行此操作的文档。否则,Git 无法实现您的目标。


推荐阅读