Make git store text files with CRLF line endings with .gitattributes
I’d like git to store text files with the CLRF line ending-style internally, I don’t just want to see such line endings in the working directory (but the WD should also contain CRLF files). What is the .gitattributes configuration that would achieve this?
I need this so the repository can be seamlessly synced with a Mercurial one that also uses CRLF internally (due to the original files being such).
- Mercurial: “abandoned transaction found - run hg recover”. Recover does not work
- Check in single file with Mercurial?
- git equivalent of 'hg serve'?
- How do you specify authentication information for Mercurial as part of Jenkins? (mercurial_keyring)
- Backup a local working directory on BitBucket - clone or push
- git --bare clone analogue for Hg (Mercurial)
After going over every possible configuration I found online, including here, I couldn’t find a solution that would yield CRLF. The files, in the end, end up being stored as LF. I tried the following .gitattributes file:
* text= * text eol=crlf
Also tried text=auto, text=crlf and even without the first line, no luck. I also re-clone the repo between changing this.
Edit: literally 5 seconds after I posted this I found another snippet that appears to be working:
However I’m unsure what exactly this does. Can somebody confirm please that this is what I want it won’t bite me later?
One Solution collect form web for “Make git store text files with CRLF line endings with .gitattributes”
You want the opposite of what Git supports natively. Git considers linefeed to be the native internal line ending, and either supports doing no filtering, so lf or crlf are stored depending what the files in the filesystem have, or filtering to convert crlf in files on the filesystem to lf internally, and lf internally to crlf on the filesystem. So none of the eol settings will work for you, since it sounds like you want the opposite; crlf internally, and lf on the filesystem.
Git also support arbitrary “smudge” and “clean” filters. “smudge” does some transformation that “dirties” the file, and “clean” should do the opposite transformation to clean it up to commit.
To do this, add the following to your
[filter "dosify"] clean = unix2dos smudge = dos2unix
And then in your
If you don’t have
unix2dos installed, you could instead use:
[filter "dosify"] clean = tr '\\n' '\\r\\n' smudge = tr '\\r\\n' '\\n'
edit: To respond to your edit,
-crlf just means that no crlf <-> lf translations will be done in either direction. It won’t translate lf in your working directory to crlf internally, so if you have editors that store lf, but need the data to be crlf internally, it won’t help with that. If you just want to avoid doing any sort of translation, then just
* -crlf should work, to avoid having any automatic translation applied. However, not doing any translation is the default if you haven’t ever set any settings like
autocrlf (except on Windows, where I believe that
autocrlf is the default).