2019年10月1日 星期二

[ Git 常見問題 ] 使用 LFS 解決GitHub上無法上傳大文件問題

Source From Here
Question
今天使用的 GitHub上傳幾個比較大的 檔案,有的大小超過 100MB了,結果 GitHub 的報錯提示無法上傳大於 100MB 的文件:
# fallocate -l 55M 55M.img // Create large file
# ls -hl
total 56M
-rw-r--r--. 1 root root 55M Oct 1 21:40 55M.img
...

# git add 55M.img
# git commit -m 'Upload large file >50M'
[master faa8827] Upload large file >50M
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 55M.img


# git push
...
remote: warning: File 55M.img is 55.00 MB; this is larger than GitHub's recommended maximum file size of 50.00 MB
To https://github.com/johnklee/GitPro
b4d43a3..faa8827 master -> master


# fallocate -l 105M 105M.img // Create large file > 100M
# ls -hl 105M.img
-rw-r--r--. 1 root root 105M Oct 1 21:47 105M.img

# git add 105M.img
# git commit -m 'Add large file > 100M'
[master ce928ef] Add large file > 100M
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 105M.img


# git push
...
remote: error: File 105M.img is 105.00 MB; this exceeds GitHub's file size limit of 100.00 MB
To https://github.com/johnklee/GitPro
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://github.com/johnklee/GitPro'

仔細看了下報錯信息,發現可以使用 GitHub 的 LFS大文件存儲)服務來實現上傳大文件。

GitHub LFS 簡介
GitHub LFS 是一個開源的 Git 擴展,可以讓 Git 追踪大文件的版本信息。


如何使用 GitHub LFS 讓 Git 處理大文件
請參考下面步驟:

安裝
請參考 這裡 選擇屬於自己 OS 的安裝方式 (這邊使用 CentOS):
# sudo yum install epel-release
# curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.rpm.sh | sudo bash
...
Generating yum cache for github_git-lfs...
Importing GPG key 0xDC282033:
Userid : "https://packagecloud.io/github/git-lfs (https://packagecloud.io/docs#gpg_signing) "
Fingerprint: 6d39 8dbd 30dd 7894 1e2c 4797 fe2a 5f8b dc28 2033
From : https://packagecloud.io/github/git-lfs/gpgkey
Generating yum cache for github_git-lfs-source...

The repository is setup! You can now install packages.


# sudo yum install -y git-lfs
# git lfs install
Updated git hooks.
Git LFS initialized.

用 git lfs 管理大文件
用 git lfs track 命令跟踪特定后缀的大文件,或者也可以直接编辑 .gitattributes,类似与.gitignore文件的编写:
# git lfs track "*.img"
"*.img" already supported

# cat .gitattributes
*.bin filter=lfs diff=lfs merge=lfs -text
*.img filter=lfs diff=lfs merge=lfs -text

接下来就可以像平时使用 git 那样正常使用了,可以将大文件提交到 GitHub 了
# echo 'test' >> 105M.img

# gis
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
# (use "git push" to publish your local commits)
#
# Changes not staged for commit:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
# modified: 105M.img
#
no changes added to commit (use "git add" and/or "git commit -a")


# git add -u
# git commit -m 'Update 105M.img'
# git push // This time, you can see message indicating the usage of LFS
Uploading LFS objects: 100% (1/1), 110 MB | 9.7 MB/s, done
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 388 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/johnklee/GitPro
faa8827..bb134c5 master -> master

Supplement
FAQ - Linux / UNIX: Create Large 1GB Binary Image File With dd Command

沒有留言:

張貼留言

[Git 文章收集] Differences between git merge and git rebase

Source From  Here Preface Merging and rebasing are the two most popular way to applying changes from one branch into another one. They bot...