Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get git to change the line ending in the working directory?

Tags:

git

I have git on windows configured with git config --global core.autocrlf false so that git does not auto convert files on checkout from LF line endings to CRLF.

When I create a new file on windows and add it I get the following output.

git add windows-file.txt
warning: CRLF will be replaced by LF in windows-file.txt.
The file will have its original line endings in your working directory.

So git is changing my line ending from windows to unix when the windows-file.txt is being added to the git index which is what I want.

The problem I have is that the working directory version is not changed, How can I configure git so that it changes the line endings of both the working directory and the git index?

UPDATE

After the add and the commit git status does not show any differences even though the local working directory version has windows line endings and the repo version has unix line endings.

UPDATE Contents of .gitattributes at the root of the repo

# Set default behaviour, in case users don't have core.autocrlf set.
text eol=lf

# These files are text and should be normalized (convert crlf => lf)
*.java       text
*.xml        text
*.cmd        text
*.sh         text
*.txt        text
*.md         text
*.js         text
*.jsp        text
*.html       text
*.htm        text
*.vm         text
.project     text
.classpath   text
*.properties text
*.txt        text
*.bat        text
*.launch     text

# Denote all files that are truly binary and should not be modified.
*.png    binary
*.jpg    binary
*.jar    binary
*.class  binary
*.gz     binary
*.tar    binary
*.dll    binary
*.exe    binary
*.zip    binary
like image 487
ams Avatar asked Jan 15 '13 19:01

ams


People also ask

How do I change the end of a line in git?

Global settings for line endings The git config core. autocrlf command is used to change how Git handles line endings. It takes a single argument. On Windows, you simply pass true to the configuration.

How do I change from CRLF to LF in git?

You can set the mode to use by adding an additional parameter of true or false to the above command line. If core. autocrlf is set to true, that means that any time you add a file to the Git repository that Git thinks is a text file, it will turn all CRLF line endings to just LF before it stores it in the commit.

How does git store line endings?

Git store whole files as "blobs" and only do conversion when reading (checking out) or writing (indexing). Git does not store "neutralized lines". So yes, Git can save files with mixed line endings, but that's usually a bad practice to avoid.

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.


1 Answers

When you have no uncommitted changes try:

git rm --cached -r . 
git reset --hard

to remove the local files and get them from the index.

like image 200
laktak Avatar answered Sep 19 '22 20:09

laktak