Git's primary solution to all this is to specify that LF is the best way to store line endings for text files in a Git repository's object database. It doesn't force this on you but most developers using Git and GitHub have adopted this as a convention and even our own help recommends setting up your config to do this.
text eol=crlf Git will always convert line endings to CRLF on checkout. You should use this for files that must keep CRLF endings, even on OSX or Linux. text eol=lf Git will always convert line endings to LF on checkout. You should use this for files that must keep LF endings, even on Windows.
^M represents carriage return. This diff means something removed a Unicode BOM from the beginning of the line and added a CR at the end.
To tell what line endings a file in the repository is using, use git show to extract the file's contents. This will give you the contents without changing the line endings.
Try setting core.autocrlf value like this :
git config --global core.autocrlf true
This answer seems relevant since the OP makes reference to a need for a multi-OS solution. This Github help article details available approaches for handling lines endings cross-OS. There are global and per-repo approaches to managing cross-os line endings.
Global approach
Configure Git line endings handling on Linux or OS X:
git config --global core.autocrlf input
Configure Git line endings handling on Windows:
git config --global core.autocrlf true
Per-repo approach:
In the root of your repo, create a .gitattributes
file and define line ending settings for your project files, one line at a time in the following format: path_regex line-ending-settings
where line-ending-settings
is one of the following:
The text
value can be configured further to instruct Git on how to handle line endings for matching files:
text
- Changes line endings to OS native line endings.text eol=crlf
- Converts line endings to CRLF
on checkout.text eol=lf
- Converts line endings to LF
on checkout.text=auto
- Sensible default that leaves line handle up to Git's discretion.Here is the content of a sample .gitattributes file:
# Set the default behavior for all files.
* text=auto
# Normalized and converts to
# native line endings on checkout.
*.c text
*.h text
# Convert to CRLF line endings on checkout.
*.sln text eol=crlf
# Convert to LF line endings on checkout.
*.sh text eol=lf
# Binary files.
*.png binary
*.jpg binary
More on how to refresh your repo after changing line endings settings here. Tldr:
backup your files with Git, delete every file in your repository (except the .git directory), and then restore the files all at once. Save your current files in Git, so that none of your work is lost.
git add . -u
git commit -m "Saving files before refreshing line endings"
Remove the index and force Git to rescan the working directory.
rm .git/index
Rewrite the Git index to pick up all the new line endings.
git reset
Show the rewritten, normalized files.
In some cases, this is all that needs to be done. Others may need to complete the following additional steps:
git status
Add all your changed files back, and prepare them for a commit. This is your chance to inspect which files, if any, were unchanged.
git add -u
It is perfectly safe to see a lot of messages here that read[s] "warning: CRLF will be replaced by LF in file."
Rewrite the .gitattributes file.
git add .gitattributes
Commit the changes to your repository.
git commit -m "Normalize all the line endings"
Use .gitattributes instead, with the following setting:
# Ignore all differences in line endings
* -crlf
.gitattributes would be found in the same directory as your global .gitconfig. If .gitattributes doesn't exist, add it to that directory. After adding/changing .gitattributes you will have to do a hard reset of the repository in order to successfully apply the changes to existing files.
Issue related to git commands on Windows operating system:
$ git add --all
warning: LF will be replaced by CRLF in ...
The file will have its original line endings in your working directory.
Resolution:
$ git config --global core.autocrlf false
$ git add --all
No any warning messages come up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With