即使内容相同,Git状态也会将文件显示为已更改

Aro*_*eel 168 git

我从其他人那里收到了git checkout,并尝试将未分级的更改提交到本地存储库.但是,即使内容完全相同,很多(如果不是每个)文件也会显示为已修改.

我已经设置core.fileMode为false并且设置core.autocrlf为false,但没有成功.

值得一提的是,我收到的Git repo来自使用Windows的人,而我使用的是Linux.

如何进行实际更改?

编辑:输出git config -l:

user.name=Aron Rotteveel
user.email=<removed>
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=auto
color.ui=true
color.pager=true
color.branch.current=yellow reverse
color.branch.local=yellow
color.branch.remote=green
color.diff.meta=yellow bold
color.diff.frag=magenta bold
color.diff.old=red bold
color.diff.new=green bold
color.status.added=yellow
color.status.changed=green
color.status.untracked=cyan
core.pager=less -FRSX
core.whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol
alias.co=checkout
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.hidedotfiles=dotGitOnly
core.autocrlf=false
remote.origin.url=<removed>
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
Run Code Online (Sandbox Code Playgroud)

更新:添加了一些随机示例文件.这些文件只是纯文本,因此最容易包含.

原始文件位于:https://gist.github.com/c3c5302430935155ef3d.Hexdumps肯定表明文件不同,但我不知道是什么导致这个,以及如何解决它.

HEAD版本:

0000000: 4854 4d4c 2e53 6166 654f 626a 6563 740d  HTML.SafeObject.
0000010: 0a54 5950 453a 2062 6f6f 6c0d 0a56 4552  .TYPE: bool..VER
0000020: 5349 4f4e 3a20 332e 312e 310d 0a44 4546  SION: 3.1.1..DEF
0000030: 4155 4c54 3a20 6661 6c73 650d 0a2d 2d44  AULT: false..--D
0000040: 4553 4352 4950 5449 4f4e 2d2d 0d0a 3c70  ESCRIPTION--..<p
0000050: 3e0d 0a20 2020 2057 6865 7468 6572 206f  >..    Whether o
0000060: 7220 6e6f 7420 746f 2070 6572 6d69 7420  r not to permit 
0000070: 6f62 6a65 6374 2074 6167 7320 696e 2064  object tags in d
0000080: 6f63 756d 656e 7473 2c20 7769 7468 2061  ocuments, with a
0000090: 206e 756d 6265 7220 6f66 2065 7874 7261   number of extra
00000a0: 0d0a 2020 2020 7365 6375 7269 7479 2066  ..    security f
00000b0: 6561 7475 7265 7320 6164 6465 6420 746f  eatures added to
00000c0: 2070 7265 7665 6e74 2073 6372 6970 7420   prevent script 
00000d0: 6578 6563 7574 696f 6e2e 2054 6869 7320  execution. This 
00000e0: 6973 2073 696d 696c 6172 2074 6f0d 0a20  is similar to.. 
00000f0: 2020 2077 6861 7420 7765 6273 6974 6573     what websites
0000100: 206c 696b 6520 4d79 5370 6163 6520 646f   like MySpace do
0000110: 2074 6f20 6f62 6a65 6374 2074 6167 732e   to object tags.
0000120: 2020 596f 7520 7368 6f75 6c64 2061 6c73    You should als
0000130: 6f20 656e 6162 6c65 0d0a 2020 2020 254f  o enable..    %O
0000140: 7574 7075 742e 466c 6173 6843 6f6d 7061  utput.FlashCompa
0000150: 7420 696e 206f 7264 6572 2074 6f20 6765  t in order to ge
0000160: 6e65 7261 7465 2049 6e74 6572 6e65 7420  nerate Internet 
0000170: 4578 706c 6f72 6572 0d0a 2020 2020 636f  Explorer..    co
0000180: 6d70 6174 6962 696c 6974 7920 636f 6465  mpatibility code
0000190: 2066 6f72 2079 6f75 7220 6f62 6a65 6374   for your object
00001a0: 2074 6167 732e 0d0a 3c2f 703e 0d0a 2d2d   tags...</p>..--
00001b0: 2320 7669 6d3a 2065 7420 7377 3d34 2073  # vim: et sw=4 s
00001c0: 7473 3d34 0d0a                           ts=4..
Run Code Online (Sandbox Code Playgroud)

复制版本:

