Git 没有更改的合并提交删除对文件的编辑

Git merge commit with no changes removes edits to file

我和一位同事正在开发一个使用 git 进行源代码控制的项目。今天下午,我对特定文件 MyFile.txt 进行了多次提交。我将我的更改推送到我们的主分支 (trunk),不久之后我的同事合并了他的(不相关的)更改。这是视觉效果:

在我的同事进行合并提交后(上面标记为 1.0.1834 的提交),我的所有更改都消失了!

我切换回我的提交(标记为 1.0.1833)以验证我是否正确提交了我的更改;我做了 - MyFile.txt 从那天下午开始我的所有更改。但是,如果我切换到我同事的合并提交(标记为 1.0.1834),MyFile.txt 处于其原始状态而没有我的任何更改。

我同事的合并提交 (1.0.1834) 不包含任何更改 - gitk 中的 "Diff" window 是空的。这是我们 GitLab 站点提交的另一个视图:

发生什么事了?我的更改去了哪里?我怎样才能防止这种情况在未来发生?

如果你的同事做了 git merge --ours

就会发生这种情况

生成的合并提交将与他之前的提交 HEAD 相同。

这是我们的合并策略:

the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches.
Note that this is different from the -Xours option to the recursive merge strategy.