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

4

34

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?

Roomy answered 6/8, 2012 at 7:54 Comment(2)
This first question is answered here.Petra
git-ls-files ?Lindquist
L
31

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

Laurettalaurette answered 6/8, 2012 at 9:21 Comment(6)
My msysgit wasn't compiled with USE_LIBPCRE, would this work too git grep -I --files-with-matches --basic-regexp '\r' HEAD (getting no results right now also with '\r\n' )Roomy
Try this: git grep -I --files-with-matches $'\r' HEADLaurettalaurette
This command returns all files in HEAD with *nix ending, or? Would git grep -I --files-with-matches $'\r\n' HEAD deliver the DOS line endings?Roomy
Unix line endings are only \n (LF), Windows line endings are \r\n (CR LF). So the above should find Windows line endings. If it doesn't work, maybe a conversion happens somewhere inside git grep.Laurettalaurette
is there a way to check the remote repository for '\r' vs the local repository? I am running it locally but think its outputing all the files because I have core.autocrlf set to true is my theory (aka converting to windows line endings on my box and committing unix... but i think someone else may have committed windows line endings to the remote)Yentai
@Yentai The command (with HEAD) specifically checks the files in the (remote) repository, not the checked out files. For a specific file you can also run this and you will get the raw contents (e.g. for a README.md): git cat-file -p HEAD:README.md. Look at it with e.g. | less -u and if it has ^M, that means there's \r in the file.Laurettalaurette
M
8

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
Micronutrient answered 6/8, 2012 at 8:28 Comment(0)
A
3

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.

Allot answered 22/12, 2016 at 10:11 Comment(0)
B
2

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.

Basidiospore answered 27/5, 2019 at 16:39 Comment(1)
This does not answer the question "Is there a way to determine the line endings in a existing git repository?"Corollaceous

© 2022 - 2024 — McMap. All rights reserved.