Addressing line feed issues between macOS and Windows in Git

Addressing line feed issues between macOS and Windows in Git

  • coding
  • 2024-03-29
  • 4 minutes to read
Table of Contents

Introduction

I use a Mac and from time to time I either want to contribute to the same project from a Windows machine, or I may have someone else contribute to a project from a Windows machine. When this happens, I run into issues with line endings. This post will show you how to fix that.

The Problem

I have a project that I have opened up on a Windows VM running in Parallels on my Mac because I wanted to test some behaviour on Windows. My staging environment in the repository is clean and so there should be no changes to commit. However, when I opened up the same project on VS Code within Windows I was met with 69 changes! If I look at any of the files to identify the change, there is no difference.

A screenshot of VS Code in Windows showing 69 changes, the comparison of the before and after of the selected file is consistent.

This is because the changes are related to line endings. Rather annoyingly, different operating systems use different line endings. Windows uses CRLF (Carriage Return Line Feed) and Unix-based systems use LF (Line Feed).

So the objective is to configure Git to handle line endings in a way that is consistent across both operating systems.

The Solution

core.autocrlf

The command that fixes this is git config core.autocrlf. The command accepts a single argument. For macOS the argument required is input and for Windows the argument required is true.

Apply config locally

Running this command as is will only apply the settings to the git repository that you are currently in i.e. it will be applied to the local configuration. Running the command git config --list followed by git config --global --list will show you the current settings for both configurations:

## run from macOS
git config core.autocrlf input
git config --list
core.autocrlf=input
git config --global --list

Note that I have stripped out all configuration options returned by git config --list for the purpose of this explanation.

This would be fine in scenarios where the Windows machine is another user or a distinct machine however, there is a further issue for my set up. The problem lies in the fact that my parallels VM running Windows is mapped to use the same directory as my Mac. So rather than having two separate repositories - one on my Mac and one on my Windows VM they are technically using the same repository and therefore the same local git configuration. So whilst I could then switch to my Windows VM and set the configuration to true, this would then populate the same repository with the different line endings settings! Here is what happens if I run the command on my Windows VM:

## run from Windows VM
git config core.autocrlf true
git config --list
core.autocrlf=input
core.autocrlf=true
git config --global --list

For my set up, I will need to apply the setting globally.

Apply config globally

So rather than apply this configuration locally, I can apply it globally by adding the --global flag and the settings will be applied to all repositories on my machine:

## run from macOS
git config --unset core.autocrlf
git config --global core.autocrlf input
git config --list

git config --global --list
core.autocrlf=input

Running git config --unset core.autocrlf will remove the option from my local configuration, and then the option will be applied to my global confirmation. From the Windows VM, I can then run the following commands:

## run from Windows VM
git config --global core.autocrlf=true
git config --list

git config --global --list
core.autocrlf=true

Conform line endings

The last thing I need to do is conform the line endings of the files in the repository so that they are consistent with the settings I have just applied. This can be done by running the following commands:

git add . -am "Saving files before refreshing line endings"
git rm -rf --cached .
git reset --hard HEAD
git commit -m "Conform line endings"

This will commit all outstanding changes, refresh the current branch to reflect the new configuration and then commit any changes in the repository as a result of running the refresh. In other words, commit any line feed corrections.

Conclusion

This post has shown you how to fix line feed issues in Git between macOS and Windows. The key is to set the core.autocrlf configuration to input on macOS and true on Windows. This can be done locally or globally. If you are running Parallels and sharing the same repositories across a Windows VM you will need to set the configuration globally.

References

#mtfbwy



Recent Posts

Hugo Live Reload Keeps Reloading

Hugo Live Reload Keeps Reloading

  • 2024-05-18
  • 3 minutes to read

I have run into an issue from time to time where Hugo's live reload feature keeps reloading the page. This is how I fixed it.

Read More
First Steps with Mermaid in Vscode

First Steps with Mermaid in Vscode

  • 2024-05-17
  • 7 minutes to read

My blog theme supports mermaid, a visual diagram syntax, so I wanted to try it out in vscode.

Read More
T-SQL Tuesday #174 My favourite job interview question

T-SQL Tuesday #174 My favourite job interview question

  • 2024-05-14
  • 4 minutes to read

This is my 12th contribution to TSQL Tuesday, the data community blog party. Have a read, and why not join in next time!

Read More