git可以在空格和制表符之间自动切换吗?

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文件都有标签而不是空格.

编辑:更改了强制结帐命令.当然,你应该首先承诺你的工作.

  • @ Marc-André好点.我实际上使用coreutils版本.(安装`homebrew`,然后运行`brew install coreutils`). (3认同)
  • 我还发现将属性文件放在 `~/.config/git/attributes` 中并将配置文件放在 `~/.gitconfig` 中更容易,因此这些规则适用于所有项目,而不是弄乱每个项目。 (3认同)
  • @Jeremy:expand/unexpand是unix命令.你要么必须找到Windows端口/等价物,要么使用像[Cygwin]这样的东西(http://www.cygwin.com/) (2认同)
  • 似乎这不再起作用,过滤器对我没有任何作用。结帐后,文件仍有空格。这事有进一步更新吗? (2认同)

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

使用GitHub(或其他类似服务)的每个人都非常有用的信息

~/.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个空格,那么您可以编写类似的转换解决方案.

  • 相关:[将git config存储为存储库的一部分](/sf/ask/1283073501/); 另请注意,您可以在repo中使用(并提交)`.gitattributes`文件 (2认同)