小编典典

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

all

我在 python 程序中使用制表符进行缩进,但我想与使用空格的人合作(使用 git)。

git 有没有办法在推/取时自动在空格和制表符之间转换(例如,4 个空格 = 1 个制表符)?(类似于 CR/LF 转换)


阅读 67

收藏
2022-06-21

共1个答案

小编典典

这是完整的解决方案:

在您的存储库中,添加一个文件.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 文件现在都有制表符而不是空格。

编辑 :更改了强制签出命令。当然,您应该首先提交您的工作。

2022-06-21