Is there a way to determine the line endings in a

2020-02-10 02:41发布

Is there a way to determine the line endings in a existing git repository?

If I clone a existing repository how do I determine what core.autocrlf was used by the creator?

I'm still uncertain whats the best setting for core.autocrlf e.g on a windows box (since there are multiple opinions: Distributing git configuration with the code or https://help.github.com/articles/dealing-with-line-endings)

Bonus question: Can you determine on windows (with standard tools) if a repo has mixed line endings (by wrong core.autocrlf setting) through all commits?

4条回答
Viruses.
2楼-- · 2020-02-10 02:57

To check what line endings were actually committed in the repository (regardless of your core.autocrlf setting), try the following:

git grep -I --files-with-matches --perl-regexp '\r' HEAD

(-I means that binary files should not be looked at.)

查看更多
来,给爷笑一个
3楼-- · 2020-02-10 03:09

The best line endings practice is here: https://stackoverflow.com/a/10855862

To determine the line endings in a existing git repository:

  1. Set core.autocrlf to false, so it will not change file endings while transmitting files.
  2. git clone your repo for ex. in a new directory.
  3. Use the text editor that shows line endings to open the files (for ex. PHPStorm does). You should open several files as line endings may differ from file to file.

You can't determine what core.autocrlf was used by the creator as it is local config except the repo has .gitattributes file.

On Windows if you are not using .gitattributes just use core.autocrlf true as it set by default.

查看更多
神经病院院长
4楼-- · 2020-02-10 03:10

In Windows, just run the below command from the command prompt:

git config --list

This will list all the git configuration variables.

If you want to get the individual config setting (e.g. for core.autocrlf), run the following command on the windows command prompt:

git config --get core.autocrlf

This will either give you a "true" OR "false" value.

IF you wish to change this, edit C:\ProgramData\Git\config file, and change the value from false to true

Note: This only applies for Windows operating systems.

查看更多
【Aperson】
5楼-- · 2020-02-10 03:21

I would still maintain that setting (core.autocrlf) to false, as I explain in " Distributing git configuration with the code" that you mention, and uses eol gitattributes directive for a more fine-grained control.

That being said, to detect a mixed line endings:

  • set core.autocrlf to true
  • git clone your repo
  • git diff: if diffs are visible just after your clone... some automatic eol conversions just took place in the working tree.

Update 2016 (4 years later): a more modern way to detect eol changes:

 git -c color.diff.whitespace="red reverse" diff -R -- afile
查看更多
登录 后发表回答