翱翔天空3.3 git 使用git attributes将代码缩进由制表符修改为空格
使用空格做缩进而非制表符,这样代码更容易阅读。那么假如git库中的代码是以制表符tab缩进的,本地想用空格查看,该如何做?
1 可以使用以前介绍的replace工具
2使用emacs打开某文件,然后M-x untabity
但这两种做法都需要手工操作,有没有自动的方法呢?
有的,就是attributes
方法如下,
1 在项目的.git/config文件中加入如下配置
[filter "tabspace"]
;clean = unexpand --tabs=2 --first-only ;; commit
smudge = expand --tabs=2 --initial ;; checkout
其中smudge指定在检出时运行的命令,clean指定提交时的。上述配置会将每行起始处的每个tab替换为两个空格。
上述clean配置在提交时做相反的转换,我一般都将clean行注释掉,甚至设为和smudge的命令一样为expand,以便保证提交的代码没有起始tab.
2 创建attributes文件,指定使用上述配置的文件类型
在.git/info/目录下创建attributes文件,文件内容如下
*.c filter=tabspace
*.cc filter=tabspace
*.h filter=tabspace
*.hpp filter=tabspace
*.cpp filter=tabspace
*.asm filter=tabspace
这样c源码文件就都会使用config中的"tabspace "配置,检出代码时将tab转换为空格。
配置好后,以后所有的代码都是空格缩进了。很方便吧,赶快试试吧!