snippetjavascriptTip
How do I fix incorrect line endings in a git repository?
Viewed 0 times
incorrectfixrepositoryhowgitendingsline
Problem
A little while back, while working on a Ruby repository, a colleague of mine encountered an issue where they couldn't run a Ruby script. The error was a little odd, leading us down a rabbit hole of trying to figure it out:
Naturally, we looked it up on StackOverflow to figure out what was going on. Turns out, line endings were the culprit. For whatever reason, the script had
The first step to fixing this was to set line endings correctly, and have git manage how it handles them globally:
While this command sets the line endings to
Fortunately, there's a way to do this without changing the commit history, as another arduous search on StackOverflow revealed. The solution is to use the
Naturally, we looked it up on StackOverflow to figure out what was going on. Turns out, line endings were the culprit. For whatever reason, the script had
CRLF line endings instead of the expected LF line endings. This can happen when files are edited on different operating systems, as Windows uses CRLF while Unix-based systems (like Linux and macOS) use LF. This may not have been the exact case, as the whole team is on Unix-based systems, but VS Code confirmed that the file had CRLF line endings.The first step to fixing this was to set line endings correctly, and have git manage how it handles them globally:
While this command sets the line endings to
LF for all files in the repository, it doesn't fix existing files. To fix the existing files, we need to re-commit them with the correct line endings. However, this seems like a hassle and will invariably mess up the repository's history.Fortunately, there's a way to do this without changing the commit history, as another arduous search on StackOverflow revealed. The solution is to use the
git add --renormalize command, which will re-check all files in the repository and apply the new line ending settings.Solution
env: ruby\r: No such file or directoryThe first step to fixing this was to set line endings correctly, and have git manage how it handles them globally:
While this command sets the line endings to
LF for all files in the repository, it doesn't fix existing files. To fix the existing files, we need to re-commit them with the correct line endings. However, this seems like a hassle and will invariably mess up the repository's history.Fortunately, there's a way to do this without changing the commit history, as another arduous search on StackOverflow revealed. The solution is to use the
git add --renormalize command, which will re-check all files in the repository and apply the new line ending settings.These two commands fixed the issue without messing up the repository's history. After running these commands, the Ruby script could be executed without any issues and we were back on track. Hopefully, this little tip will save you some time in the future, if you ever encounter a similar issue with line endings in your git repository! 🍻
Code Snippets
env: ruby\r: No such file or directory# Set the line endings to LF for all files in the repository
git config --global core.autocrlf input# Re-check all files and apply the new line ending settings
git add --renormalize .Context
From 30-seconds-of-code: fix-incorrect-line-endings
Revisions (0)
No revisions yet.