代码之家  ›  专栏  ›  技术社区  ›  Olivier Verdier

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

  •  180
  • Olivier Verdier  · 技术社区  · 15 年前

    4 回复  |  直到 13 年前
        1
  •  203
  •   Marco de Jongh    11 年前

    在存储库中,添加一个文件 .git/info/attributes 其中包括:

    *.py  filter=tabspace
    

    Linux/Unix

    现在运行以下命令:

    git config --global filter.tabspace.smudge 'unexpand --tabs=4 --first-only'
    git config --global filter.tabspace.clean 'expand --tabs=4 --initial'
    

    首先使用brew安装coreutils:

    brew install coreutils
    

    现在运行以下命令:

    git config --global filter.tabspace.smudge 'gunexpand --tabs=4 --first-only'
    git config --global filter.tabspace.clean 'gexpand --tabs=4 --initial'
    

    所有系统

    您现在可以签出项目的所有文件。您可以通过以下方式实现:

    git checkout HEAD -- **
    

    所有python文件现在都将有制表符而不是空格。

        2
  •  146
  •   Community CDub    8 年前

    是的,一个潜在的解决方案是使用 git attribute filter driver (另见 GitPro book ),以定义涂抹/清除机制。

    alt text

    这样:

    • 每次签出回购协议的某些文件时,可以在选项卡中转换空间,
    • 但是,当您签入(以及推送和发布)时,这些相同的文件仅使用空格存储。

    tabspace .git/info/attributes (对于应用于Git repo中所有文件的筛选器),包含以下内容:

    *.py  filter=tabspace
    

    现在运行以下命令:

    # local config for the current repo
    git config filter.tabspace.smudge 'script_to_make_tabs'
    git config filter.tabspace.clean 'script_to_make_spaces'
    

    Olivier answer 有关此类污迹/清洁说明集的具体工作示例。

        3
  •  40
  •   Smuuf    11 年前

    ~/.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
    

    attributes

    *.js  filter=tabspace
    *.html  filter=tabspace
    *.css  filter=tabspace
    *.json  filter=tabspace
    

    attributes2

    *.js  filter=tabspace2
    *.html  filter=tabspace2
    *.css  filter=tabspace2
    *.json  filter=tabspace2
    

    从事个人项目

    mkdir project
    cd project
    git init
    cp ~/path/to/attributes .git/info/
    

    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
    

    这样,您就可以使用上的普通选项卡 2 space indented 项目。

    当然,您可以编写类似的解决方案来从 4 space to 2 space

        4
  •  1
  •   odyth    7 年前

    如果您使用的是windows,则需要执行一些额外的步骤 @Olivier Verdier's 工作的解决方案。

    1. 下载 CoreUtils
    2. 安装后,将安装位置放在您的路径中( How to add a path variable )
    推荐文章