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

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

How to Search for a Lost File in the Git Log

How to Search for a Lost File in the Git Log

  • 2024-04-27
  • 4 minutes to read

I have lost a file in my Git repository. How can I find it by searching the git log?

Read More
No Such Shell Function 'Zle Line Init' in Zsh

No Such Shell Function 'Zle Line Init' in Zsh

  • 2024-04-25
  • 3 minutes to read

Troubleshooting the error message "no such shell function 'zle line init'" in zsh when using OhMyPosh.

Read More
Getting Started With Python in Vscode

Getting Started With Python in Vscode

  • 2024-04-05
  • 2 minutes to read

This post will help you get started with Python in Vscode and identify some of the useful extensions to install.

Read More