Can git automatically switch between spaces and tabs?

后端 未结 4 1120
离开以前
离开以前 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 05:49

    Very useful info for everyone using GitHub (or other similar service)

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

    Then I have two files: attributes

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

    and attributes2

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

    Working on personal projects

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

    That way, when you finally push your work on github, it won't look silly in the code view with 8 space tabs which is default behavior in all browsers.

    Contributing to other projects

    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
    

    That way you can work with normal tabs on 2 space indented projects.

    Of course you can write similar solution for converting from 4 space to 2 space which is the case if you want to contribute to projects published by me and you tend to use 2 spaces while developing.

提交回复
热议问题