0000000: 4854 4d4c 2e53 6166 654f 626a 6563 740a  HTML.SafeObject.
0000010: 5459 5045 3a20 626f 6f6c 0a56 4552 5349  TYPE: bool.VERSI
0000020: 4f4e 3a20 332e 312e 310a 4445 4641 554c  ON: 3.1.1.DEFAUL
0000030: 543a 2066 616c 7365 0a2d 2d44 4553 4352  T: false.--DESCR
0000040: 4950 5449 4f4e 2d2d 0a3c 703e 0a20 2020  IPTION--.<p>.   
0000050: 2057 6865 7468 6572 206f 7220 6e6f 7420   Whether or not 
0000060: 746f 2070 6572 6d69 7420 6f62 6a65 6374  to permit object
0000070: 2074 6167 7320 696e 2064 6f63 756d 656e   tags in documen
0000080: 7473 2c20 7769 7468 2061 206e 756d 6265  ts, with a numbe
0000090: 7220 6f66 2065 7874 7261 0a20 2020 2073  r of extra.    s
00000a0: 6563 7572 6974 7920 6665 6174 7572 6573  ecurity features
00000b0: 2061 6464 6564 2074 6f20 7072 6576 656e   added to preven
00000c0: 7420 7363 7269 7074 2065 7865 6375 7469  t script executi
00000d0: 6f6e 2e20 5468 6973 2069 7320 7369 6d69  on. This is simi
00000e0: 6c61 7220 746f 0a20 2020 2077 6861 7420  lar to.    what 
00000f0: 7765 6273 6974 6573 206c 696b 6520 4d79  websites like My
0000100: 5370 6163 6520 646f 2074 6f20 6f62 6a65  Space do to obje
0000110: 6374 2074 6167 732e 2020 596f 7520 7368  ct tags.  You sh
0000120: 6f75 6c64 2061 6c73 6f20 656e 6162 6c65  ould also enable
0000130: 0a20 2020 2025 4f75 7470 7574 2e46 6c61  .    %Output.Fla
0000140: 7368 436f 6d70 6174 2069 6e20 6f72 6465  shCompat in orde
0000150: 7220 746f 2067 656e 6572 6174 6520 496e  r to generate In
0000160: 7465 726e 6574 2045 7870 6c6f 7265 720a  ternet Explorer.
0000170: 2020 2020 636f 6d70 6174 6962 696c 6974      compatibilit
0000180: 7920 636f 6465 2066 6f72 2079 6f75 7220  y code for your 
0000190: 6f62 6a65 6374 2074 6167 732e 0a3c 2f70  object tags..</p
00001a0: 3e0a 2d2d 2320 7669 6d3a 2065 7420 7377  >.--# vim: et sw
00001b0: 3d34 2073 7473 3d34 0a                   =4 sts=4.
Run Code Online (Sandbox Code Playgroud)

小智 107

我已经使用以下stpes解决了这个问题

1)从Git的索引中删除每个文件.

git rm --cached -r .
Run Code Online (Sandbox Code Playgroud)

2)重写Git索引以获取所有新行结尾.

git reset --hard
Run Code Online (Sandbox Code Playgroud)

请注意,第2步可能会删除您的本地更改.解决方案是git网站https://help.github.com/articles/dealing-with-line-endings/上描述的步骤的一部分

  • 一定要小心,否则将丢失任何其他更改。 (3认同)
  • 就我而言,我对一些文件做了一堆注释,然后将没有 .git 文件夹的存储库复制到新计算机。当我意识到我丢失了 .git 包时,想回去取回它已经太晚了。所以我: 1. 签出了整个存储库的新副本 2. 用我所做的更改的文件替换了普通文件 3. 在 OP 的帖子中执行第一步:`git rm --cached -r 。` 4. 这个阶段我的更改(以及任何其他被替换的文件),所以我取消了它们。此时我的仓库恢复正常。 (2认同)
  • 辉煌。谢谢你 (2认同)

its*_*ndy 69

你有没有改变文件的模式?我在我的机器上完成了它,本地开发机器有777给所有文件,而repo有755,显示每个文件都被修改.我做了git diff,它显示旧模式和新模式是不同的.如果这是问题,那么你可以通过git config core.filemode false
干杯轻松忽略它们

  • 在 Windows 文件系统中使用 Windows 10 lxss(即 Ubuntu Bash)和 Linux 中的 git,我认为这是一个行结束问题。甚至没有考虑过这可能是文件模式也不同的方式。 (3认同)
  • 为什么默认启用“core.filemode”?这可能破坏了许多干净的存储库历史记录。 (2认同)

seh*_*ehe 56

更新:根据对此问题的评论,问题已解决:

这很简单:第一个文件有CRLF行结束(windows),第二个文件有LF(Unix).该fileUTIL(Git中的\ usr\bin中提供)会告诉你,(file a b将回复类似a: ASCII text, with CRLF line terminators b: ASCII text)

原答案如下:


您显示的差异显示单个不同的行.你可以发布.git/config(或更好git config -l).

您可能会激活一些空白忽略

你应该尝试禁用core.whitespace=fix,-indent-with-non-tab,trailing-space,cr-at-eol;

git show HEAD:myfile|md5sum
md5sum myfile
Run Code Online (Sandbox Code Playgroud)

可用于验证文件实际上是不同的.使用外部差异也可以

git show HEAD:myfile > /tmp/myfile.HEAD

diff -u myfile /tmp/myfile.HEAD

# or if you prefer an interactive tool like e.g.:
vim -d myfile /tmp/myfile.HEAD
Run Code Online (Sandbox Code Playgroud)

  • @AronRotteveel:这很简单:第一个文件有 CRLF 行尾(windows),第二个 LF(Unix)。**编辑** `file` util 会告诉你(`file ab` 会回复类似`a: ASCII text, with CRLF line terminators b: ASCII text`) (2认同)

