GitHub上傳超過100M的單個文件(包括處理和解決)

版權聲明:本文爲博主原創文章,版權均歸本人所有,任何人或團體、機構全部轉載或者部分轉載、摘錄,請保留本博客鏈接或標註來源。博客地址:https://blog.csdn.net/qq_31285709/article/details/105026835

一、背景

在完善畢設的過程中,有使用STK生成的衛星軌道數據,1000多顆衛星兩個小時(7200秒)的數據保存在csv文件中。這一個文件包含7203×1000行數據,一共315.7M。畢設的系統實時更新上GitHub,但是在同步這個文件時遇到了問題。

二、問題詳情

在某一次系統代碼和文件修改中,我修改了一個常規文件,然後加入了這個315M的csv文件,於是想把這兩個改動同步到GitHub上。
按照正常流程進行git add、git commit、git push這兩個改動的地方。
在git push運行輸入賬號密碼之後顯示:

root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git push https://github.com/我的賬號/我的項目.git
Username for 'https://github.com': 我的賬號
Password for 'https://我的賬號@github.com':
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (8/8), 141.21 MiB | 5.41 MiB/s, done.
Total 8 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
remote: error: Trace: a54c2204be132cb46c9fb4fe3817a14f
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File 我的的項目/settings/locationflie/edge_devices/mist Fixed Position.csv is 315.70 MB; this exceeds GitHub's file size limit of 100.00 MB
To https://github.com/我的賬號/我的項目.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://github.com/我的賬號/我的項目.git'

顯示主要的問題是我的文件太大了,GitHub檢測到我push的commit裏面包含一個大於100M的文件。
remote: error: GH001: Large files detected.
remote: error: File mist Fixed Position.csv is 315.70 MB; this exceeds GitHub’s file size limit of 100.00 MB

三、問題處理

此時是在push上去的過程中出現的問題。如果這是最近一次commit,並且還沒有push到github上,爲了保證在這次commit沒有問題的文件還能夠順利的push上去,需要把在這次push的過程中出現問題的文件移除本次commit。
在這裏我需要把mist Fixed Position.csv這個文件移除這次的commit中,然後把沒有問題的文件push上去。

1.首先是移除的操作

在你之前git add的路徑下(需要移除的文件的路徑下)運行:

git rm --cached "需要移除的文件名.後綴"

移除成功會有顯示,說明文件已經移除當前commit,如下:

rm '我的項目/settings/locationflie/edge_devices/mist Fixed Position.csv'

如果移除不成功可能會顯示沒有這個文件,可能是你當前的文件路徑不對。一定要到要移除的文件的本地路徑下運行git rm命令。
提示:該命令不會把本地文件刪除。

2.重新commit

把沒有問題的文件按照之前的commit再重新commit。

git commit --amend -CHEAD

如果成功會有一個commit編號,顯示成功添加commit,如下:

[master b54c3e6] update 2020.3.22 1.40
 Date: Sun Mar 22 01:40:32 2020 +0800
 1 file changed, 1 insertion(+)
 create mode 100644 我的項目/settings/locationflie/edge_devices/改動的文件

3.把重新commit好的文件push上去

git push https://github.com/我的賬號/我的項目.git

輸入賬戶名和密碼之後就可以成功把沒有問題的文件按照之前的commit給push上去了,最後會顯示添加完成。

4.問題處理全部過程

下面給出問題處理的全部過程,基本上除了一開始git rm那裏可能會遇到問題其他地方還沒有問題。

root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git rm --cached "mist Fixed Position.csv"
rm '我的項目/settings/locationflie/edge_devices/mist Fixed Position.csv'
root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git commit --amend -CHEAD
[master b54c3e6] update 2020.3.22 1.40
 Date: Sun Mar 22 01:40:32 2020 +0800
 1 file changed, 1 insertion(+)
 create mode 100644 我的項目/settings/locationflie/edge_devices/改動的文件
root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git push https://github.com/我的賬號/我的項目.git
Username for 'https://github.com': 我的賬號
Password for 'https://我的賬號@github.com':
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 576 bytes | 115.00 KiB/s, done.
Total 7 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/我的賬號/我的項目.git
   c162e69..b54c3e6  master -> master

四、問題解決

1.解決思路

在處理完git push遇到的問題之後,現在需要考慮如何把mist Fixed Position.csv(315.7M)這個超過100M的文件push到GitHub上。
在網上查了一下解決方法,大概有兩種。

