删除旧的,已经推送的,git commit中的文件更改?

我有一个旧的提交A。当我提交A时,我更改了2个文件:F1F2。我想更改A,因此它仅修改文件F1。我想分别提交F2。我要从我的git存储库中删除F1

我尝试通过

git rebase -i HEAD~3 // The exact number doesn't matter,I just change "pick" to "edit" next to A
git reset HEAD^ // unstage all changes
git add F1 // don't add F2,we want to commit it separately
git rebase --continue

但是,这给出了错误

F2: needs update
You must edit all merge conflicts and then
mark them as resolved using git add

我通过做git rebase --skip

解决了问题

但是,这会完全从git历史记录中删除提交A

我在做什么错了?

我查看了SO上的其他帖子,并尝试了解决方案,但是它们没有用。

ppliang69 回答:删除旧的,已经推送的,git commit中的文件更改?

除了我必须添加1条命令外,我几乎完成了@Tim帖子中的建议。这是我所做的:

git rebase -i HEAD~3 // The exact number doesn't matter,I just change "pick" to "edit" next to A
git reset HEAD^ // unstage all changes
git add F2
git commit -m "Add F2"
git add F1
git commit -m "Add F1"
git rebase --continue

链接的帖子中未提及git reset HEAD^

,

这是我要这样做的方式:

git rebase -i HEAD~3 //Change "pick" to "edit" for commit to change
git reset HEAD^ -- F2 //reset F2 to previous version in staging area
git commit --amend //replace current commit with F1 change only
git add F2 // add new F2 back to staging area
git commit //commit F2 in a separate commit
git rebase --continue

请注意,与所有基准一样,此操作会重写历史记录,因此,只有在提交尚未推送到公共存储库的情况下,才应该这样做。

本文链接:https://www.f2er.com/3126440.html

大家都在问