Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Removing carriage returns from source-controlled files

I've got a Git repository that has some files with DOS format (\r\n line endings). I would like to just run the files through dos2unix (which would change all files to UNIX format, with \n line endings), but how badly would this affect history, and is it recommended at all?

I assume that the standard is to always use UNIX line endings for source-controlled files, and optionally switch to OS-specific line endings locally?

like image 675
Blixt Avatar asked Mar 18 '10 00:03

Blixt


People also ask

Should I use CRLF or LF?

Whereas Windows follows the original convention of a carriage return plus a line feed ( CRLF ) for line endings, operating systems like Linux and Mac use only the line feed ( LF ) character. The history of these two control characters dates back to the era of the typewriter.

What is M in git?

^M represents carriage return. This diff means something removed a Unicode BOM from the beginning of the line and added a CR at the end.

What is CRLF and LF in git?

Git Git Warning. Created: November-23, 2021. LF stands for Line Feed which is a way to represent the end of a line in UNIX-based systems. But in a Windows-based system, a line is usually expressed by CR (Carriage Return) and a line feed (LF).


2 Answers

This crlf thing drove us crazy when we converted from svn to git (in a central (bare) like) scm environment. The thing that ultimately got us was we copied the global .gitconfig file to everyone's user root (yep both windows and linux) with the initial one coming from a Windows system and having core.autocrlf=true and core.safecrlf=false which played havoc on the linux users (like bash scripts didn't work and all those awful ^M's). So we initially did a checkout and clone script that did a dos2unix after these commands. Then I ran across the core.autocrlf and core.safecrlf config items and set them based on the O/S:

Windows: core.autocrlf=true and core.safecrlf=false Linux: core.autocrlf=input and core.safecrlf=false

These were set with: ---on Windows---

git config --global core.autocrlf true
git config --global core.safecrlf false

---on Linux---

git config --global core.autocrlf input
git config --global core.safecrlf false

Then for our Linux developers we setup a little bash script /usr/local/bin/gitfixcrlf:

#!/bin/sh
# remove local tree
git ls-files -z | xargs -0 rm
# checkout with proper crlf
git checkout .

Which they only had to run on their local sandbox clones once. Any future cloning was done correctly. Any future push pulls now were handled correctly. So, this solved our multiple O/S issues with linefeeds. Also Note that Mac falls in the same config as Linux.

like image 166
pn1 dude Avatar answered Sep 22 '22 09:09

pn1 dude


The approach you’ll have to use depends on how public your repository is.

If you don’t mind or care about changing all SHAs because you’re more or less the only one using it but want to have this issue sorted out for all times, you can run a git filter-branch and apply dos2unix to all files in each commit. (If you’re sharing the repository, everyone else needs more or less to completely renew it, so this is potentially dangerous.)

So the better option and also an easier way would be to change it only in the current heads. This means that your past commits still have \r\n endings but unless you’re doing much cherry-picking from the past this should not be a problem. The diff tools might complain a bit more often, of course, but normally you’ll only diff with commits in the vicinity, so this issue resolves itself as the commits accumulate.

And UNIX line endings are standard, you’re correct about that. Best approach is to setup your editor to only write these endings even on windows. Otherwise, there is also a autocrlf setting which you can use.


Addition to the history rewriting part:

Last time I did the same, I used the following command to change all files to unix endings.

#!/bin/bash
all2dos() { find * -exec dos2unix {} \; }
export -f all2dos
git filter-branch -f --tree-filter 'all2dos' --tag-name-filter cat --prune-empty -- --all
like image 34
Debilski Avatar answered Sep 22 '22 09:09

Debilski