Preface: When submitting and pushing local changes to the warehouse, a large file was submitted by mistake, resulting in an error report that the push file is too large, so it is necessary to remove the large file that has been committed before pushing
If you know the file or folder path to delete, you can start from step 4
1. Perform gc operation on the warehouse
$ git gc
2. Query large files
git verify-pack -v .git/objects/pack/pack-*.idx | sort -k 3 -g | tail -5
Note that this is queried from the .git folder, so it needs to be executed in the root directory of the warehouse
The displayed result is a large file, and the first 6 ids are the file submission id
3. Get the file name corresponding to the id
git rev-list --objects --all | grep id名称
如:git rev-list --objects --all | grep 78b35d
After the id is the full path name of the file
4. Delete the file (if you know the file path, you can directly execute the following command)
git filter-branch --force --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch 文件名' --tag-name-filter cat -- --all
like:
git filter-branch --force --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch CustomPhysics2DIntProject2022.3/Build/01 - 副本/UnityPlayer.dll' --tag-name-filter cat -- --all
If you want to delete a directory, you can directly fill in the directory, such as:
git filter-branch --force --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch CustomPhysics2DIntProject2022.3/Build' --tag-name-filter cat -- --all
If an exception occurs: Cannot rewrite branches: You have unstaged changes.
Excuting an order:
git stash
re-delete after
5. Delete the pointer to the old commit
rm -rf .git/refs/original/
6. Delete log
rm -rf .git/logs/
7. Give up the possibility of all unassociated object recovery
git reflog expire --expire=now --all
8. Clean up the trash and repackage
git repack -A -d //重新打包
git gc --aggressive --prune=now //清理垃圾,重新打包
9. Forcibly push the remote
git push --force
git push --force --all
10. Multiple branch cleanup
git checkout branchName //切换分支,多个分支需要分别清理