从 git/GitHub 的历史记录中删除文件夹及其内容 [英] Remove folder and its contents from git/GitHub's history

查看:27
本文介绍了从 git/GitHub 的历史记录中删除文件夹及其内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 GitHub 帐户上处理一个存储库,这是我偶然发现的一个问题.

I was working on a repository on my GitHub account and this is a problem I stumbled upon.

  • 带有安装了几个 npm 包的文件夹的 Node.js 项目
  • 软件包位于 node_modules 文件夹中
  • 将该文件夹添加到 git 存储库并将代码推送到 github(当时没有考虑 npm 部分)
  • 意识到您实际上并不需要将该文件夹作为代码的一部分
  • 删除那个文件夹,推送它

那时,整个 git 存储库的大小约为 6MB,而实际代码(除该文件夹外的所有代码)仅约为 300 KB.

At that instance, the size of the total git repo was around 6MB where the actual code (all except that folder) was only around 300 KB.

现在我最终要寻找的是一种从 git 历史记录中删除该包文件夹详细信息的方法,因此如果有人克隆它,他们不必下载价值 6mb 的历史记录,其中只有他们的实际文件将在最后一次提交时获得 300KB.

Now what I am looking for in the end is a way to get rid of details of that package folder from git's history so if someone clones it, they don't have to download 6mb worth of history where the only actual files they will be getting as of the last commit would be 300KB.

我为此查找了可能的解决方案并尝试了这两种方法

I looked up possible solutions for this and tried these 2 methods

Gist 似乎在运行脚本后可以正常工作,它表明它摆脱了该文件夹,然后表明修改了 50 个不同的提交.但它并没有让我推送那个代码.当我尝试推送它时,它说 Branch up to date 但显示在 git status 上修改了 50 个提交.其他 2 种方法也无济于事.

The Gist seemed like it worked where after running the script, it showed that it got rid of that folder and after that it showed that 50 different commits were modified. But it didn't let me push that code. When I tried to push it, it said Branch up to date but showed 50 commits were modified upon a git status. The other 2 methods didn't help either.

现在,即使它显示它删除了该文件夹的历史记录,当我在我的本地主机上检查该存储库的大小时,它仍然在 6MB 左右.(我也删除了 refs/original 文件夹,但没有看到 repo 大小的变化).

Now even though it showed that it got rid of that folder's history, when I checked the size of that repo on my localhost, it was still around 6MB. (I also deleted the refs/originalfolder but didn't see the change in the size of the repo).

我想澄清的是,是否有一种方法可以摆脱提交历史记录(这是我认为唯一发生的事情),而且还可以消除 git 一直假设要回滚的那些文件.

What I am looking to clarify is, if there's a way to get rid of not only the commit history (which is the only thing I think happened) but also those files git is keeping assuming one wants to rollback.

假设为此提出了一个解决方案并应用于我的本地主机,但无法复制到该 GitHub 存储库,是否可以克隆该存储库,回滚到第一次提交执行该技巧并推送它(或者这是否意味着那个 git 仍然会有所有这些提交的历史记录? - 也就是 6MB).

Lets say a solution is presented for this and is applied on my localhost but cant be reproduced to that GitHub repo, is it possible to clone that repo, rollback to the first commit perform the trick and push it (or does that mean that git will still have a history of all those commits? - aka. 6MB).

我的最终目标是基本上找到从 git 中删除文件夹内容的最佳方法,这样用户就不必下载价值 6MB 的东西,并且仍然可能有其他从未触及模块文件夹的提交(几乎就是所有这些)在 git 的历史中.

My end goal here is to basically find the best way to get rid of the folder contents from git so that a user doesn't have to download 6MB worth of stuff and still possibly have the other commits that never touched the modules folder (that's pretty much all of them) in git's history.

我该怎么做?

推荐答案

警告:git filter-branch 是 不再正式推荐

WARNING: git filter-branch is no longer officially recommended

如果您在这里复制粘贴代码:

If you are here to copy-paste code:

这是一个从历史记录中删除 node_modules 的示例

This is an example which removes node_modules from history

git filter-branch --tree-filter "rm -rf node_modules" --prune-empty HEAD
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
echo node_modules/ >> .gitignore
git add .gitignore
git commit -m 'Removing node_modules from git history'
git gc
git push origin master --force

git 实际上做了什么:

第一行遍历与 HEAD(您当前的分支)相同的树(--tree-filter)上的所有引用,运行命令 rm -rf node_modules.此命令删除 node_modules 文件夹(-r,没有 -rrm 不会删除文件夹),不提示用户(-f).添加的 --prune-empty 递归删除无用(不改变任何东西)提交.

The first line iterates through all references on the same tree (--tree-filter) as HEAD (your current branch), running the command rm -rf node_modules. This command deletes the node_modules folder (-r, without -r, rm won't delete folders), with no prompt given to the user (-f). The added --prune-empty deletes useless (not changing anything) commits recursively.

第二行删除对该旧分支的引用.

The second line deletes the reference to that old branch.

其余的命令相对简单.

这篇关于从 git/GitHub 的历史记录中删除文件夹及其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