Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Git core.autocrlf不';你不为我工作吗?_Git - Fatal编程技术网

为什么Git core.autocrlf不';你不为我工作吗?

为什么Git core.autocrlf不';你不为我工作吗?,git,Git,这就是我正在做的: $ git init /tmp/repo $ echo -e "1\r\n2" > x.txt $ hexdump x.txt 0000000 31 0d 0a 32 $ git config --local core.autocrlf false $ git add x.txt $ git commit -m 'test' x.txt [master (root-commit) fe8c018] x.txt 1 file changed, 2 insertions(

这就是我正在做的:

$ git init /tmp/repo
$ echo -e "1\r\n2" > x.txt
$ hexdump x.txt
0000000 31 0d 0a 32
$ git config --local core.autocrlf false
$ git add x.txt
$ git commit -m 'test' x.txt
[master (root-commit) fe8c018] x.txt
 1 file changed, 2 insertions(+)
 create mode 100644 x.txt
据我所知,回购协议中的文件现在是
1\r\n2
。现在,如果我签出此代码,文件的内容应该是
1\n2
,对吗?但事实并非如此:

$ git config --global core.autocrlf true
$ git clone file:///tmp/repo r
$ cd r
$ hexdump x.txt
0000000 31 0d 0a 32
为什么它仍然是
\r\n
?是
\n
,对吗

$ git --version
git version 2.14.1

我认为git不会将此文件识别为文本文件,并将其视为二进制文件autocrlf不会影响二进制文件

要将特定文件或扩展名标记为文本,可以添加.gittributes文件,在其中指定要作为文本处理的文件,例如:

*.x text
将告诉git将所有扩展名为“x”的文件视为文本,并应用autocrlf


请参阅本文档,了解我们要做的是在回购协议的根目录中添加一个
.gittributes
文件,其中包含以下内容:

* text eol=lf
*.ttf binary
*.woff binary
*.woff2 binary
第一行是“catch all”,它将所有行的结尾设置为Unix样式-您可以省略
eol
位或将其设置为
crlf
。这会捕获你的.txt文件

并不是说
.gittributes
将优先于单个开发人员在其机器上的git配置,这就是为什么建议使用git配置的原因,尤其是在跨平台工作的情况下


不幸的是,这从未起作用,也从未弄清楚原因:
*-text
作为git配置手册页
core.autocrlf
选项用于将“LF”转换为“CRLF”

$ git init /tmp/repo
Initialized empty Git repository in /private/tmp/repo/.git/
$ cd /tmp/repo
$ echo -ne "1\n2" > x.txt
$ hexdump x.txt
0000000 31 0a 32
0000003
$ git config --local core.autocrlf false
$ git add x.txt
$ git commit -m 'test' x.txt
[master (root-commit) 7ed185b] test
1 file changed, 2 insertions(+)
create mode 100644 x.txt
$ cd ..
$ git config --global core.autocrlf true
$ git clone file:///tmp/repo r
Cloning into 'r'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
$ cd r
$ hexdump x.txt
0000000 31 0d 0a 32
0000004

我按你的建议做了,没有效果。
-text
取消设置
text
属性。@MichałPolitowski tnx,我有一个不同的用例,盲目地从我的文件中复制它。谢谢你发现它!我不确定我的答案是否是超级相关的,因为问题中的扩展现在是“txt”。但也许它对将来探索这一领域的人会有用。