git - 删除旧的、已推送的 git commit 中对文件的更改?

标签 git git-rebase

我有一个旧的提交,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 上的其他帖子,并尝试了解决方案,但没有成功。

最佳答案

这是我的做法:

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

请注意,正如所有 rebase 、重写历史记录一样,只有在提交尚未推送到公共(public)存储库时才应执行此操作。

关于git - 删除旧的、已推送的 git commit 中对文件的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58795826/

相关文章:

git - "git rebase --interactive"在尝试重新排序时静默丢弃提交

git - rebase 很重要吗?

git - 我如何确定哪些提交在 git 中被压缩了?

linux - 通过 GPRS 部署到嵌入式设备

Git 同步到上游 : Newly created branch not showing in my fork

iphone - 在 iPhone 项目中使用 git

java - 如何使用 egit/eclipse 配置工作区,以便我的更改转到 fork 上的分支

Git: rebase merge 提交

git - 在 Git 中的根提交之前插入一个提交?

git - 从特定提交中恢复对特定文件的更改