git rebase 删除仅包含行尾更改的提交

Posted

技术标签:

【中文标题】git rebase 删除仅包含行尾更改的提交【英文标题】:git rebase drops commit that only contains line-ending changes 【发布时间】:2022-01-10 18:16:11 【问题描述】:

我正在尝试重新设置包含修复某些文件行尾的提交的分支(通过将它们设为 LF 而不是 CRLF)。如果我尝试做一个普通的变基

git rebase origin/develop

我在行尾提交之后的提交中收到大量冲突行,因为行尾已更改,因此 git 认为每一行都已更改。但如果我改为尝试

git rebase -s recursive -Xignore-space-at-eol origin/develop

发生这种情况:

dropping f0eddc9f4244fb7ebdf9b3f514f875f6b34fb4b7 Fix line endings -- patch contents already upstream

如何让 git 忽略行尾更改以进行合并,而不是在检查是否需要补丁时?

【问题讨论】:

【参考方案1】:

我想出了一个解决方案。关键是在变基期间修改合并策略选项。这不受官方支持,因此使用此技术需要您自担风险!

    启动交互式变基(首先中止任何先前尝试的变基)。 在修复 rebase todo 文件中的行结尾的提交之后插入一个“break”行。 对变基 todo 文件进行任何其他所需的更改(如果有),然后继续变基。 当您到达修复提交的行尾时,如果存在冲突,请键入以下内容来解决冲突:
git diff --name-only --diff-filter=U > changed-files
xargs -a changed-files git checkout --ours --
xargs -a changed-files dos2unix
xargs -a changed-files git add
rm changed-files
git rebase --continue
    当 git rebase 在行尾提交后立即停止时,更改合并策略并继续:
echo recursive > .git/rebase-merge/strategy
echo ' --ignore-space-at-eol' > .git/rebase-merge/strategy_opts
git rebase --continue
    照常继续变基。

【讨论】:

以上是关于git rebase 删除仅包含行尾更改的提交的主要内容,如果未能解决你的问题,请参考以下文章

Git: git rebase 用法小结 (转)

【Git】rebase 用法小结

Git~rebase

sh Git rebase不会更改提交时间戳

如何仅通过一个“验证”来进行 Git rebase

使用 `git rebase` 更改旧的提交消息