How to clean up line-endings with Git
Asked Answered
A

2

7

I'm quite inexperienced with Git, and this one baffles me:

I just switched my working copy to another branch (a simple git checkout master), and immediately after that, git status tells me about hundreds of changed files. However, it's all about line endings; a git diff --ignore-space-at-eol gives me a lot of warnings:

warning: CRLF will be replaced by LF in src/.../somefile.js.
The file will have its original line endings in your working directory.

I generally consider it a bad idea to have DOS-style line endings in versioned text files, but this is what they have been committed like in the first place; I did this in a Linux box, and the files I got have CRLF line endings.

What is the best way to clean up in my situation? Should I change the line ending of every single file and commit this?

How can Git be configured to avoid such situations in the future?

Avebury answered 21/10, 2013 at 7:51 Comment(3)
As already mentioned, Git is capable of automatically handling line-ending issues on commits with the core.autocrlf setting. This question has a great answer that might assist you in deciding whether or not that option is for you.Kedah
possible duplicate of Force LF eol in git repo and working copyBean
Ok, the 2nd part is answered - in many places, as I see it. As for the 1st part of my question, there seems to be no solution but to commit sanitized data (the CRLF files are from a cloned Github repo) and hope for the best.Avebury
M
2

Try one of these out (depending on your OS):

// Windows
git config --global core.autocrlf true

// Linux, OSX
git config --global core.autocrlf input
Maryettamaryjane answered 21/10, 2013 at 7:54 Comment(3)
I tried it (the Linux version, both with and without --global) and switched to the other branch and back again (git checkout -f ...), but it seems to be too late for my working copy ... Should I kill it completely and clone it anew?Avebury
I'm surprised git didn't figure it out right away. But if that's an option you have, then (safely) try it out.Maryettamaryjane
Perhaps because of the Git version? This is a Debian box, and I have Git v1.7.10.4, while 1.8.4.1 is the current version.Avebury
L
2

As I mentioned before, I prefer:

Whatever solution you choose, check out the "Trying to fix line-endings with git filter-branch, but having no luck" to fix your current index.
In particular, test the removal of the .git/index file, followed by a git reset.

Lacrimatory answered 21/10, 2013 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.