删除提交但保留更改

时间:2017-06-29 14:04:05

标签: git

假设我有这样的提交历史:

commit 1 - The first commit
commit 2 - The commit I want to revert, but keep the changes of
commit 3 - The third commit
commit 4 - The fourth commit
[uncommited work]

如果我还没有推送任何内容(或者只提交1),我该如何删除提交2,但是将提交2中所做的更改应用于当前未提交的工作?

我所能找到的只是关于如何删除提交以及它的更改或重置为该提交,这将(根据我的理解)从历史记录中删除所有提交。

2 个答案:

答案 0 :(得分:2)

我会按照你想要的方式重新排序修改,然后选择提交2,存储弹出,然后将HEAD指针设置为“rebuilt”提交4:

git stash save "will come back"
git checkout commit1
git cherry-pick commit2..commit4
git cherry-pick  commit2
git stash pop
git reset --soft HEAD~1

应该这样做

答案 1 :(得分:0)

我相信git rebase -i HEAD~3 应该做这个伎俩。

然后,您将能够选择提交2进行编辑,更改提交的内容或已更改的文件,然后git将在重新编辑的提交2“重放”提交3和4

相关问题