<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Kevin Shu</title>
    <description>The latest articles on Forem by Kevin Shu (@kevinshu).</description>
    <link>https://forem.com/kevinshu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1073631%2Fcb367382-13dd-4787-b1e5-8dae2e21b528.jpeg</url>
      <title>Forem: Kevin Shu</title>
      <link>https://forem.com/kevinshu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kevinshu"/>
    <language>en</language>
    <item>
      <title>Git and normalization of line-endings</title>
      <dc:creator>Kevin Shu</dc:creator>
      <pubDate>Fri, 28 Apr 2023 20:12:58 +0000</pubDate>
      <link>https://forem.com/kevinshu/git-and-normalization-of-line-endings-228j</link>
      <guid>https://forem.com/kevinshu/git-and-normalization-of-line-endings-228j</guid>
      <description>&lt;h1&gt;
  
  
  Git and normalization of line-endings
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;A few months ago, I spent hours trying to decide about the best way to deal with line endings and how to switch a repo to using .gitattributes. I just found those comprehensive notes, and thought they'd be easier to find here than buried in my notes...&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;line-ending normalization is about converting &lt;code&gt;LF&lt;/code&gt; &amp;lt;=&amp;gt; &lt;code&gt;CR+LF&lt;/code&gt;, for cross-platform compatibility&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.gitattributes&lt;/code&gt; file is the safest &lt;code&gt;git&lt;/code&gt; mechanism to manage line-endings normalization&lt;/li&gt;
&lt;li&gt;updating normalization settings is tricky because &lt;code&gt;git&lt;/code&gt; may report changes on unmodified files, and it is totally not obvious what is happening&lt;/li&gt;
&lt;li&gt;there are some tricks to help understand what is happening and to fix things&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Line endings and Operating systems
&lt;/h2&gt;

&lt;p&gt;When you press &lt;code&gt;&amp;lt;Enter&amp;gt;&lt;/code&gt; in your text editor, the file is modified with invisible characters that represent the new line. This invisible thing is most commonly  represented in two ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ASCII character LF (aka Line Feed)&lt;/li&gt;
&lt;li&gt;ASCII character CR+LF (aka Carriage Return + Line Feed)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Historically, most systems used to require CR+LF, and Unix systems decided in the 1980s to remove the CR character to simplify things and save disk space.&lt;/p&gt;

&lt;p&gt;In practice, Windows is the only modern operating systems that still uses CRLF line endings.&lt;/p&gt;

&lt;p&gt;When developing in a team, you will end up with people working on Windows and other operating systems, and you will need to manage this difference in your source control system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Git's core.autocrlf
&lt;/h2&gt;

