简体   繁体   English

从未推送的 Git 提交中删除文件(但不是最后一个)

[英]Remove files from un-pushed Git commit (but not last one)

I just found out that, in a past commit, I included files that should not be in the repository ( .idea Webstorm files and others).我刚刚发现,在过去的提交中,我包含了不应该在存储库中的文件( .idea Webstorm 文件和其他文件)。 This commit "sits in the middle" with unpushed commits before it and after it.这个提交“位于中间”,在它之前和之后都有未推送的提交。 Also, within this commit are files that do need to be committed and that were furher modified in later (unpushed) commits.此外,在此提交中包含确实需要提交并在以后(未推送)提交中进一步修改的文件。

I have seen these answers: Completely remove file from all Git repository commit history and Remove files from Git commit but they don't seem to answer my specific issue.我已经看到了这些答案: 从所有 Git 存储库提交历史记录中完全删除文件从 Git 提交中删除文件,但它们似乎没有回答我的具体问题。 What is the solution?解决办法是什么?

Your situation你的情况

I'm creating the situation you described with 3 un-pushed commits:我正在使用 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

Removing committed WebStorm files删除提交的 WebStorm 文件

Beware: the following steps are going to change the history, meaning, if the commits have already been pushed, changing the history will break the repository for all team members.注意:以下步骤将更改历史记录,这意味着,如果提交已经被推送,更改历史记录将破坏所有团队成员的存储库。 If you haven't pushed yet, you can follow the steps without bothering your team members.如果您还没有推送,您可以按照步骤操作,而不会打扰您的团队成员。

Start interactive rebasing including the hash eddae38 of the wrong commit:开始交互式eddae38 ,包括错误提交的哈希eddae38

$ git rebase -i eddae38^

The text editor pops up with the following content:文本编辑器弹出如下内容:

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

In the first line, change pick to edit , save the file and close the text editor.在第一行中,将pick更改为edit ,保存文件并关闭文本编辑器。

Attention!注意力! Make a copy of your WebStorm file if want to keep it.如果想保留它,请复制您的 WebStorm 文件。 Then, remove the WebStorm file from the commit:然后,从提交中删除 WebStorm 文件:

$ git rm .\webstormfile

Commit the cleaned up index:提交清理后的索引:

$ git commit --amend --no-edit

Finish the rebasing:完成变基:

$ git rebase --continue

Delete the files with another commit, then run git rebase -i on the last pushed commit and squash the wrong commit with the one that deletes the files使用另一个提交删除文件,然后在最后一次推送的提交上运行git rebase -i并使用删除文件的提交压缩错误的提交

Make sure you backup your files, as you'll need them.确保备份您的文件,因为您将需要它们。 After the commit fix, restore the files and add them to .gitignore to prevent further commits提交修复后,恢复文件并将它们添加到.gitignore以防止进一步提交

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM