删除旧文件的更改,已推送,git 提交?

Remove changes to file in old, already pushed, 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 上的其他帖子,并尝试了解决方案,但它们没有用。

我几乎按照@Tim post 的建议做了,除了我必须添加 1 个命令。这是我所做的:

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

链接 post 中未提及 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

请注意,与所有变基一样,这会重写历史记录,因此只有在提交尚未推送到 public 存储库时才应该这样做。