&lt;p&gt;This settings is defined via git config. It applies globally or per repo. When enabled, it applies normalization on all files detected by git as text&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Checkout Windows-style, commit Unix-style (&lt;code&gt;core.autocrlf=true&lt;/code&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Git will convert LF to CRLF when checking out text files.&lt;/li&gt;
&lt;li&gt;When committing text files, CRLF will be converted to LF.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;This is the default value pushed by the installer on Windows systems&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Checkout as-is, commit Unix-style (&lt;code&gt;core.autocrlf=input&lt;/code&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Git will not perform any conversion when checking out text files.&lt;/li&gt;
&lt;li&gt;When committing text files, CRLF will be converted to LF.&lt;/li&gt;
&lt;li&gt;some people recommend using this when developing on Unix systems&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Checkout as-is, commit as-is (&lt;code&gt;core.autocrlf=false&lt;/code&gt;)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Git will not perform any conversions when checking out or committing text files.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;This is default value if the setting is not defined.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Some people consider it is not git's responsibility to do line-ending normalization. It could be tempting to go "checkout as-is commit as-is" in order to disable git's normalization. But it cannot be commited to a repo, so it is dependent on developer workstation settings ====&amp;gt; fragile&lt;/p&gt;

&lt;p&gt;Regardless of what is defined in people's local &lt;code&gt;core.autocrlf&lt;/code&gt; setting, individual repository maintainers can override the behavior via the &lt;code&gt;.gitattributes&lt;/code&gt; file, which is the most robust way to go.&lt;/p&gt;

&lt;h2&gt;
  
  
  .gitattributes
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;.gitattributes&lt;/code&gt; assigns attributes to file types. The &lt;code&gt;text&lt;/code&gt; and &lt;code&gt;eol&lt;/code&gt; attributes are used to control the end-of-line normalization process&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-text&lt;/code&gt; : disable normalization for this type of file. Should be used for any type of binary file.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;text&lt;/code&gt; : normalizes this type of file using &lt;code&gt;core.eol&lt;/code&gt;, which defaults to OS native (&lt;code&gt;core.eol&lt;/code&gt; should not be touched in normal situations)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;eol=lf&lt;/code&gt; : forces &lt;code&gt;lf&lt;/code&gt; on all systems, regardless of OS&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;eol=crlf&lt;/code&gt; : forces &lt;code&gt;crlf&lt;/code&gt; on all systems, regardless of OS&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;global wildcards&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;* text=auto&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;lets &lt;code&gt;git&lt;/code&gt; detect file type and apply normalization accordingly&lt;/li&gt;
&lt;li&gt;similar to setting &lt;code&gt;core.autocrlf=true&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;* -text&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;people have tried to use this to emulate "checkout as-is commit as is"&lt;/li&gt;
&lt;li&gt;but they had various levels of success&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;you can always use these wildcards in addition to more specific overrides&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Beware, line-endings normalization &lt;strong&gt;must not&lt;/strong&gt; be enabled on &lt;strong&gt;&lt;em&gt;any&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;binary&lt;/strong&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating the normalization settings
&lt;/h2&gt;

&lt;p&gt;If you change the normalization settings (either &lt;code&gt;core.autocrlf&lt;/code&gt; or &lt;code&gt;.gitattributes&lt;/code&gt;), you will have some work to do on your local repository, on your remote repository, and on your colleagues workstations.&lt;/p&gt;

&lt;p&gt;You can also just leave it be, but you expose yourself to weird git behaviors (untouched files reported as changed, among others) or other issues.&lt;/p&gt;

&lt;p&gt;Your first reflex would be to look at the line endings in your code editor and play around with the different git commands you'll find online, but it can very quickly become very confusing.&lt;/p&gt;

&lt;h4&gt;
  
  
  View the difference between Index and Workspace
&lt;/h4&gt;

&lt;p&gt;You will find quantities of "solutions"/tutorials in stackoverflow or other websites that tell you what to do, but they always miss some edge cases.&lt;/p&gt;

&lt;p&gt;Git normalization does not happen in the workspace, but during the transition into or out of the index, so you need a way to view line-endings in both the index and the workspace before acting.&lt;/p&gt;

&lt;p&gt;Here is the thing that should be checked to understand what is happening, and most tutorials don't talk about it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git ls-files --eol
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which could result in this type of output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;i/lf    w/crlf  attr/                   Applications/K8S/versions.tf
i/lf    w/lf    attr/text eol=lf        .gitignore
i/-text w/-text attr/                   Services/SMB/hosts-2022-10-20.xlsx
i/lf    w/lf    attr/                   .gitattributes
i/crlf  w/crlf  attr/                   Applications/K8S/ci/backend.tfvars
i/lf    w/crlf  attr/text eol=lf        Legacy/Modules/Keyvault/.gitignore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;i/ tells you how the file is saved in the index&lt;/li&gt;
&lt;li&gt;w/ tells you how the file is presented in the workspace&lt;/li&gt;
&lt;li&gt;attr/ tells you how the .gitattributes file(s) is (are) hinting git to deal with this file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a usual Windows developer using the &lt;code&gt;core.autocrlf=true&lt;/code&gt; option (which is the default pushed by git installation on Windows), you should normally mostly get a mix of the first three types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;i/lf    w/crlf  attr/&lt;/code&gt; : the file is normalized by git and uses Windows standard line-endings &lt;code&gt;crlf&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;i/lf    w/lf    attr/text eol=lf&lt;/code&gt; : the file is normalized by git and enforced to use &lt;code&gt;lf&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;i/-text w/-text attr/&lt;/code&gt; : the file is autodetected as a binary and not normalized by git&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you ended up with a mix of any other ones, it may be because you or somebody made some changes in the &lt;code&gt;.gitattributes&lt;/code&gt; file or the &lt;code&gt;core.autocrlf&lt;/code&gt; option.&lt;/p&gt;

&lt;h4&gt;
  
  
  Repairing &lt;code&gt;i/crlf w/crlf attr/&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This file was most probably pushed by someone using &lt;code&gt;core.autocrlf=false&lt;/code&gt; and working in Windows. This will typically make git complain about changes on untouched files.&lt;/p&gt;

&lt;p&gt;Fix strategies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;in any case, &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;make sure your have a clean repo before acting&lt;/li&gt;
&lt;li&gt;communicate on this change, because people will encounter real conflicts&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;option1: make a commit that will fix all the files in your repo with &lt;code&gt;git add . --renormalize&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pb: will impede your capability to do a blame&lt;/li&gt;
&lt;li&gt;you could instruct blame to be more happy with option &lt;code&gt;-w&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;in practice, git GUIs will happily workaround this&lt;/li&gt;
&lt;li&gt;if necessary you could also use these blame options --ignore-rev, --ignore-revs-file&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;option2: rewrite your history&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pb: rewriting history is hard.&lt;/li&gt;
&lt;li&gt;need to synchronize all committers&lt;/li&gt;
&lt;li&gt;almost impossible in opensource projects&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://www.ofcodeandcolor.com/2013/08/29/normalizing-line-endings-in-git-repositories/"&gt;https://www.ofcodeandcolor.com/2013/08/29/normalizing-line-endings-in-git-repositories/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.moxio.com/blog/43/ignoring-bulk-change-commits-with-git-blame"&gt;https://www.moxio.com/blog/43/ignoring-bulk-change-commits-with-git-blame&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Repairing &lt;code&gt;i/lf w/crlf attr/text eol=lf&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;In this case, the index is ok, but the workspace is "broken".&lt;/p&gt;

&lt;p&gt;This file was probably checked out before attribute &lt;code&gt;eol=lf&lt;/code&gt; was specified.&lt;br&gt;
Git will not bother you with this. But maybe your code editor or tool will bug you if it requires &lt;code&gt;crlf&lt;/code&gt; line-endings for some file types.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;visual studio may complain or introduce incoherent line-endings if csproj have "wrong" line endings&lt;/li&gt;
&lt;li&gt;terraform will complain if &lt;code&gt;*.lock.hcl&lt;/code&gt; files have wrong line-endings&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The fix : delete the local file, and check it out again&lt;/p&gt;

&lt;p&gt;Bulk fixing: pipe the output of this command to xargs rm, then do a git reset (&lt;strong&gt;with all the precautions needed!!!&lt;/strong&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git ls-files --eol | grep "i/lf    w/crlf  attr/t" | cut -f2 -d$'\t'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Repairing &lt;code&gt;i/lf w/lf attr/text eol=crlf&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;In this case, the index is ok, but the workspace is "broken".&lt;/p&gt;

&lt;p&gt;This time, it may be a problem if your tooling or IDE requires &lt;code&gt;lf&lt;/code&gt; line-endings.&lt;/p&gt;

&lt;p&gt;The fix: same as the other "broken" workspace situation.&lt;/p&gt;

</description>
      <category>git</category>
      <category>devjournal</category>
    </item>
  </channel>
</rss>
