如何在保留历史记录的同时用远程仓库的更改强行覆盖本地仓库?

How do I forcibly ovewrite a local repo with changes from a remote repo while still preserving history?

我知道如果我想用远程仓库中的文件强行覆盖本地仓库,我会这样做:

git fetch --all
git reset --hard origin/master

这个问题是当我去查看本地仓库的历史时,它已经完全抹杀了历史并用远程仓库历史的副本代替它。

有没有一种方法可以在保留历史记录的同时执行此强制覆盖?

例如,如果我在本地存储库中有一个包含以下内容的文件:

- ContentA
- ContentB
- ContentC

我在远程仓库中有相同的文件,内容如下:

- ContentA
- ContentC
- ContentD

从远程仓库拉取后,我希望历史记录显示以下文件:

Version #1
- ContentA
- ContentB
- ContentC

Version #2
- ContentA
- ContentC
- ContentD

我不希望它显示这个(来自强制覆盖):

Version #1
- ContentA
- ContentC
- ContentD

我也不希望它显示这个(来自合并):

Version #1
- ContentA
- ContentB
- ContentC

Version #2
- ContentA
- ContentB
- ContentC
- ContentD

这可能吗?

您不能让遥控器为您进行合并。您需要进行合并,然后将您拥有的内容推送给它,它只是快进到您的提交。所以你需要安排你的提交来解决你想要的任何东西,然后推送它

您可以使用 --no-commit 选项进行合并,然后使用 git checkout --ours 或 git checkout --theirs 选择您想要哪一方的文件,然后 git 添加这些文件,最后 git 提交以结束合并,select 您想要更改哪个文件。至于你想要的历史,你可以编辑实际的提交评论,如果那是你想要的。