为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Git
翻过高山走不出你 2019-05-30 16:57:51
git能在空格和制表符之间自动切换吗?我在python程序中使用选项卡进行缩进,但我希望与使用空格的人协作(使用git)。在推送/取时,GIT是否可以在空格和制表符(例如,4空格=1制表符)之间自动转换?(类似于CR/LF转换)
查看完整描述

3 回答

?
月关宝盒

TA贡献1772条经验 获得超5个赞

以下是完整的解决方案:

在存储库中,添加一个文件.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'

OSX

首先用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文件现在都有制表符而不是空格。

编辑*更改强制签出命令。当然,你应该先完成你的工作。


查看完整回答
反对 回复 2019-05-30
?
慕虎7371278

TA贡献1802条经验 获得超4个赞

是的,一个潜在的解决方案是使用Git属性过滤驱动程序(另见Gitpro书),定义污点/清洁机制。


这样:

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

您可以声明此筛选器驱动程序(此处命名为“tabspace‘)在.git/info/attributes(对于应用于Gitrepo中所有文件的筛选器),内容如下:

*.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'

看见奥利维尔回答关于这样一组污点/干净指令的具体工作示例。


查看完整回答
反对 回复 2019-05-30
?
POPMUISE

TA贡献1765条经验 获得超5个赞

对于使用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

我有两个文件: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/

这样,当您最终将工作推到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

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

当然,您也可以编写类似的解决方案,用于从4 space to 2 space如果您想要为我发布的项目做出贡献,并且在开发过程中倾向于使用两个空格,情况就是这样。


查看完整回答
反对 回复 2019-05-30
  • 3 回答
  • 0 关注
  • 1168 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信