Git 倉庫中有時會不小心加入了一些大文件,例如模型文件,視頻文件
模型文件有可能以 .weights 結(jié)尾,或者 .onnx 結(jié)尾等等
視頻文件有可能以 .avi 結(jié)尾,或者 .mp4 結(jié)尾
大文件如果一直在倉庫中,倉庫體積會非常大,下面我們提供一個腳本,專門用于清理 倉庫中的文件使用。
倉庫清理腳本
本腳本旨在從指定的 Git 倉庫中移除不必要的 .weights
文件,優(yōu)化倉庫,并將更改推送到遠程服務(wù)器。它提供了用戶交互、詳細日志輸出及錯誤處理功能,以實現(xiàn)順暢且可靠的清理過程。
使用前準備
在使用本腳本之前,請確保滿足以下條件:
- 您擁有一個包含待移除
.weights
文件的 Git 倉庫。 - 已在系統(tǒng)上安裝 Git。
可選:為了提升性能,建議安裝 git-filter-repo 工具(通常通過 pip install git-filter-repo
安裝)。如果安裝了 git-filter-repo
,腳本將自動使用它替代 git filter-branch
命令進行更高效的歷史清理。
使用方法
-
下載或復制腳本:將以下腳本保存為
clean_repo.sh
文件。
#!/bin/bash
# 清理指定倉庫中無用的.weights文件
# 通過命令行參數(shù)接收倉庫地址(文件夾路徑)
# 獲取倉庫路徑(優(yōu)先使用命令行參數(shù),否則使用當前目錄)
if [ -n "$1" ]; then
repository_path="$1"
else
repository_path="."
fi
# 檢查路徑是否存在且為Git倉庫
if [ ! -d "$repository_path" ] || ! (cd "$repository_path" && git rev-parse --is-inside-work-tree &>/dev/null); then
echo "Error: The specified path '$repository_path' is not a valid Git repository."
exit 1
fi
# 用戶確認是否繼續(xù)
read -p "Are you sure you want to proceed with cleaning the repository at '$repository_path'? [y/N] " confirm
confirm=${confirm,,} # Convert to lowercase
if [[ $confirm != "y" ]]; then
echo "Aborting the operation."
exit 0
fi
cd "$repository_path" || exit 1 # 切換到指定倉庫目錄,若失敗則退出腳本
# 清理垃圾文件并記錄結(jié)果
echo "Cleaning up unnecessary .weights files..."
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch *.weights' --prune-empty --tag-name-filter cat -- --all || {
echo "Error: Failed to clean up .weights files. Check the output above for details."
exit 1
}
# 記錄清理前后的倉庫大小
before_size=$(du -sh .git | cut -f1)
echo "Repository size before cleanup: $before_size"
# 提交到遠程倉庫
echo "Pushing changes to remote repository (this may take some time)..."
git push origin --force --all || {
echo "Error: Failed to push changes to the remote repository. Check your network connection and authentication settings."
exit 1
}
# 回收垃圾并壓縮本地倉庫
echo "Performing garbage collection and compression..."
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
# 可選:進行更深度的壓縮(視情況決定是否需要)
git gc --aggressive --prune=now
# 記錄清理后的倉庫大小
after_size=$(du -sh .git | cut -f1)
echo "Repository size after cleanup: $after_size"
echo "Cleanup completed successfully."
-
賦予執(zhí)行權(quán)限:在終端中,使用
chmod +x clean_repo.sh
命令為腳本賦予執(zhí)行權(quán)限。 -
執(zhí)行腳本:文章來源:http://www.zghlxwxcb.cn/news/detail-858808.html
-
指定倉庫路徑:運行
./clean_repo.sh /path/to/repository
,其中/path/to/repository
是您要清理的倉庫路徑。 -
使用當前目錄:如果您想清理當前目錄下的倉庫,只需運行
./clean_repo.sh
。
腳本將引導您完成確認、清理、推送、壓縮等步驟,并在過程中輸出詳細日志。文章來源地址http://www.zghlxwxcb.cn/news/detail-858808.html
-
指定倉庫路徑:運行
注意事項
- 謹慎操作:清理操作會修改倉庫歷史,可能導致分支合并復雜性增加。請確保所有團隊成員知曉此次清理,并在執(zhí)行前備份重要數(shù)據(jù)。
- 權(quán)限要求:執(zhí)行清理和推送操作需具有相應(yīng)的 Git 權(quán)限。確保您有權(quán)修改所清理倉庫的歷史記錄及向遠程服務(wù)器推送更改。
- 性能影響:對于大型倉庫,清理和壓縮過程可能耗時較長。請耐心等待,并確保網(wǎng)絡(luò)連接穩(wěn)定。
- 清理效果:清理后,倉庫在本地和遠程的大小可能不會立即更新。請等待一段時間后刷新查看,或聯(lián)系服務(wù)提供商確認是否需要手動觸發(fā)更新。
到了這里,關(guān)于【代碼管理】Git刪除倉庫中的大文件壓縮倉庫大小的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!