Oli*_*ier 192 git tabs indentation
我在我的python程序中使用制表符缩进,但我想与使用空格的人合作(使用git).
有没有办法让git在推/取时自动在空格和制表符之间进行转换(例如,4个空格= 1个制表符)?(类似于CR/LF转换)
Oli*_*ier 190
这是完整的解决方案:
在您的存储库中,添加.git/info/attributes包含以下内容的文件:
*.py filter=tabspace
Run Code Online (Sandbox Code Playgroud)
的Linux/Unix
现在运行命令:
git config --global filter.tabspace.smudge 'unexpand --tabs=4 --first-only'
git config --global filter.tabspace.clean 'expand --tabs=4 --initial'
Run Code Online (Sandbox Code Playgroud)
OS X.
首先使用brew安装coreutils:
brew install coreutils
Run Code Online (Sandbox Code Playgroud)
现在运行命令:
git config --global filter.tabspace.smudge 'gunexpand --tabs=4 --first-only'
git config --global filter.tabspace.clean 'gexpand --tabs=4 --initial'
Run Code Online (Sandbox Code Playgroud)
所有系统
您现在可以查看项目的所有文件.你可以这样做:
git checkout HEAD -- **
Run Code Online (Sandbox Code Playgroud)
现在所有的python文件都有标签而不是空格.
编辑:更改了强制结帐命令.当然,你应该首先承诺你的工作.
Von*_*onC 134
是的,一个可能的解决方案是使用git属性过滤器驱动程序(另请参阅GitPro书籍)来定义涂抹/清理机制.

那样:
您可以tabspace在.git/info/attributes(对于应用于Git存储库中的所有文件的过滤器)中声明此过滤器驱动程序(在此处命名为' ),其中包含以下内容:
*.py filter=tabspace
Run Code Online (Sandbox Code Playgroud)
现在运行命令:
# local config for the current repo
git config filter.tabspace.smudge 'script_to_make_tabs'
git config filter.tabspace.clean 'script_to_make_spaces'
Run Code Online (Sandbox Code Playgroud)
请参阅Olivier的答案,了解这种涂抹/清洁指令的具体实例.
sim*_*imo 38
~/.gitconfig
[filter "tabspace"]
smudge = unexpand --tabs=4 --first-only
clean = expand --tabs=4 --initial
[filter "tabspace2"]
smudge = unexpand --tabs=2 --first-only
clean = expand --tabs=2 --initial
Run Code Online (Sandbox Code Playgroud)
然后我有两个文件:
attributes
*.js filter=tabspace
*.html filter=tabspace
*.css filter=tabspace
*.json filter=tabspace
Run Code Online (Sandbox Code Playgroud)
和 attributes2
*.js filter=tabspace2
*.html filter=tabspace2
*.css filter=tabspace2
*.json filter=tabspace2
Run Code Online (Sandbox Code Playgroud)
mkdir project
cd project
git init
cp ~/path/to/attributes .git/info/
Run Code Online (Sandbox Code Playgroud)
这样,当你最终在github上推动你的工作时,8 space tabs在所有浏览器中默认行为的代码视图中都不会显得愚蠢.
mkdir project
cd project
git init
cp ~/path/to/attributes2 .git/info/attributes
git remote add origin git@github.com:some/repo.git
git pull origin branch
Run Code Online (Sandbox Code Playgroud)
这样您就可以使用2 space indented项目的常规选项卡.
当然4 space to 2 space,如果您想为我发布的项目做出贡献,并且您在开发过程中倾向于使用2个空格,那么您可以编写类似的转换解决方案.