Fix Git Line Feeds on Windows Forever

April 08, 2017 1 mins read

Fix Git Line Feeds on Windows Forever

In my day-to-day workflow, I frequently use a cygwin shell on Windows, namely Babun. As you might imagine, shell scripts with Windows line endings \r\n are simply no good at all:

$ ./script.sh
bash: ./script.sh: /bin/sh^M: bad interpreter: No such file or directory

$ ./script.sh
./script.sh: line 2: $'\r': command not found

This blog post is a note to my future self to not read through the thousands of answers available on StackOverflow again. Let’s get rid of those \r\n things.

Git Config to the Rescue

Usually, those script files find their way through Git to my hard drive. Therefore, let’s use Git to automatically fix those line endings to \n for all times to come!

# set config
$ git config core.eol lf
$ git config core.autocrlf input

Migrate Existing Repositories

You might have existing Git repositories that have \r\n line feeds. Changing the config alone won’t transform all those line endings in existing repositories. Make sure to have a clean working directory and then execute:

# refresh repo
git rm --cached -r .
git reset --hard

Now enjoy your awesome \n line endings and never look back.

Comments

👋 I'd love to hear your opinion and experiences. Share your thoughts with a comment below!

comments powered by Disqus