Remove files from un-pushed Git commit (but not last one)
我刚刚发现,在过去的提交中,我包含了不应该在存储库中的文件(
我看到了这些答案:从所有Git存储库提交历史记录中完全删除文件,从Git提交中删除文件,但它们似乎并没有回答我的特定问题。解决方案是什么?
你的情况
我创造了你描述的3个联合国推动承诺的情况:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $ git clone <url> $ touch file1 $ git add file1 $ git commit -m"Correct 1st commit" $ touch file2 $ touch webstormfile $ git add . $ git commit -m"Wrong 2nd commit with by mistake included WebStorm file" $ touch file3 $ git add file3 $ git commit -m"Correct 3rd commit" $ git log --oneline $ $ ad73bfa Correct 3rd commit $ eddae38 Wrong 2nd commit with by mistake included WebStorm file $ ad219bf Correct 1st commit |
正在删除提交的Webstorm文件
开始交互再平衡,包括错误提交的hash
1 | $ git rebase -i eddae38^ |
号
文本编辑器弹出以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | pick eddae38 Wrong 2nd commit with by mistake included WebStorm file pick ad73bfa Correct 3rd commit # Rebase ad219bf..ad73bfa onto ad219bf # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like"squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out |
在第一行中,将pick更改为edit,保存文件并关闭文本编辑器。
注意!如果你想保留你的webstorm文件,就复制一份。然后,从提交中删除webstorm文件:
1 | $ git rm .\webstormfile |
。
提交已清理索引:
1 | $ git commit --amend --no-edit |
完成再平衡:
1 | $ git rebase --continue |
。
用另一个提交删除文件,然后在最后一次推送的提交上运行
确保备份您的文件,因为您需要它们。在提交修复之后,恢复文件并将其添加到