小智 42

我有同样的问题.在win-> lin copy之后我已经修改了所有文件.
我使用fromdos来修复行结尾
然后

git add -uv 
Run Code Online (Sandbox Code Playgroud)

添加更改.
它添加了3个文件(不是全部),我实际修改过.之后,git状态只显示3个修改过的文件.在git commit之后,git状态一切正常

  • 谢谢,这就是我需要的.我使用了@ sehe建议的比较md5sum来验证文件是否完全相同(在我的例子中是相同的).之后使用-u标志正确过滤那些与变化没有实际差异的文件. (2认同)

MiF*_*vil 23

通过将core.autocrlf从false更改为core.autocrlf = input,我能够解决Windows机器上的问题

git config core.autocrlf input
Run Code Online (Sandbox Code Playgroud)

正如/sf/answers/77861941/中所建议的那样


Von*_*onC 21

但是,即使内容完全相同,很多(如果不是每个)文件也会显示为已修改.

使用git 2.8(2016年3月),您将能够快速检查这些更改是否与eol相关.

提交a7630bd(2016年1月16日)由托斯滕Bögershausen( )tboegi.
(由Junio C gitsterHamano合并- -05f1539提交,2016年2月3日)

ls-files:添加eol诊断

在跨平台环境中工作时,用户可能想要检查文本文件是否在存储库中标准化存储,以及是否.gitattributes正确设置.

可以让Git在索引和工作树中显示行结尾和有效的text/eol属性.

行尾(" eolinfo")如下所示:

"-text"        binary (or with bare CR) file
"none"         text file without any EOL
"lf"           text file with LF
"crlf"         text file with CRLF
"mixed"        text file with mixed line endings.
Run Code Online (Sandbox Code Playgroud)

有效的text/eol属性是以下之一:

"", "-text", "text", "text=auto", "text eol=lf", "text eol=crlf"
Run Code Online (Sandbox Code Playgroud)

git ls-files --eol 给出这样的输出:

i/none   w/none   attr/text=auto      t/t5100/empty
i/-text  w/-text  attr/-text          t/test-binary-2.png
i/lf     w/lf     attr/text eol=lf    t/t5100/rfc2047-info-0007
i/lf     w/crlf   attr/text eol=crlf  doit.bat
i/mixed  w/mixed  attr/               locale/XX.po
Run Code Online (Sandbox Code Playgroud)

显示在索引(' i')和工作树(' w')中的数据中使用的eol约定,以及对于显示的每个路径有效的属性.

  • 虽然这显示了问题,但并没有解决问题。 (2认同)
  • 我通过将 .gitattributes 文件添加到存储库中再次解决了这个问题,其中只有 `* text=auto` :) - 也许把它放在你的答案的顶部? (2认同)
  • @DevinGRhode 谢谢。我已将您的评论包含在答案中以获得更多可见性。 (2认同)

Sak*_*kis 18

在我的情况下,文件在更改文件权限后显示为已修改.

要使git忽略权限更改,请执行以下操作:

# For the current repository
git config core.filemode false   

# Globally
git config --global core.filemode false
Run Code Online (Sandbox Code Playgroud)

  • 你的回答节省了我很多时间。谢谢! (2认同)
  • 将文件从一台笔记本电脑复制到另一台笔记本电脑后,我遇到了这个问题。你的固定保存了它。谢谢! (2认同)

tru*_*ker 9

以下是我在克隆在 Windows 上创建的项目时在 Linux 上解决问题的方法:

在 linux 上为了让事情正常工作,你必须有这个设置:core.autocrlf=input

这是如何设置它: git config --global core.autocrlf input

然后再次从github克隆项目。


Mar*_*air 5

Git的常见问题有一个答案,可能是相关的,虽然我以前从来没有碰到过这样的:

为什么git diff有时列出一个没有变化的文件?

git diff和其他git操作已经过优化,因此它甚至不会查看磁盘和git索引中状态(大小,修改时间等)不同的文件.这使得git diff对于小的变化非常快.如果以某种方式触摸了文件,git diff必须查看内容并比较它,这是一个慢得多的操作,即使实际上没有变化.git diff将文件列为提醒,说明它没有得到最佳使用.运行git状态不仅会显示状态,还会更新索引,状态为未更改的文件磁盘进行后续操作,而不仅仅是差异,速度更快.导致许多文件被diff列出的典型案例是运行批量编辑命令,如perl -pi -e'...'.

什么git status为你显示?

  • 哇.提醒一些官方git文档是多么难以理解. (4认同)

小智 5

我只是在 .git/config 中设置了 filemode = false 并且它对我有用。

filemode = false
Run Code Online (Sandbox Code Playgroud)


Dan*_*non 5

就我而言,文件完全相同内容、行结尾、文件模式、md5 校验和,一切),但在git status.

有帮助的是

# Garbage collect stuffs from .git
git gc

# commit stuff
git add . 
git commit -m "temporary" 

# this doesn't change contents
git reset HEAD^1 --soft    
Run Code Online (Sandbox Code Playgroud)

之后,git status仅显示我编辑的一个编辑文件。