git-merge 是否可以忽略行结束差异?
git merge 是否可以忽略行结束差异?
也许我问错了问题......但是:
我尝试了 uisng config.crlf input
但事情变得有点混乱并且失控,特别是当我事后应用它时 /em>.
一方面,事后应用此配置似乎不会影响在应用此选项之前提交到存储库的文件。 另一件事是,突然间所有提交都会导致许多有关 CRLF 转换为 LF 的恼人警告消息。
老实说,我并不关心使用什么换行符,我个人更喜欢 Unix 风格 \n
,但无论如何。 我所关心的只是让 git merge 变得更聪明并忽略行结尾的差异。
有时我有两个相同的文件,但 git 会将它们标记为冲突(冲突是整个文件),只是因为它们使用不同的行结束字符。
更新:
我发现 git diff
接受 --ignore-space-at-eol
选项,是否可以让 git merge
也使用这个选项吗?
Is it possible for git merge
to ignore line-ending differences?
Maybe I'm asking the wrong question ... but:
I tried uisng config.crlf input
but things got a bit messy and out of control, specially when I applied it after the fact.
For one thing, applying this config after the fact doesn't seem to affect files that were committed to the repository before applying this option. Another thing is that suddenly all commits now result in lots of annoying warning messages about CRLF being converted to LF.
To be honest, I don't really care what line-ending is used, I personally prefer the Unix style \n
, but whatever. All I care about, is for git merge
to be a bit smarter and ignore the differences in line-endings.
Sometimes I have two identical files, but git would mark them as being in conflict (and the conflict is the whole file) simply because they use a different line ending character.
Update:
I found out that git diff
accepts a --ignore-space-at-eol
option, would it be possible to let git merge
use this option as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
阅读https://stackoverflow.com/a/12194759/1441706和https://stackoverflow.com/a/14195253/1441706
对我来说,这个命令完美地完成了任务:
After reading https://stackoverflow.com/a/12194759/1441706 and https://stackoverflow.com/a/14195253/1441706
for me, this command did the trick perfectly:
不过我建议使用 sed 等工具来实现正确的行结尾,然后比较文件。 我花了几个小时来比较具有不同行结尾的项目。
最好的方法是:
dev
(git checkout -b dev
),在此分支上提交文件并运行(如果第一个项目位于掌握):git diff master..dev --names-only
仅查看已更改文件的名称
however i suggest to use tool like sed to achieve correct line endings, and then diff files. I spent couple hours on diffing projects with various line endings.
The best way was to:
.git
directory) to another directory create repository in it, then add files and commit them (should be on master branch in new repository).dev
(git checkout -b dev
), commit files on this branch and run (if first project is in master):git diff master..dev --names-only
to see names of changed files only
2013 年更新:
较新的 git 版本授权使用策略
递归
和策略选项(-X
)进行合并:但使用“
-Xignore-space-change
”也是一种可能性jakub.g 还有评论,策略也适用于挑选:
这比
ignore-all-space
效果要好得多。在 Git 2.29(2020 年第 4 季度)之前,内部使用合并递归机制的所有“合并”操作都应遵循
merge.renormalize
配置,但其中许多操作并没有这样做。请参阅 提交 00906d6、提交 8d55225, 提交 6f6e7cf,提交 fe48efb(2020 年 8 月 3 日),作者:伊利亚·纽伦 (
newren
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 4339259,2020 年 8 月 10 日)原始答案(2009年5月)
忽略 eol 样式的补丁已在 2007 年 6 月,但它只涉及
git diff --ignore-space-at-eol
,而不是git merge
。当时有人问这样的问题:
Julio C Hamano 并不是很热情:
当谈到
git merge
时,一般的想法是依赖第三方合并工具。例如,我设置了DiffMerge 成为 Git 合并工具,设置一个规则集,允许合并工具忽略某些类型文件的 eol 。
在 Windows 上使用 MSysGit1.6.3 进行设置,对于 DOS 或 Git bash 会话,使用 DiffMerge 或 KDiff3:
c:\HOMEWARE\cmd
)。merge.sh:
Git 配置命令:
检查系统级别的
DOS 脚本(注意:dos2unix 命令来自此处,用于模拟 Unix eol 风格。该命令已复制到本答案开头提到的目录中。):
此时(点击“return”)。 "),DiffMerge 或 KDiff3 将打开,您将亲眼看到哪些行被实际合并,哪些行被忽略。
警告:使用 DiffMerge 时结果文件将始终处于 Windows eol 模式 (CRLF)...
KDiff3 提供以一种或另一种方式保存。
Update 2013:
More recent git versions authorize using merge with strategy
recursive
and strategy option (-X
):But using "
-Xignore-space-change
" is also a possibilityjakub.g also comments that the strategies work also with cherry-picking:
This works much better than
ignore-all-space
.Before Git 2.29 (Q4 2020), All "mergy" operations that internally use the merge-recursive machinery should honor the
merge.renormalize
configuration, but many of them didn't.See commit 00906d6, commit 8d55225, commit 6f6e7cf, commit fe48efb (03 Aug 2020) by Elijah Newren (
newren
).(Merged by Junio C Hamano --
gitster
-- in commit 4339259, 10 Aug 2020)Original answer (May 2009)
The patch for ignoring eol style has been proposed in June 2007, but it only concerns
git diff --ignore-space-at-eol
, notgit merge
.At the time, the question has been askeed:
Julio C Hamano was not exactly enthusiastic:
The general idea, when it comes to
git merge
, is to rely on the third-party merge tool.For instance, I have setup DiffMerge to be the tool for Git merge, setting a ruleset which allow that merge tool to ignore eol for certain type of files.
Setup on Windows, with MSysGit1.6.3, either for DOS or Git bash session, with DiffMerge or KDiff3:
c:\HOMEWARE\cmd
).merge.sh:
Git config commands:
git config at system level:
DOS script (note: the dos2unix command comes from here, and is used to simulate a Unix eol-style. That command has been copied in the directory mentioned at the beginning of this answer.):
At this point (Hitting "return"), DiffMerge or KDiff3 will open, and you will see for yourself what lines are actually merged, and what lines are ignored.
Warning: the result file will always be in Windows eol mode (CRLF) with DiffMerge...
KDiff3 offers to save in one way or another.
我一直在寻找相同的答案,我发现这个
因此,在任何存储库中运行此命令都可以解决问题:
I was looking for the same answer and I found out this
So running this command in any repository will do the trick:
正如这个答案: https://stackoverflow.com/a/5262473/943928
你可以尝试:
git合并-s递归-Xignore-space-at-eol
As in this answer: https://stackoverflow.com/a/5262473/943928
You could try:
git merge -s recursive -Xignore-space-at-eol
我所做的是将所有内容保留为默认值(即 autocrlf=true),触摸所有文件(find . -exec touch {} \;),让 git 将它们视为“已修改”并将它们提交回来,然后完成。 否则,你要么总是被烦人的消息或令人惊讶的差异所困扰,要么不得不关闭 git 的所有空白功能。
你会失去责备信息,但最好是早点做而不是晚点:)
What I did was leave everything as default (i.e. autocrlf=true), touch all files (find . -exec touch {} \;), let git see them as 'modified' and commit them back, and be done with it. Otherwise you'll always either be plagued by annoying messages or surprising differences, or have to turn off all of git's whitespace features.
You'll lose blame information, but it's better to do it sooner rather than later :)
“git merge -Xrenormalize”就像一个魅力。
"git merge -Xrenormalize" works like a charm.
现在在我看来,最好的方法是在合并两个分支(和提交)之前规范化它们的行结尾。
我用谷歌搜索“将 crlf 转换为 lf”,发现这是第一个结果:
http://stahlforce.com/dev/index.php?tool=remcrlf
我下载并使用了它,看起来是一个不错的工具。
请务必指定目录和文件类型(例如 .py),否则它可能会尝试弄乱
.git
目录的内容!It seems to me now that the best way is to normalized the line endings on both branches (and commit) before merging them.
I googled "convert crlf to lf" and found this as the first results:
http://stahlforce.com/dev/index.php?tool=remcrlf
I downloaded it and used, seems like a nice tool.
Be sure though to specify a directory and a file type (e.g. .py) otherwise it might try to mess with the contents of the
.git
directory!AFAICT,(我还没有尝试过)您可以使用 git diff 来比较要合并到共同祖先的分支,然后使用 git apply 应用结果。 这两个命令都有
--ignore-whitespace
选项来忽略行尾和空格错误。不幸的是,如果补丁没有完全应用,整个操作就会中止。 您无法修复合并冲突。 有一个
--reject
选项可以在.rej
文件中保留不可修补的块,这会有所帮助,但与在一个文件中显示合并冲突不同。AFAICT, (I haven't tried it) you could use
git diff
to compare the branch you want to merge to the common ancestor, then apply the results withgit apply
. Both commands have--ignore-whitespace
options to ignore line ending and white space errors.Unfortunately, if the patch doesn't apply cleanly, the whole operation is aborted. You can't fix merge conflicts. There is a
--reject
option to leave unpatchable hunks in.rej
files, which helps, but isn't the same as having the merge conflicts shown in one file.阅读解决合并冲突:强制覆盖所有文件后,
我终于解决了这个问题的版本。 我试图从上游存储库中提取更新,但我当前的存储库存在 CRLF 相关问题,因此无法合并。 应该指出的是,我没有需要担心的局部变化。 以下步骤解决了我的问题:
按照 github 关于同步分叉的说明 (https:// help.github.com/articles/syncing-a-fork/):
git fetchupstream
git重置--hard上游/master
我对 git 的有限理解告诉我,这正在做我想做的事——重新调整我的分支(没有实际未提交的更改)以获得对上游源代码所做的所有更改。 根据源页面,通常不需要此步骤,但 CRLF 问题使其成为必需。
git 合并上游/master
git Push
After reading Resolve merge conflicts: Force overwrite all files
I finally resolved my version of this issue. I was trying to pull updates from the upstream repo but my current one was having CRLF related issues and was unable to merge as result. It should be noted I had NO LOCAL CHANGES i needed to worry about. The following steps resolved my issue:
As per github's instructions on syncing forks (https://help.github.com/articles/syncing-a-fork/):
git fetch upstream
git reset --hard upstream/master
My limited understanding of git tells me this is doing what i want-- rebasing my fork (with no actual uncommitted changes) to gain all the changes made to the upstream source. According to the source page, this step should normally not be required, but the CRLF issue made it required.
git merge upstream/master
git push