Can git automatically switch between spaces and tabs?

后端 未结 4 1118
离开以前
离开以前 2020-11-21 05:39

I use tabs for indentation in my python programs, but I would like to collaborate (using git) with people who use spaces instead.

Is there a way for git to automatic

4条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 06:01

    Yes, one potential solution is to use a git attribute filter driver (see also GitPro book), to define a smudge/clean mechanism.

    alt text

    That way:

    • each time you checkout some files of your repo, spaces can be converted in tabs,
    • but when you check-in (and push and publish), those same files are stored back using only spaces.

    You can declare this filter driver (named here 'tabspace') in the .git/info/attributes (for a filter applied to all files within the Git repo), with the following content:

    *.py  filter=tabspace
    

    Now run the commands:

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

    See Olivier's answer for a concrete working example of such a smudge/clean set of instructions.

提交回复
热议问题