你的情况
我正在使用 3 个未推送的提交来创建您描述的情况:
$ 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 文件
注意:以下步骤将更改历史记录,这意味着,如果提交已被推送,更改历史记录将破坏所有团队成员的存储库。如果您还没有推送,您可以按照步骤操作,而不会打扰您的团队成员。
开始交互式变基,包括错误提交的哈希 eddae38:
$ git rebase -i eddae38^
文本编辑器弹出如下内容:
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 文件:
$ git rm .\webstormfile
提交清理后的索引:
$ git commit --amend --no-edit
完成变基:
$ git rebase --continue