用法
吉特 forget-blob file_to_forget
安装
从获取脚本 的github 并使其可执行。
要一步完成,请将以下内容粘贴到终端上
须藤 wget //raw.githubusercontent.com/nachoparker/git-forget-blob/master/git-forget-blob.sh -O / usr / local / bin/git-forget-blob sudo chmod +x / usr / local / bin/git-forget-blob
如果你不这样做’t like installing to / usr / local / bin 使用 须藤 , just copy 吉特的忘记blob 无论您在哪里。只要文件位于 $ PATH 具有执行权限。
细节
无论是由于错误还是改变主意,迟早我们都会处理使git仓库忘记文件的问题。
我们很快意识到 吉特 rm 并不足够,因为git记得该文件在我们的历史中曾经存在过,因此将保留对该文件的引用。
更糟糕的是,变基也不容易,因为对blob的任何引用都将阻止git垃圾收集器清理空间。这包括远程引用和reflog引用。
通常,只要存储库中存在一些大块的二进制Blob,我们就会遇到此问题 需要 持有,更糟糕的是,如果我们不得不不时更新它。这可能导致我们的存储库规模迅速增加。
输入 吉特的忘记blob
# Completely remove a file from a 吉特 repository history # # Copyleft 2017 通过 Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com> # GPL licensed (see end of file) * Use at your own risk! # # 用法: # 吉特的忘记blob file_to_forget # # Notes: # It rewrites history, therefore 将 change commit references function 吉特的忘记blob() { 吉特 repack -A ls .git/objects/pack/*.idx &>/dev/null || { echo "there is nothing to be forgotten in this repo" && return; } local BLOBS=( $( 吉特 verify-pack -v .git/objects/pack/*.idx | grep blob | \ awk '{ print $1 }' ) ) for ref in ${BLOBS[@]}; do local FILE="$( 吉特 rev-list --objects --all | grep $ref | awk '{ print $2 }' )" [[ "$FILE" == "$1" ]] && break unset FILE done [[ "$FILE" == "" ]] && { echo "$1 not found in repo history" && return; } 吉特 tag | xargs 吉特 tag -d 吉特 filter-branch --index-filter "吉特 rm --cached --ignore-unmatch $FILE" rm -rf .git/refs/original/ .git/refs/remotes/ .git/*_HEAD .git/logs/ 吉特 for-each-ref --format="%(refname)" refs/original/ | \ xargs -n1 --no-run-if-empty 吉特 update-ref -d 吉特 reflog expire --expire-unreachable=now --all 吉特 repack -A -d 吉特 修剪 } # License # # This script is free 软件; you can redistribute it and/or modify it # under the terms of the GNU General Public License as published 通过 # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This script is distributed in the hope that it 将 be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this script; if not, write to the # Free Software Foundation, Inc., 59 Temple Place, Suite 330, # Boston, MA 02111-1307 USA
简而言之,这
- 用途 吉特 filter-branch 申请 吉特 rm 每次提交
- 然后,它将删除所有可能的参考,包括遥控器,标签和reflog
- 接下来,它将删除未引用的包,并
- 最后,它使用git强制进行垃圾收集 gc –prune .
注意事项:
- 这会重写历史记录,因此强制推送,合并,冲突和此类细节 将 发生。
- 出于同样的原因,
标签将丢失并且提交哈希将改变。
记住,在尝试之前,请保留已回购的回购副本,并小心使用。
有用的工具nachoparker,已添加到我的个人库中。
很棒的工具!
有什么办法可以将它与提交哈希一起使用吗?我的问题是我有一个旧提交,添加了一个包含数百个文件的文件夹,这些文件总计约1GB(它’s an SDK).
此后,我们从存储库中删除了这些文件夹,但是,它们保留在历史记录和.git文件夹中的某个位置,使我们的存储库很大。
使用git-forget-blob对提交中的每个文件运行都会很繁琐(如果我什至可以找到这样的列表)。你有什么建议?
我猜你可以做(假设a6aa2117是错误的提交)
吉特 diff–仅名称a6aa2117 a6aa2117〜|同时阅读做gitgot-blob $ l;完成
这应该忘记 所有 以前的文件 改性 在那个提交中。确保您没有修改您不想在同一提交中忘记的任何其他文件。
如有疑问,请先做第一部分,再仔细检查
吉特 diff–仅名称a6aa2117 a6aa2117〜
可能需要很长时间
因为我的远程存储库大小仍然很大,我如何在远程存储库上执行此操作?
您好,
我已经使用了脚本(实际上很多次)似乎都还不错,从第二次开始我得到了一些消息,例如:
“file ‘filetodelte’在存储库中找不到’
但仍有一个github网址,该文件仍然存在。
可能是什么问题?
谢谢,
保罗