git秉承“代码安全为主”,每一次commit都会硬性做备份。
之前我使用自己的脚本
| #!/bin/bash |
| |
| |
| usage() { |
| echo "Usage: $0 [path] [lines]" |
| echo " path: local git repository" |
| echo " lines: how much files to show&remove, default 100" |
| echo |
| echo "eg1: $0 ~/local_repo 200" |
| echo "eg2: $0 ." |
| } |
| if [[ "$1" == "-h" || "$1" == "--help" ]]; then |
| usage |
| exit 0 |
| fi |
| |
| |
| cd $1 |
| head_lines=${2:-100} |
| |
| list(){ |
| |
| |
| |
| |
| |
| IFS=$'\n'; |
| |
| |
| objects=`git verify-pack -v .git/objects/pack/pack-*.idx | grep -v chain | sort -k3nr | head -n $head_lines` |
| |
| output="NO.,raw,pack,SHA,path" |
| i=0 |
| for y in $objects |
| do |
| i=$((i+1)) |
| |
| size=$((`echo $y | cut -f 5 -d ' '`/1024)) |
| |
| compressedSize=$((`echo $y | cut -f 6 -d ' '`/1024)) |
| |
| sha=`echo $y | cut -f 1 -d ' '` |
| |
| fileLoc=`git rev-list --all --objects | grep $sha` |
| |
| output="${output}\n${i},${size},${compressedSize},${fileLoc}" |
| done |
| |
| echo -e $output | column -t -s ', ' |
| } |
| list_hint() { |
| echo "Remember the first number of files to be deleted, then press q to exit. |
| 请记住要删除前多少个文件,然后按q退出。 |
| |
| All sizes are in kB's. The pack column is the size of the object, compressed, inside the pack file. |
| 所有大小均以kB为单位。“pack”列:pack内,raw文件压缩后的大小。" |
| } |
| |
| rm_local() { |
| git filter-branch --force --index-filter "git rm -rf --cached --ignore-unmatch $*" --prune-empty --tag-name-filter cat -- --all |
| } |
| |
| rm_all() { |
| rm_local "${files[*]}" |
| set -x |
| rm -rf .git/refs/original/ |
| git reflog expire --expire=now --all |
| git gc --prune=now |
| git gc --aggressive --prune=now |
| set +x |
| |
| cmd_danger="pushd ${1} && \ |
| git push origin $(git rev-parse --abbrev-ref HEAD) --force && \ |
| git remote prune origin && \ |
| popd" |
| |
| echo "$cmd_danger" | xclip -selection clipboard && copied_en="📋 copied, " && copied_cn="📋 已复制到剪贴板," |
| echo "${copied_en}please execute the below command manually to clean the remote repository: |
| ${copied_cn}请手动执行以下命令,以清理远程仓库:" |
| echo -e "$cmd_danger" |
| |
| } |
| |
| |
| |
| |
| set -e |
| trap 'echo "💡 Tips for line 40 error: ${head_lines} is too large, should be lesser. ${head_lines}太大了,再小点。"' EXIT |
| result=$(list) |
| trap - EXIT |
| |
| echo -e "$(list_hint)\n\n$result" | less -K -S |
| |
| files=$(echo "$result" | tail -n +2 | awk '{print $5}') |
| |
| echo -n "🗑 (ENTER to delete all listed) How many largest files to delete:" |
| read -r lines |
| [ -n "$lines" ] && head_lines=$lines |
| files=($(echo "$files" | head -n $head_lines)) |
| IFS=$' \t\n' |
| result=$(printf "%s\n" "${files[@]}") |
| set +e |
| echo -e "⚠️ 即将删除以下文件,按q继续,按Ctrl+C取消\n\n$result" | less -K && rm_all $1 |
但是这次弹了一个警告:
| git-filter-branch has a glut of gotchas generating mangled history |
| rewrites. Hit Ctrl-C before proceeding to abort, then use an |
| alternative filtering tool such as 'git filter-repo' |
| (https: |
| filter-branch manual page for more details; to squelch this warning, |
| set FILTER_BRANCH_SQUELCH_WARNING=1 |
省流
| ❯ pip install git-filter-repo |
| ❯ git filter-repo |
| NOTICE: Removing 'origin' remote; see 'Why is my origin removed?' |
| in the manual if you want to push back there. |
| (was https://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@github.com/AClon314/tinyrenderer) |
| Processed 303 blob sizes |
| Parsed 67 commits |
| New history written in 0.05 seconds; now repacking/cleaning... |
| Repacking your repo and cleaning out old unneeded objects |
| HEAD 现在位于 b01235c Lesson 4: perspective=v.xyz / (1-v.z/cam); cam=0.8~2, too large will likely orthogonal 正交 |
| 枚举对象中: 327, 完成. |
| 对象计数中: 100% (327/327), 完成. |
| 使用 12 个线程进行压缩 |
| 压缩对象中: 100% (174/174), 完成. |
| 写入对象中: 100% (327/327), 完成. |
| 总共 327(差异 186),复用 239(差异 145),包复用 0 |
| Completely finished after 0.11 seconds. |
| ❯ git remote add origin https://github.com/user/repo # 根据git提示 |
| ❯ git push |
本文作者:Nolca
本文链接:https://www.cnblogs.com/nolca/p/18667113
版权声明:本作品采用 收益分享revenue sharing 许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步