从 GitHub 上的 Git 存储库和远程文件中完全删除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5563564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Completely remove files from Git repo and remote on GitHub
提问by Abhijeet Rastogi
I accidentally added a folder of images and committed. Then, I made one more commit.
Then I removed those files using git rm -f ./images
and committed again.
我不小心添加了一个图像文件夹并提交。然后,我又提交了一次。然后我删除了这些文件git rm -f ./images
并再次提交。
Right now, I have made a lot more commits in that branch (master). In my HEAD, I don't have that ./static/images
folder.
现在,我在该分支(主)中进行了更多提交。在我的 HEAD 中,我没有那个./static/images
文件夹。
Due to this, my repo size has increased a lot. How can I remove those blobs completely? And I want to remove it from my remote GitHub repo too.
因此,我的回购规模增加了很多。我怎样才能完全删除这些斑点?我也想从我的远程 GitHub 存储库中删除它。
回答by Darhuuk
This is what you're looking for: ignoring doesn't remove a file. I suggest you read that page, but here's the specific command to use:
这就是您要查找的内容:忽略不会删除文件。我建议您阅读该页面,但这是要使用的特定命令:
git filter-branch --index-filter \
'git rm -r --cached --ignore-unmatch <file/dir>' HEAD
Also, to remove all the deleted files from caches git creates, use:
此外,要从 git 创建的缓存中删除所有已删除的文件,请使用:
rm -rf .git/refs/original/ && \
git reflog expire --all && \
git gc --aggressive --prune
You can find more info about the last command, as well as a script that does everything you want in one single action, here: git: forever remove files or folders from history.
您可以在此处找到有关最后一个命令的更多信息,以及在一个操作中完成您想要的所有操作的脚本,请访问:git: never remove files or folders from history。
Another links with lots of explanation: Remove sensitive data.
另一个有很多解释的链接:删除敏感数据。
[Edit]Also, see this StackOverflow question: Remove sensitive files and their commits from Git history.
[编辑]另外,请参阅此 StackOverflow 问题:从 Git 历史记录中删除敏感文件及其提交。
(Commands copied from natacado
's answer in the question linked above.) If you have already removed the files from the working copy, the following should work. Find out the hash for the commit that added the unwanted files. Then do:
(从natacado
上面链接的问题中的答案中复制的命令。)如果您已经从工作副本中删除了文件,则以下内容应该有效。找出添加不需要的文件的提交的哈希值。然后做:
git filter-branch --index-filter \
'git update-index --remove filename' <introduction-revision-sha1>..HEAD
git push --force --verbose --dry-run
git push --force
回答by RJFalconer
You could just rebase your whole branch and remove both the commit that added the images and the commit that removed them.
您可以重新设置整个分支并删除添加图像的提交和删除它们的提交。
git rebase -i master
You will be presented with a list of commits. Delete the lines that have the rogue commits you want to remove. ("dd" deletes a line in vim, the default editor. Then save with ZZ)
您将看到一个提交列表。删除包含要删除的恶意提交的行。(“dd”在默认编辑器vim中删除一行。然后用ZZ保存)
The dangling commits will then be cleaned up in the course of natural git garbage collection, a process you can force with the command given in Darhuuk's answer.
然后将在自然 git 垃圾收集过程中清除悬空提交,您可以使用 Darhuuk 的答案中给出的命令强制执行该过程。
Edit: This will work even if you have pushed to a remote repository, but you'll have to push with --force. (The same applies to the git filter-branch solution).
编辑:即使您已推送到远程存储库,这也将起作用,但您必须使用 --force 进行推送。(这同样适用于 git filter-branch 解决方案)。
Note that this will be very annoying to anyone who has pulled from your branch. They should consult "recovering from upstream rebase".
请注意,这对于从您的分支中拉出的任何人来说都会非常烦人。他们应该咨询“从上游变基中恢复”。
Presumably your original accidental addition of images is part of a commit you wish to keep. In this case, you need to edit the commit during the rebase to split out the parts you wish to keep. You can do this by replacing "pick" in the "rebase -i" list for that commit with "e" (for edit). The rebase process will stop here and you can split the commit with "git commit --amend".
据推测,您最初意外添加的图像是您希望保留的提交的一部分。在这种情况下,您需要在 rebase 期间编辑提交以拆分您希望保留的部分。您可以通过将该提交的“rebase -i”列表中的“pick”替换为“e”(用于编辑)来完成此操作。变基过程将在此处停止,您可以使用“git commit --amend”拆分提交。
回答by drzaus
I'm essentially repeating the answercombined with this Windows formatting caveat, only so it doesn't get lost as a comment.
我基本上是在重复结合此Windows 格式警告的答案,只是为了不会作为评论丢失。
Note the use of double-quotes rather than single quotes, because it depends on the shell that you're using how strings are parsed.
请注意使用双引号而不是单引号,因为这取决于您使用的 shell 如何解析字符串。
Single Line:
单线:
git filter-branch --index-filter "git rm -r --cached --ignore-unmatch <file/dir>" HEAD
Multiple Lines:
多行:
git filter-branch --index-filter \
"git rm -r --cached --ignore-unmatch <file/dir>" HEAD