The recommended approach for Git is to use the following:
*.sh text eol=lf
That means that the file is a text file, and it should always use LF characters, which is what you want for shell files. However, when you do that, you need to do git add --renormalize .
and then commit both the changes and the .gitattributes
file. Otherwise, if you've already checked the file in with CRLF endings, nothing will change.
If you really have files that are binary, such as small images, you can do this:
*.jpg binary
However, if you do that in this case, because the repository already contains CRLF endings, the file won't be modified, so you'll still end up with CRLF endings. For a true binary file, like an image, that's what you want, but for a shell script, that would be undesirable.
Note that if you have other files in your repository which are text, you can also do this:
*.c text
which will enable automatic line ending conversion, or if you just want all of the remaining file types to be automatically guessed, you can do this:
* text=auto
That will guess whether a file is text or binary based on whether it contains NUL bytes within the first couple kilobytes. Note that you still need to do git add --renormalize .
and commit both the resulting changes and the .gitattributes
for that to have any effect.
If you still see changes in the local working tree after running git add --renormalize .
and committing, try using git grep -lP '\r' HEAD:FILE
(replacing FILE
) to see if the file has a CRLF at the end of it in the HEAD
revision. If not, you've done it correctly.