Number of lines annotated by Git is not equal to number of lines in the file, check file encoding and line separators.

项目中某个java文件左边栏右键->Annotate无法显示,IDEA提示Number of lines annotated by Git is not equal to number of lines in the file, check file encoding and line separators.

可能是之前某一次Commit的换行符有问题,正常应该是LF或者CRLF,那个文件是CR - Classic Mac ( )。

回想几天之前有一次Commit,IDEA提示:

当时选择了中间那个Commit As Is

简单解决方式是对那个java文件右下角由CR改为LF重新Commit即可,然后就能够查看Git Annotate了,但这样有个问题,之前的Annotate信息会丢失,全部变成最后提交者。
因为该java文件较大,接近1w行代码,有很多人修改过,希望能保留Annotate历史信息。

尝试了2种方式均失败:

  1. 对单个文件回退版本,git log --oneline Xxx.javagit reset verNum Xxx.java,注意单个文件不能添加--hard参数。
    再次Commit发现line separator没有变,仍然是CR无法查看Annatate。

  2. git reset --hard 回退到分支,在git cherry pick
    因为中间有同学的提交是个merge操作,直接cherry pick提示is a merge but no -m option was given,该merge操作涉及文件很多,尝试添加-m参数也不行。

最后突然想到个思路:
git reset --soft到有问题Commit之前的分支,然后按git log history依次git commit,最后git push -f,这样该大java文件的Annotate得以保留,依次Commit的信息变成了最后提交者,由于最近的Commit改动较小,折中考虑选择了此方案。

值得注意的是为了操作安全,以上操作是单独拉了单独分支(包括本地和远程),git操作测试无误后再对原分支进行操作。


参考:
https://confluence.jetbrains.com/display/IDEADEV/IntelliJ+IDEA+2018.3+EAP+183.2153.8+Release+Notes
https://www.jetbrains.com/help/idea/configuring-line-endings-and-line-separators.html
https://youtrack.jetbrains.com/issue/IDEA-194895
https://blog.csdn.net/m0_38072683/article/details/82927639
https://blog.csdn.net/ejennahuang/article/details/99591008

原文地址:https://www.cnblogs.com/cdfive2018/p/12880637.html