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

18.5k Views Asked by At

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

There are 4 best solutions below

0
On

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.

0
On

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

I mentioned in "git status shows files as changed even though contents are the same" that git ls-files --eol is a good approach. (Git 2.8+)

Make it possible to let Git show the line endings in the index and in the working tree and the effective text/eol attributes.

The end of line ("eolinfo") are shown like this:

"-text"        binary (or with bare CR) file
"none"         text file without any EOL
"lf"           text file with LF
"crlf"         text file with CRLF
"mixed"        text file with mixed line endings.

Example:

i/none   w/none   attr/text=auto      t/t5100/empty
i/-text  w/-text  attr/-text          t/test-binary-2.png
i/lf     w/lf     attr/text eol=lf    t/t5100/rfc2047-info-0007
i/lf     w/crlf   attr/text eol=crlf  doit.bat
i/mixed  w/mixed  attr/               locale/XX.po

That being said:

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
6
On

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.)

1
On

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

git ls-files --eol

# for a given file:
git -c color.diff.whitespace="red reverse" diff -R -- afile

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.