How to change existing line ending CRLF to LF and successfully commit it to repository
Asked Answered
R

2

6

In my project, there are many kinds of environments (Windows, Linux, Mac OS).

There are some people initially having wrong setting for Git, so in the git repository, some files have been mixed up with CRLF (I want all the files in git repo to be LF).

How can I quickly fix all at once the existing CRLF in repo to LF (my developing environment is restricted with just some simple editor - Visual Studio, Sakura Editor, ...). Fixing line ending one by one is so time-consuming.

Appreciate for any advices.

Racemose answered 27/11, 2021 at 2:55 Comment(0)
B
8

Running below command in the root directory of your repository should do what you need. (If you are using Windows, run it in Git Bash.)

find . -type f -not -path './.git/*' -print0 | xargs -0 dos2unix
Bajaj answered 27/11, 2021 at 20:2 Comment(2)
This answer should be accepted. It saved my day!Pacian
Note that you may need to install dos2unix using Homebrew or similar: brew install dos2unixTranseunt
L
2

If you only have to do this once, you could use something like notepad++, which allows find and replace of files in a directory. You can use Search > Find in Files... and use 'Extended Mode' to change CRLF to LF with find: \r\n and replace: \n.

Details are in this post: Change EOL on multiple files in one go

Alternatively, if you are on linux, it looks like you can use dos2unix, which can be installed if not already on your system.

Source: https://linux.die.net/man/1/dos2unix

If you want to do this regularly, you should be able to script it. Or use Vim from command line like this:

vim file.txt -c "set ff=unix" -c ":wq"

These options are detailed here, just using different parameters than yours: How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script

Lower answered 27/11, 2021 at 3:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.