开发者

Make Git "LF will be replaced by CRLF" warnings go away

开发者 https://www.devze.com 2022-12-09 04:31 出处:网络
I have setup Git so it doesn\'t commit inconsistent line endings. The problem with that is a whole pile of files appear modified even though they are not. What do I type to make these files have the l

I have setup Git so it doesn't commit inconsistent line endings. The problem with that is a whole pile of files appear modified even though they are not. What do I type to make these files have the line endings fixed on the loca开发者_如何学Cl side?

# git checkout dev
M   src/au/policy/dao/EmailQueue.java
M   src/au/policy/dao/EmailQueueFactory.java
M   src/au/policy/dao/PolicyPublisher.java
Already on 'dev'

# git diff
warning: LF will be replaced by CRLF in src/au/policy/dao/EmailQueue.java
warning: LF will be replaced by CRLF in src/au/policy/dao/EmailQueueFactory.java
warning: LF will be replaced by CRLF in src/au/policy/dao/PolicyPublisher.java

This is what I added to my git config file which seems to do what I intended aside from this issue:

autocrlf = true


This might happen if you change core.autocrlf config variable (if I understand your problem correctly).

If you are at clean state, i.e. just after commit, and you don't have uncomitted changes, forced re-checkout and removing index should do the trick:

The below command git reset --hard HEAD will make your current branch to point to the latest commit and all uncommitted code will be lost. Make sure to commit the code or take the backup

$ rm .git/index
$ git reset --hard HEAD

That, I think, would sync both working area files, and the index (staging area) to follow crlf settings.


I had this problem when creating new Xcode project. My solution for this problem:

In terminal write

$: git config --global --edit

Then in git config file change safecrlf to false. My settings:

[core]
    autocrlf = input
    safecrlf = false

I know git have cmd line tools for this but they don't work for me. And then Xcode create git repos without any problem.


Only thing I can think of is to check if core.safecrlf is set to warn.

git config --get core.safecrlf

I think possible values are true, false, and warn. I believe that setting to false will resolve the warning, though it may not be a good idea.


You can just delete and re-checkout the offending files from the index like this:

rm <files>
git checkout -- <files>

Or, if they are the only modified files (be careful with this command), you can script it like this:

git diff --name-only --diff-filter=M | xargs rm --
git checkout -- .

On a GNU system you can use a slightly safer pipe, but you don't appear to have spaces or other delimiting characters in your filenames in any case.

git diff -z --name-only --diff-filter=M | xargs -0 rm --


Try this, it worked for me:

cd src/au/policy/dao
dos2unix

If there are other files in that folder, then you'll want to break it up into the following (otherwise it will try to do it on every file in any subdirectories, which may take a while):

cd src/au/policy/dao
dos2unix EmailQueue.java
dos2unix EmailQueueFactory.java
dos2unix PolicyPublisher.java

It ran really quick on my machine and fixed all of the line endings, and it's a little simpler and easier than some of these other fixes.


Note that note all of the above fixes may work for you, for example you might have received the code through a simple file transfer. You can accept each warning by pressing ENTER, but on on large repos that can take ages. Another way to ignore the conversion on code that has already been checked-out and edited is to create a patch file:

git diff > changes.patch


In my case, after setting autocrlf variable to true, then running

git stash

and then

git stash pop

everything cleared out

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号