最近開發(fā)項目中,使用了幾個體積超過100M的第三方框架,這樣導(dǎo)致在提交代碼入庫時,會被拒絕,以GitHub為例,單個文件超過50M入庫會警告,超過100M會不然入庫。
解決方案
1、將單個文件大于100M的文件不入庫
git rm --cached giant_file
# Stage our giant file for removal, but leave it on disk
git commit --amend -CHEAD
# Amend the previous commit with your change
# Simply making a new commit won't work, as you need
# to remove the file from the unpushed history as well
git push
# Push our rewritten, smaller commit
如果上面的沒有解決,也可以使用下面的命令
git filter-branch -f --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch FrameworkFold/XXXFramework/xxx' --tag-name-filter cat -- --all
git commit --amend -CHEAD
git push
這樣導(dǎo)致雖然可以入庫成功,但是本地已經(jīng)刪除了這個大文件,項目運行起來還需要重新將大文件加入到項目中才行。GitHub也推薦使用 BGF 。
2、突破GitHub的限制,使用 git-lfs(Git Large File Storage) 支持單個文件超過100M

LFS 并不能像"變魔術(shù)一樣"處理所有的大型數(shù)據(jù):它需要記錄并保存每一個變化。然而,這就把負擔轉(zhuǎn)移給了遠程服務(wù)器 - 允許本地倉庫保持相對的精簡。
為了實現(xiàn)這個可能,LFS 耍了一個小把戲:它在本地倉庫中并不保留所有的文件版本,而是僅根據(jù)需要提供檢出版本中必需的文件。
但這引發(fā)了一個有意思的問題:如果這些龐大的文件本身沒有出現(xiàn)在你的本地倉庫中....改用什么來代替呢? LFS 保存輕量級指針中有真實的文件數(shù)據(jù)。當你用一個這樣的指針去遷出一個修訂版時,LFS 會很輕易地找到源文件(不在他上面可能就在服務(wù)器上,特殊緩存)然后你下載就行了。
因此,你最終只會得到你真正想要的文件 - 而不是一些你可能永遠都不需要冗余數(shù)據(jù)。
# 1、安裝git-lfs
brew install git-lfs
# 2、沒有特別說明的情況下,LFS 不會處理大文件問題,因此,我們必須明確告訴 LFS 該處理哪些文件。將 FrameworkFold/XXXFramework/xxx的文件設(shè)置成大文件標示。
git lfs track "FrameworkFold/XXXFramework/xxx"
# 3、常規(guī)的push操作
git add .
git commit -m "add large file"
git push
追蹤文件路徑(標示大文件):
1、追蹤單個文件:
git lfs track "FrameworkFold/XXXFramework/xxx"
或者修改倉庫路徑下的 .gitattributes 文件:
FrameworkFold/XXXFramework/xxx filter=lfs diff=lfs merge=lfs -text
2、追蹤指定類型的文件:
git lfs track "*.exe"
3、追蹤指定目錄下的文件:
git lfs track "FrameworkFold/*"
相關(guān)知識
一些問題
1、Remote "origin" does not support the LFS locking API. Consider disabling it with
# 在最后一步push的時候
git push -u origin develop1.0
Remote "origin" does not support the LFS locking API. Consider disabling it with:
$ git config lfs.https://git.oschina.net/harrydeng/xxx.git/info/lfs.locksverify false
Git LFS: (0 of 1 files) 0 B / 207.25 MB
batch request: Access denied
exec request failed on channel 0: exit status 255
error: failed to push some refs to 'git@git.oschina.net:harrydeng/xxx.git'
git config lfs.https://git.oschina.net/harrydeng/xxx.git/info/lfs.locksverify false
2、batch request: Access denied
# 在最后一步push的時候
git push -u origin develop1.0
Remote "origin" does not support the LFS locking API. Consider disabling it with:
$ git config lfs.https://git.oschina.net/harrydeng/xxx.git/info/lfs.locksverify false
Git LFS: (0 of 1 files) 0 B / 207.25 MB
batch request: Access denied
exec request failed on channel 0: exit status 255
error: failed to push some refs to 'git@git.oschina.net:harrydeng/xxx.git'
# 刪除 .git/hooks/pre-push 文件即可
That looks like a server issue with deploy keys. For now, try removing .git/hooks/pre-push.
3、GitHub 目前 Git LFS的總存儲量為1G左右,超過需要付費。(上傳失敗時,可以開啟VPN進行上傳)
4、batch response: Repository or object not found
$ git lfs push origin master
Git LFS: (0 of 1 files) 0 B / 207.25 MB
batch response: Repository or object not found: https://gitee.com/harrydeng/LargeFileStorage.git/info/lfs/objects/batch
Check that it exists and that you have proper access to it
失敗原因:
是gitee.com這個git倉庫并不支持lfs,所以在大文件入庫的時候,提示失敗
解決方式:將單個文件大于100M的文件不入庫。
目前來說,GitHub、GitLab、Coding。gitee(也就是git.oschina.net)目前還不支持。