从历史记录中删除文件夹及其内容 git/GitHub's

我在我的帐户上工作了 GitHub, 这是一个我遇到的问题。

Node.js 使用具有多个已安装软件包的文件夹 npm

包裹在文件夹中
node_modules


将此文件夹添加到存储库中 git 并推动代码 github /那时我没有想到部分 npm/

意识到,实际上你不需要这个文件夹是代码的一部分

删除此文件夹推她

在这种情况下,总大小 git repo 是的 6

MB.

, 虽然实际代码 /除了这个文件夹之外/ 弥补

300 KB

.

现在我在寻找什么, - 这是摆脱此包文件夹的详细信息的方法。 git, 所以,如果有人克隆她,他们不需要加载历史 6 MB,其中唯一的实际文件在上次固定时会收到/犯罪/, 将被制作 300 KB。

我正在寻找这个可能的解决方案并尝试了这些 2 方法

https://coderoad.ru/2164581/
/

http://help.github.com/remove-sensitive-data/
/

https://gist.github.com/1588371
在启动脚本后,精华似乎工作了他所示,他已经摆脱了这个文件夹,然后展示了它被改变了 50 不同的提交。 但他不允许我点击这个代码。 当我试图点击他时,他说
Branch up to date

, 但表明了这一点 50 承诺改为
git status

. 休息 2 该方法也没有帮助。

现在,虽然他表明他摆脱了这个文件夹的历史,但是当我检查我的这个repo的大小时 localhost, 他还在周围 6 MB。 /我也删除了文件夹
refs/original

, 但没有看到repo的大小的变化/.

我想澄清是否有一种方法,不仅可以摆脱固定史上/犯罪/ /在我看来,在我看来,唯一发生的事情/, 但是来自那些文件 git 商店,假设有人想要回滚。

假设解决方案为此提供并适用于我的 localhost, 但不能再现在这个 GitHub repo,无论是否可以克隆这个repo,滚动回到第一个提交,执行技巧并按下它 /或者这意味着 git 仍然有所有这些提交的故事吗? - 也被称为。 6 MB./.

这里的最终目标是主要找到摆脱文件夹内容的最佳方式 git, 这样用户无需下载 6 MB材料仍然是,也许是其他提交,从未关注模块文件夹 /这几乎都是他们/ 在历史上 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 'Removing node_modules from git history'
git gc
git push origin master --force


实际上是什么

git:

第一行通过同一树上的所有链接 /
--tree-filter

/, 我。 HEAD /你当前的分支机构/, 执行一支球队
rm -rf node_modules

. 此命令删除该文件夹。 node_modules /
-r

, 没有
-r

,
rm

不会删除文件夹/, 没有给予用户的请求 /
-f

/. 添加
--prune-empty

删除无用 /不改变/ Commith递归。

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

rest 球队比较简单。

卫东

赞同来自:

我发现参数
--tree-filter

, 用于其他响应,可以非常慢,尤其是在大量提交的大型存储库中。

以下是我用来完全删除历史目录的方法 git, 使用选项
--index-filter

, 哪个工作更快:


# Make a fresh clone of YOUR_REPO
git clone YOUR_REPO
cd YOUR_REPO

# Create tracking branches of all branches
for remote in `git branch -r | grep -v /HEAD`; do git checkout --track $remote ; done

# Remove DIRECTORY_NAME from all commits, then remove the refs to the old commits
# /repeat these two commands for as many directories that you want to remove/
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch DIRECTORY_NAME/' --prune-empty --tag-name-filter cat -- --all
git for-each-ref --format="%/refname/" refs/original/ | xargs -n 1 git update-ref -d

# Ensure all old refs are fully removed
rm -Rf .git/logs .git/refs/original

# Perform a garbage collection to remove commits with no refs
git gc --prune=all --aggressive

# Force push all branches to overwrite their history
# /use with caution!/
git push origin --all --force
git push origin --tags --force


您可以在之前和之后检查存储库的大小
gc

通过:


git count-objects -vH

帅驴

赞同来自:

除了受欢迎的答案

我想增加一些笔记

Windows

- 系统。 团队


git filter-branch --tree-filter 'rm -rf node_modules' --prune-empty HEAD


完美跑步

没有任何

修改! 所以

你不应该

使用
Remove-Item

,
del

或其他任何东西
rm -rf

.

如果需要指定文件或目录的路径,请使用



类型的类型
./path/to/node_modules

奔跑吧少年

赞同来自:

我找到的最好和最准确的方法是上传文件 bfg.jar:
https://rtyley.github.io/bfg-repo-cleaner/
/

然后执行命令:


git clone --bare [url=https://project/repository]https://project/repository[/url] project-repository
cd project-repository
java -jar bfg.jar --delete-folders DIRECTORY_NAME # i.e. 'node_modules' in other examples
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push --mirror [url=https://project/new-repository]https://project/new-repository[/url]


如果要删除文件,请使用该选项 delete-files:


java -jar bfg.jar --delete-files *.pyc

石油百科

赞同来自:

它看起来像答案 up-to-date 这个问题是

不是

使用
filter-branch

直接地 /至少 git 不再推荐它/ 并推迟在外部仪器上的工作。 特别是,
https://github.com/newren/git-filter-repo/
目前推荐 git-filter-repo. 这个工具的作者
https://github.com/newren/git- ... ranch
关于为什么直接使用
filter-branch

会导致问题。

上述大多数多行脚本要删除
dir

从历史可以重写如下:


git filter-repo --path dir --invert-paths


显然,该工具比那样强大。 您可以通过作者申请过滤器, email, refname 以及更多 /
https://htmlpreview.github.io/ ... .html
/. 另外,它是

快速地

. 安装简单 - 她
https://github.com/newren/git- ... LL.md
.

龙天

赞同来自:

完全的 copy&paste 配方,只是添加命令来评论 /对于解决方案 copy-paste/, 测试后:


git filter-branch --tree-filter 'rm -rf node_modules' --prune-empty HEAD
echo node_modules/ >> .gitignore
git add .gitignore
git commit -m 'Removing node_modules from git history'
git gc
git push origin master --force


之后,您可以删除字符串 "node_modules/" 的 .gitignore

知食

赞同来自:

为了 Windows 用户,请注意使用
"

反而
'

还添加了
-f

对于强制执行该命令,如果已经存在了另一个备份。


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

知食

赞同来自:

我删除了文件夹 bin 和 obj 来自旧项目 C#, 使用 git 在 windows. 小心他们


git filter-branch --tree-filter "rm -rf bin" --prune-empty HEAD


它破坏了安装的完整性 git, 删除文件夹 usr/bin 在安装文件夹中 git.

要回复问题请先登录注册