Correct setting for git autocrlf as per use case

只谈情不闲聊 提交于 2019-11-29 14:38:36

问题


I was searching for the proper setting to be used as per certain use cases but could not find any source describing the same. Therefore, I am asking this question to serve as a solution to anyone looking for the correct setting for git's autocrlf option.

Use Case 1: I am on Mac, the other developers are all on Windows. They are managing the source code before I joined in.

Use Case 2: I am on Windows, the other developers are all on Mac. They are managing the source code before I joined in.

Use Case 3: I am on Linux, the other developers are all on Windows. They are managing the source code before I joined in.

Use Case 4: I am on Windows, the other developers are all on linux. They are managing the source code before I joined in.

Use Case 5: I am on Linux, the other developers are all on Mac. They are managing the source code before I joined in.

Use Case 6: I am on Mac, the other developers are all on Linux. They are managing the source code before I joined in.

What setting of git core.autocrlf should I be using ?

EDIT: Why this question is not a duplicate to many similar questions:

All other questions and their answers provide the required facts and knowledge that leaves a lot to be done by the reader. This question aims at asking the specific answer to specific scenarios.


回答1:


Simple:

 git config core.autocrlf false

(for all and any scenario)

core.autocrlf is a config, which means it is not pushed or cloned with the repo: it has to be set by the user.

It was the legacy way to handle eol at the repo level.

What you want to use (add or modify depending on your scenario) are gitattributes core.eol directives.

  • .gitattributes is a file which can be managed in the git repo like any other file. Once you agree on an eol policy, that policy will be enforced at each clone.
  • you can set core.eol directives for a file, or group of files if you want (as opposed to the global repository-wide config core.autocrlf)

For heterogeneous environment, the core.eol (only for the files you deem problematic) should be native (if you suspect your editor insist on using the system eol instead of using the one already present in the file).

For more, see "Mind the End of Your Line".




回答2:


Don't worry about core.autocrlf; add a .gitattributes file in the root directory and put the following line in:

* text=auto

This will cause git to automatically convert all line endings to LF on commit, and (for Windows machines) convert line endings to CRLF on checkout.

Note that for use case 1 and 3, where the existing developers are on Windows, adding this line to .gitattributes may make it look like you all of a sudden have a ton of changes to commit. That is because you have a lot of files with CRLF in them in the repository, and git knows it has to convert them all to LF. Go ahead and commit those changes, even though it looks like nothing has really changed.

One other thing to note is that if everyone is on the same OS, then I wouldn't even mess around with converting line endings; in that case, put the line * -text in your .gitattributes file so that no conversion happens.



来源:https://stackoverflow.com/questions/35104293/correct-setting-for-git-autocrlf-as-per-use-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!