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

git rebase drops commit that only contains line-ending changes

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

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 忽略出于合并目的的行结束更改,而不是在检查是否需要补丁时?

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

  1. 开始交互式变基(首先中止任何先前尝试的变基)。
  2. 在 rebase todo 文件中修复行结尾的提交后插入一个“中断”行。
  3. 对变基待办文件进行任何其他所需的更改(如果有),然后继续进行变基。
  4. 当你到达修复提交的行尾时,如果有冲突,输入以下内容解决冲突:
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
  1. 当git rebase 在行尾提交后立即停止时,更改合并策略并继续:
echo recursive > .git/rebase-merge/strategy
echo ' --ignore-space-at-eol' > .git/rebase-merge/strategy_opts
git rebase --continue
  1. 照常继续变基。