我有一个旧的提交,A
。当我提交 A
时,我更改了 2 个文件:F1
和 F2
。我想改变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/