a. 第一種是git config修改(有待考究)
在終端運行:git config http.postBuffer 524288000
然後通過git config -l 可以查看配置的內容。
配置內容會多出一條:http.postbuffer=524288000
這說明git設置 http.post緩存設置爲500M,可以理解爲push的全部文件大小加起來可以有500M。(對於http.postBuffer這個配置我沒有證實過實現的效果,我也不知道理解是否正常,有待了解
但是之後我把mist Fixed Position.csv通過git add、git commit、git push之後,還是會顯示文件太大,不能超過100M的提示。
這個方法似乎沒有達到想要的效果,因爲單文件大於100M的問題沒有解決。

b.第二種是git lfs的方法(推薦使用)
很多博主都有提到這個方法,而且有效。但是大部分都會提到去官網下載一個 Git LFS 客戶端,要不就是安裝Homebrew。在這裏我只想用Linux命令行安裝,不想去裝其他的。
於是接下來的解決流程我是用win10的Linux命令行進行操作。

2. 解決流程

解決流程基於git lfs的方法,環境是win10的Linux系統環境。
a.安裝git lfs
sudo apt-get install git-lfs
git lfs install

下面是結果顯示。

root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# sudo apt-get install git-lfs
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  git-lfs
0 upgraded, 1 newly installed, 0 to remove and 294 not upgraded.
Need to get 2129 kB of archives.
After this operation, 7662 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/universe amd64 git-lfs amd64 2.3.4-1 [2129 kB]
Fetched 1092 kB in 14s (80.8 kB/s)
Selecting previously unselected package git-lfs.
(Reading database ... 52251 files and directories currently installed.)
Preparing to unpack .../git-lfs_2.3.4-1_amd64.deb ...
Unpacking git-lfs (2.3.4-1) ...
Setting up git-lfs (2.3.4-1) ...
Processing triggers for man-db (2.8.3-2) ...
root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git lfs install
Updated git hooks.
Git LFS initialized.

b.把大文件添加到 lfs
首先,保證運行命令的文件路徑爲你想要傳的大文件路徑下。
git lfs track "需要上傳的文件名.後綴"

下面是結果顯示。

root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git lfs track "mist Fixed Position.csv"
Tracking "mist Fixed Position.csv"

此時本地文件路徑下會生成一個.gitattributes文件。

c. 將.gitattributes文件push
很多博客也提到要先把.gitattributes文件push了之後,再去push超過100Mdde 大文件。
git add .gitattributes
可以通過git status查看git的狀態。
git commit -m "此處可編輯說明信息"
git push https://github.com/你的賬號/你的項目.git
此時完成了對.gitattributes文件的push。

下面是結果顯示。

root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git add .gitattributes
root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   .gitattributes

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ../../../../.classpath
        ../../../../.project
        ../../../../.settings/
        
root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git commit -m "update 2020.3.22 2.23"
[master ee71609] update 2020.3.22 2.23
 1 file changed, 1 insertion(+)
 create mode 100644 我的項目/settings/locationflie/edge_devices/.gitattributes
root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git push https://github.com/我的賬號/我的項目.git
Username for 'https://github.com': 我的賬號
Password for 'https://我的賬號@github.com':
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 590 bytes | 147.00 KiB/s, done.
Total 7 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 4 local objects.
To https://github.com/我的賬號/我的項目.git
   b54c3e6..ee71609  master -> master

d.將100M以上大文件push
接下來大於100M的文件就可以正常的進行上傳操作了。
git add "需要上傳的文件名.後綴"
如果之前忽略不把它包含在git版本控制對象裏,會有提示是否添加進去。因爲之前在git status的時候發現需要上傳的文件沒有包含在git版本控制對象裏。此時需要加上-f操作。
git add "需要上傳的文件名.後綴 -f"
git commit -m "此處可編輯說明信息"
git push https://github.com/你的賬號/你的項目.git
然後就可以等待你的大文件上傳上去了。上傳完畢之後就完成把超過100M的大文件push到GitHub上的操作。

下面是結果顯示。

root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git add "mist Fixed Position.csv"
The following paths are ignored by one of your .gitignore files:
我的項目/settings/locationflie/edge_devices/mist Fixed Position.csv
Use -f if you really want to add them.
root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git add "mist Fixed Position.csv" -f
root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git commit -m "update 2020.3.22 2.25"
[master fc876d9] update 2020.3.22 2.25
 1 file changed, 3 insertions(+)
 create mode 100644 我的項目/settings/locationflie/edge_devices/mist Fixed Position.csv
root@V:/mnt/d/Others/eclipse/Workspace/我的本地項目路徑/settings/locationflie/edge_devices# git push https://github.com/我的賬號/我的項目.git
Username for 'https://github.com': 我的賬號
Password for 'https://我的賬號@github.com':
Git LFS: (1 of 1 files) 315.70 MB / 315.70 MB
Counting objects: 7, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 682 bytes | 227.00 KiB/s, done.
Total 7 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 4 local objects.
To https://github.com/我的賬號/我的項目.git
   ee71609..fc876d9  master -> master

五、參考鏈接

https://www.cnblogs.com/qmmq/p/4604862.html
https://www.jianshu.com/p/e48d53571bf0
https://blog.csdn.net/wifi74262580/article/details/85060187
https://blog.csdn.net/weixin_39278265/article/details/100541749
https://blog.csdn.net/jjjjjj123321/article/details/84890893

git config http.postBuffer 524288000:https://blog.csdn.net/u010545480/article/details/52995794

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章