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

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

问题描述




  • Node.js项目带有一个存储库,一个存放在我的GitHub帐户中的存储库,这是我偶然发现的一个问题。安装了几个npm软件包的文件夹
  • 软件包位于 node_modules 文件夹中

  • 文件夹git存储库,并将代码推送到github(当时没有考虑npm部分)

  • 意识到您并不需​​要该文件夹成为代码

  • 删除该文件夹并将其推送到该文件夹​​中



在这种情况下, git repo大约是 6MB ,其中实际代码(除该文件夹外)都只有 300 KB



现在我最终要找的是从git的历史中删除该文件夹的详细信息的方法,所以如果有人克隆它,他们没有下载6mb的历史记录,其中最后提交的唯一实际文件将为300KB。



我为此寻找可能的解决方案,并尝试了这两个方法



它在运行脚本之后就工作了,它表明它已经摆脱了那个文件夹,之后它显示有50个不同的提交被修改。但它不让我推这个代码。当我试图推送它时,它表示分支最新,但显示50个提交在 git status 上修改。其他两种方法也没有帮助。



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



我想要澄清的是,如果有办法摆脱提交历史记录(这是我认为发生的唯一事情),而且git保留假设想要回滚的文件。



可以说为此提供了一个解决方案,并且应用于我的本地主机,但不能被复制到该GitHub仓库中,是否可以克隆该仓库,回滚到第一个提交执行该技巧并推动它(或这是否意味着git将仍然有所有这些提交的历史? - 又名6MB)。



我的最终目标是基本上找到摆脱git文件夹内容的最佳方式,以便用户不必下载6MB的内容,仍然可能有其他提交从未触及模块文件夹(这几乎所有这些)在git的历史。



我该怎么做?

解决方案

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



从历史记录中删除 node_modules

  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'从git历史记录中删除node_modules'
git gc
git push origin master --force


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

  • Node.js project with a folder with a few npm packages installed
  • The packages were in node_modules folder
  • Added that folder to git repository and pushed the code to github (wasn't thinking about the npm part at that time)
  • Realized that you don't really need that folder to be a part of the code
  • Deleted that folder, pushed it

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.

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

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.

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

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.

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

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.

How can I do this?

解决方案

If you are here to copy-paste code:

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 / GitHub的历史记录中删除文件夹及其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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