Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 270
U. N. Owen Avatar asked Aug 06 '12 07:08

U. N. Owen


People also ask

How do I see line endings in github?

To tell what line endings a file in the repository is using, use git show to extract the file's contents. This will give you the contents without changing the line endings.

Does git store line endings?

Git does not store "neutralized lines". So yes, Git can save files with mixed line endings, but that's usually a bad practice to avoid.

Does git use LF or CRLF?

Git doesn't expect you to use unix-style LF under Windows. The warning "CRLF will be replaced by LF" says that you (having autocrlf = input ) will lose your windows-style CRLF after a commit-checkout cycle (it will be replaced by unix-style LF). Don't use input under Windows.

How do I fix git line endings?

fix-git-line-endings # With the exception that we are forcing LF instead of converting to windows style. #Set LF as your line ending default. #Save your current files in Git, so that none of your work is lost. #Remove the index and force Git to rescan the working directory.


2 Answers

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

like image 184
robinst Avatar answered Sep 18 '22 17:09

robinst


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 
like image 22
VonC Avatar answered Sep 20 '22 17:09

VonC