Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git 之 文件换行符 #3

Open
alwaystest opened this issue Sep 27, 2015 · 0 comments
Open

git 之 文件换行符 #3

alwaystest opened this issue Sep 27, 2015 · 0 comments

Comments

@alwaystest
Copy link
Owner

git 之 文件换行符

标签(空格分隔): git 换行 intellij


下午搞了一下午测试,发现总是在提交的时候提示自动把换行符从lf转换为crlf,真是奇怪,明明把git config --global core.autocrlf 设为false了。
查阅了资料,看到.gitattributes文件里面可以设定对文件换行符的操作,但是那篇文章说的有点矛盾,上面说.gitattributes 文件里面的设置会覆盖掉global的设置,下面给的模板里面的英文说明是没有指定global属性的时候生效。
只能自己做测试了。
0927测试记录

  1. 设置git config --global core.autocrlffalse
  2. 提交一个lf格式的中英文混合多行文本文件。
  3. 修改文件,提交git,可以正常检测出哪行发生了变化。notepad++软件里面打开文件,换行符还是lf。
  4. 提交多次修改之后,git reset head^,发现文件换行符还是lf。
  5. 用notepad++手动把换行符修改为crlf,提交git,git提示全部内容都被修改。
  6. 再次修改文件内容,提交git,git能够正确检测变化的行。
  7. 使用git reset --hard head^^恢复到换行符是lf的commit,换行符是会成lf的。
  8. 最后发现,在intellij里面使用图形化界面的git来切换branch,切换完以后换行符会全部被设为crlf
  9. github for windows切换分支后也会默认把文件换行符换为crlf。

20150928更新(这次使用windows 下的 git bash)

添加测试记录:

  1. 新建一个git repo,此时core.autocrlf == false
  2. 添加lf换行的txt文件,正常修改,add,commit,新建branch,切换branch。换行符没有变化。
  3. 手动用notepad++把换行符切换成crlf,提交,commit,reset --hard,切换branch。发现commit是什么换行符,切换过去就是什么换行符,只是切换换行符的时候diff表示文件全部被更改。很正常呀。

但是打砖块的代码切换的时候就总是自动把换行符切换成crlf

还是看看有哪里配置不一样吧。

经过排查,工程文件夹下有一个.gitattributes 的文件,不想挨个排查里面的属性了,直接删除文件,另外执行git rm .gitattributes,提交,这次把文件换行符手动转换成lf然后提交,然后reset,切换branch,终于正常了。

删除了.gitattributes文件后,intellij和github for windows的切换branch也不会更改换行符了。

所以还是.gitattributes里面的设置可以覆盖全局设置,猜测发挥效果的命令是* text=auto

.gitattributes 文件内容附在结尾。

注意:

0927晚上做测试用的是cygwin里面的git

我下午执行git各种命令的时候是用的windows下的gitbash,reset,checkout的时候也是各种错乱,尝试用网上重建index的方法修改repo中的换行设置,但是还是不行。

另外最近刚使用intellij,不知道哪些文件是需要版本控制的,搞的一团糟。

目前gitignore的配置是 .idea *.iml 都不做版本控制,目前切换branch的时候还能正常工作。先这样吧,以后慢慢改。

使用git的时候,进入cygwin,不能在intellij里面做操作。

顺带一提,intellij里面有一个文件是控制文件编码方式的,哪个使用utf-8是在里面注明的,晚上测试的时候发现有的文件一切换branch,编码就变gbk了,在intellij的默认编码设置里面全部改成utf-8就行了。

.gitattributes 文件内容

# Auto detect text files and perform LF normalization
* text=auto

# Custom for Visual Studio
*.cs     diff=csharp
*.sln    merge=union
*.csproj merge=union
*.vbproj merge=union
*.fsproj merge=union
*.dbproj merge=union

# Standard to msysgit
*.doc    diff=astextplain
*.DOC    diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot  diff=astextplain
*.DOT  diff=astextplain
*.pdf  diff=astextplain
*.PDF    diff=astextplain
*.rtf    diff=astextplain
*.RTF    diff=astextplain

参考:

https://help.github.com/articles/dealing-with-line-endings/#platform-windows
cssmagic/blog#22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant