简体   繁体   中英

Git - CRLF to LF modified files

I'm currently working on a project versioned with git and I was developing it on Windows.

Recently, I swapped from Windows to Linux and, instead of commiting the changes to the remote repository, I just zipped the file cause the feature that I was working on was not done at all.

After unzipped the project's directory on Linux, I noticed that the repository was with changes in all of the project files (due to LF and CRLF distinct line endings), and now I'm with a branch with all the files with changes and to be commited again.

I was searching for this issue and just found topics mentioning git reset --soft , it is a valid way too, but my primary purpouse was not changing the history of changes of all the files.

There is some way to 'undo' this line ending particularity and remove the files from the staging area?

The github docs show how to enable core.autocrlf for a single repository. It also states the following:

When you set the core.autocrlf option or commit a.gitattributes file, you may find that Git reports changes to files that you have not modified. Git has changed line endings to match your new configuration.

To ensure that all the line endings in your repository match your new configuration, backup your files with Git, delete all files in your repository (except the.git directory), 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"

Add all your changed files back and normalize the line endings.

$ git add --renormalize.

Show the rewritten, normalized files.

$ git status

Commit the changes to your repository.

$ git commit -m "Normalize all the line endings"

See here for more information.

The best way to handle this case is to configure your repository to always store LF endings and check out the preferred ones based on the system you're on. You can do that by putting something like this in your .gitattributes file at the root of your repository

* text=auto

and then running git add --renormalize. and commit.

If you have files that must always have an LF line ending, such as shell scripts, or must always have a CRLF line ending, like PowerShell scripts, then you can add other lines as apppropriate:

*.sh eol=lf
*.ps1 eol=crlf

Any time you make line endings changes to your .gitattributes file, make sure that you've run git add --renormalize. and committed afterwards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM