在 Git 中,如何修复删除然后重新添加历史记录中的提交?
In Git how to fix a delete then re-add commit that's way back in the history?
在一个项目中,分包商在树中移动了一些文件,进行了提交(Git 将它们标记为已删除),使用 git add
将它们重新添加到树中。那发生在几次提交之前。在我将更改合并回我的树之前,我想解决这个问题。我怎样才能在 git 历史记录中的正确位置“重新连接”这些文件?
更新
好的,所以,因为人们建议撤消提交。这不是我想要的(我认为)。
想象一下下面的情况
A
|
|\
| \
| B
| mv file_x file_y
| git commit 1
| |
| |
| git commit 2
| |
| |
| git commit 3
| |
| |
| git add file_y
| git commit 4
| /
|/
|
我想将 file_x 的历史“拼接”到提交 1 和 file_y 自提交 4 以来,而不丢失在提交 1 和 4 之间发生的任何其他更改.
一种可能是创建第二个分支,将第一个分支重置为有问题的提交之前的状态,然后从第二个分支中挑选出所需的提交。
git checkout branch1
git checkout -b branch2
git reset --hard <commit sha>
git cherry-pick <commit sha from branch 2>
...
在一个项目中,分包商在树中移动了一些文件,进行了提交(Git 将它们标记为已删除),使用 git add
将它们重新添加到树中。那发生在几次提交之前。在我将更改合并回我的树之前,我想解决这个问题。我怎样才能在 git 历史记录中的正确位置“重新连接”这些文件?
更新
好的,所以,因为人们建议撤消提交。这不是我想要的(我认为)。
想象一下下面的情况
A
|
|\
| \
| B
| mv file_x file_y
| git commit 1
| |
| |
| git commit 2
| |
| |
| git commit 3
| |
| |
| git add file_y
| git commit 4
| /
|/
|
我想将 file_x 的历史“拼接”到提交 1 和 file_y 自提交 4 以来,而不丢失在提交 1 和 4 之间发生的任何其他更改.
一种可能是创建第二个分支,将第一个分支重置为有问题的提交之前的状态,然后从第二个分支中挑选出所需的提交。
git checkout branch1
git checkout -b branch2
git reset --hard <commit sha>
git cherry-pick <commit sha from branch 2>
...