.gitattributes don't work properly on mac and windows
Asked Answered
K

2

7

On my project i use computers with different OS, one is Mac second is with windows. When I use git every change is shown as whole document change. The reason is different end-of-line in these two OS. I read this https://help.github.com/articles/dealing-with-line-endings/ and made a .gitattributes file in the root folder but the problem still exists. This is my .gitattributes file:

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.

*.css text
*.html text
*.js text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

I have no idea why it's not working because I was try a lot of configurations of this file before.

Kcal answered 12/10, 2016 at 9:28 Comment(2)
Have you tried the git config core.autocrlf option? AFAIK this is preferred. Have you got any problem with it?Valeriavalerian
Yes i try this on both computers but it dont work.Kcal
A
11

The .gitattributes file should be added with the first commit. If you add it a few commits in, you need to normalize all the existing files explicitly.

$ rm .git/index     # Remove the index to force Git to
$ git reset         # re-scan the working directory
$ git status        # Show files that will be normalized
$ git add -u
$ git commit -m "Introduce end-of-line normalization"

See https://git-scm.com/docs/gitattributes

Alkanet answered 13/10, 2016 at 21:47 Comment(0)
D
3

If .gitattributes file was not added with the first commit, the following should be performed to apply attributes locally:

  1. Go to the root of the repository

  2. Check status:

    git status

  3. If it says "nothing to commit, working tree clean", perform:

    git rm --cached -r .

    git reset --hard

The answer is based on https://dev.to/deadlybyte/please-add-gitattributes-to-your-git-repository-1jld

Dibromide answered 30/10, 2020 at 13:47 Comment(1)
Interesting... After running git add --renormalize ., the files with modified EOL were listed by git status, and I could commit the change, but in reality they still had the original EOL (verified by hex view). The commands in pt. 3. were needed to make the EOLs really change to the settings given in .gitattributes. Thanks!Immunochemistry

© 2022 - 2024 — McMap. All rights reserved.