从 Git 存储库和 GitHub 上的远程完全删除文件

发布于 2024-10-30 02:58:41 字数 221 浏览 0 评论 0原文

我不小心添加了一个图像文件夹并提交了。然后,我又做出了一项承诺。 然后我使用 git rm -f ./images 删除了这些文件并再次提交。

现在,我在该分支(主分支)中进行了更多提交。在我的 HEAD 中,我没有那个 ./static/images 文件夹。

因此,我的回购规模增加了很多。怎样才能彻底去除这些斑点呢?我也想从我的远程 GitHub 存储库中删除它。

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.

Right now, I have made a lot more commits in that branch (master). In my HEAD, I don't have that ./static/images folder.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小ぇ时光︴ 2024-11-06 02:58:41

这就是您要查找的内容:忽略不不删除文件。我建议您阅读该页面,但这里是要使用的具体命令:

git filter-branch --index-filter \
'git rm -r --cached --ignore-unmatch <file/dir>' HEAD

此外,要从 git 创建的缓存中删除所有已删除的文件,请使用:

rm -rf .git/refs/original/ && \
git reflog expire --all && \
git gc --aggressive --prune

您可以找到有关最后一个命令的更多信息,以及执行您想要的所有操作的脚本只需一次操作,此处: git:永久删除历史记录中的文件或文件夹

另一个链接有很多解释:删除敏感数据

[编辑] 另外,请参阅此 StackOverflow 问题:从 Git 历史记录中删除敏感文件及其提交

(命令是从上面链接的问题中 natacado 的答案复制的。)如果您已经从工作副本中删除了文件,则以下操作应该可以工作。找出添加了不需要的文件的提交的哈希值。然后做:

git filter-branch --index-filter \
'git update-index --remove filename' <introduction-revision-sha1>..HEAD
git push --force --verbose --dry-run
git push --force

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:

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.

Another links with lots of explanation: Remove sensitive data.

[Edit] Also, see this StackOverflow question: Remove sensitive files and their commits from Git history.

(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:

git filter-branch --index-filter \
'git update-index --remove filename' <introduction-revision-sha1>..HEAD
git push --force --verbose --dry-run
git push --force
撩起发的微风 2024-11-06 02:58:41

您可以重新调整整个分支的基础,并删除添加图像的提交和删除图像的提交。

git rebase -i master

您将看到一份提交列表。删除包含要删除的恶意提交的行。 (“dd”删除默认编辑器 vim 中的一行。然后用 ZZ 保存)

悬空提交将在自然 git 垃圾收集过程中被清理,您可以使用 Darhuuk 的答案中给出的命令强制执行此过程。

编辑:即使您已推送到远程存储库,这也将起作用,但您必须使用 --force 进行推送。 (这同样适用于 git filter-branch 解决方案)。

请注意,这对于从您的分支中退出的任何人来说都会非常烦人。他们应该参考“从上游rebase恢复”


想必您最初意外添加的图像是您希望保留的承诺的一部分。在这种情况下,您需要在变基期间编辑提交以拆分出您希望保留的部分。您可以通过用“e”(用于编辑)替换该提交的“rebase -i”列表中的“pick”来完成此操作。变基过程将在此处停止,您可以使用“git commit --amend”拆分提交。

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)

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.

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).

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".

拥抱没勇气 2024-11-06 02:58:41

我基本上是在重复答案与此Windows 格式化警告,只是为了它不会作为注释丢失。

请注意使用双引号而不是单引号,因为它取决于您使用的 shell 解析字符串的方式。

单线:

git filter-branch --index-filter "git rm -r --cached --ignore-unmatch <file/dir>" HEAD

多线:

git filter-branch --index-filter \
    "git rm -r --cached --ignore-unmatch <file/dir>" HEAD

I'm essentially repeating the answer combined with this Windows formatting caveat, only so it doesn't get lost as a comment.

Note the use of double-quotes rather than single quotes, because it depends on the shell that you're using how strings are parsed.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文