Home > Mobile >  How to change existing line ending CRLF to LF and successfully commit it to repository
How to change existing line ending CRLF to LF and successfully commit it to repository

Time:11-28

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.

CodePudding user response:

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

CodePudding user response:

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
  • Related