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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2316677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:54:30  来源:igfitidea点击:

Can git automatically switch between spaces and tabs?

gittabsindentation

提问by Olivier Verdier

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

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

Is there a way for git to automatically convert between spaces and tabs (say, 4 spaces = 1 tab) on pushing/fetching? (similar to the CR/LF conversion)

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

采纳答案by Olivier Verdier

Here is the complete solution:

这是完整的解决方案:

In your repository, add a file .git/info/attributeswhich contains:

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

*.py  filter=tabspace

Linux/Unix

Linux/Unix

Now run the commands:

现在运行命令:

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

OS X

操作系统

First install coreutils with brew:

首先使用 brew 安装 coreutils:

brew install coreutils

Now run the commands:

现在运行命令:

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

All systems

所有系统

You may now check out all the files of your project. You can do that with:

您现在可以检出项目的所有文件。你可以这样做:

git checkout HEAD -- **

and all the python files will now have tabs instead of spaces.

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

Edit: changed the forced checkout command. You should commit your work first, of course.

编辑:更改了强制结帐命令。当然,你应该先提交你的工作。

回答by VonC

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

是的,一种潜在的解决方案是使用git 属性过滤器驱动程序(另请参阅GitPro book)来定义污点/清理机制。

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:

您可以tabspace.git/info/attributes(用于应用于 Git 存储库中所有文件的过滤器)中声明此过滤器驱动程序(此处命名为“ ”),内容如下:

*.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 answerfor a concrete working example of such a smudge/clean set of instructions.

有关此类污迹/清洁指令集的具体工作示例,请参阅Olivier回答

回答by simo

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

对使用 GitHub(或其他类似服务)的每个人都非常有用的信息

~/.gitconfig

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

然后我有两个文件: attributes

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

and attributes2

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 tabswhich is default behavior in all browsers.

这样,当你最终将你的工作推送到 github 时,它在代码视图中看起来不会很傻,8 space tabs这是所有浏览器的默认行为。

Contributing to other projects

参与其他项目

mkdir project
cd project
git init
cp ~/path/to/attributes2 .git/info/attributes
git remote add origin [email protected]:some/repo.git
git pull origin branch

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

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

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

当然4 space to 2 space,如果您想为我发布的项目做出贡献,并且在开发时倾向于使用 2 个空格,那么您可以编写类似的转换解决方案。

回答by odyth

If you are on windows then you have a few extra steps to get @Olivier Verdier'ssolution to work.

如果您使用的是 Windows,那么您需要执行一些额外的步骤才能使@Olivier Verdier 的解决方案发挥作用。

  1. Download CoreUtilsfor windows
  2. After installing put the install location in your PATH (How to add a path variable)
  3. I renamed expand.exe to gexpand.exe as there is already a windows expand utility.
  1. 下载适用于 Windows 的CoreUtils
  2. 安装后将安装位置放在 PATH 中(如何添加路径变量
  3. 我将 expand.exe 重命名为 gexpand.exe,因为已经有一个 Windows 扩展实用程序。