【问题标题】:Git and IntelliJ lines separator issueGit 和 IntelliJ 行分隔符问题
【发布时间】:2016-05-31 21:24:51
【问题描述】:

我在IntelliJ 15.0.3 工作,并通过 Git Bash 使用 Git(提交和推送更改)。当我 fetch 来自远程 git 存储库的文件时 它包含不同的行分隔符(混合模式或它的调用方式)。我的意思是有些行以CRLF 结尾,有些行以LF 结尾(同一个文件)。

当我在 IDEA 中进行更改时 - 文件会自动保存,并且所有行分隔符都会减少(更改)为 IDEA 默认行分隔符(对我来说是LF)。

而 git 将这些更改视为对文件的更改,因此我提交的文件有很多如下更改:

- some line
+ some line

因为some line [CRLF] 已更改为some line [LF]

如何配置Git 以忽略这一点,或者如何配置IntelliJ IDEA 以使文件保持这种混合模式?我不想在没有更改的情况下提交更改。

【问题讨论】:

  • 在安装 GIT 时,可以选择“按原样签出,按原样提交”,这可能对您有所帮助?

标签: git intellij-idea


【解决方案1】:

在安装 git 时,我们可以选择设置 checkout as-is and commit as-is

如果没有设置,我们可以使用 git config

以下命令可帮助您完成此操作。

git config --global core.autocrlf true

根据文档:

    Git can handle this by auto-converting CRLF line endings into LF when you
 add a file to the index, and vice versa when it checks out code onto your 
filesystem. You can turn on this functionality with the core.autocrlf setting. 
If you’re on a Windows machine, set it to true – this converts LF endings into 

【讨论】:

  • 我已经添加了 git config --global core.autocrlf true 但是每当我提交文件时我仍然在 intellij 中遇到问题。 intellij 编辑器在提交后显示“内容仅在行分隔符中存在差异”。
  • 是的,这是因为您在获取后设置了此项。我认为您能做的最好的事情就是将更改复制到其他地方,使用git checkout -- <some file>git checkout -- *.*(丢弃所有更改)丢弃更改并再次获取。
【解决方案2】:

IDEA 将更改分辨率委托给 git。

所以你必须要么force desired line breaks in IDEA要么to force desired line breaks in git

在我的情况下,autocrlf true 已经存在于我的 PC 上,但工作副本最初是从同事的共享中复制的,在该共享中已通过其他设置签出。

干净的结帐可以解决问题,但我有一些文件已经更改并想保留它们。 这可以通过重置 whole project 或只是想要的 subdirectory 的 git 索引来解决。

【讨论】:

    【解决方案3】:

    尝试将以下配置添加到您的 .gitconfig(仅适用于基于 unix 的操作系统)

    [core]
        autocrlf = input
        eol = lf
    

    您也可以在终端中进行此类更改:

     git config --global core.eol lf
     git config --global core.autocrlf input
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 2011-04-15
      相关资源
      最近更新 更多