git大文件管理


  由於git在每一個commit時都會變動過的文件全部保存(不像其他的系統,只做文件增量存儲),外加未變動文件的引用,這樣如果在文件系統中有一些大的二進制文件,比如圖片,視頻,那么很快你的repo就將變得很大(特別是binary文件又會有高頻小部分變化commit的話),clone這個repo時也會耗用越來越多的時間。有沒有什么辦法來優化這個問題呢?

  一個可行的方法是使用git-fat : https://github.com/jedbrown/git-fat

其原理是:將二進制文件本身存放於共享文件系統中,保存在git repo中的信息僅僅是一些meta數據。

1.安裝: git-fat是一個shell腳本,只要下載該腳本,放到你的path變量中就安裝好了

2.使用:創建一個.gitattributes文件,來描述哪些文件是一個二進制文件:

$ cd path-to-your-repository
$ cat >> .gitattributes
*.png filter=fat -crlf
*.jpg filter=fat -crlf
*.gz  filter=fat -crlf
^D

運行git fat init 激活上面的文件后綴,從此你可以像一般文件一樣來git add, git commit那些.png,.gz,.jpg文件,而文件本身卻保存於repo之外的地方;

如果你的文件本身保存於一個共享服務器上,你可以創建一個.gitfat文件,該文件中寫入以下內容

[rsync]
remote = your.remote-host.org:/share/fat-store
sshuser = yourusername
options = -avzW

下面是在本地保存文件的使用流程和相關命令:

$ git init repo
Initialized empty Git repository in /tmp/repo/.git/
$ cd repo
$ git fat init
$ cat > .gitfat
[rsync]
remote = localhost:/tmp/fat-store
$ mkdir -p /tmp/fat-store               # make sure the remote directory exists
$ echo '*.gz filter=fat -crlf' > .gitattributes
$ git add .gitfat .gitattributes
$ git commit -m'Initial repository'
[master (root-commit) eb7facb] Initial repository
 2 files changed, 3 insertions(+)
 create mode 100644 .gitattributes
 create mode 100644 .gitfat
$ curl https://nodeload.github.com/jedbrown/git-fat/tar.gz/master -o master.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  6449  100  6449    0     0   7741      0 --:--:-- --:--:-- --:--:--  9786
$ git add master.tar.gz
git-fat filter-clean: caching to /tmp/repo/.git/fat/objects/b3489819f81603b4c04e8ed134b80bace0810324
$ git commit -m'Added master.tar.gz'
[master b85a96f] Added master.tar.gz
git-fat filter-clean: caching to /tmp/repo/.git/fat/objects/b3489819f81603b4c04e8ed134b80bace0810324
 1 file changed, 1 insertion(+)
 create mode 100644 master.tar.gz
$ git show --pretty=oneline HEAD
918063043a6156172c2ad66478c6edd5c7df0217 Add master.tar.gz
diff --git a/master.tar.gz b/master.tar.gz
new file mode 100644
index 0000000..12f7d52
--- /dev/null
+++ b/master.tar.gz
@@ -0,0 +1 @@
+#$# git-fat 1f218834a137f7b185b498924e7a030008aee2ae
$ git fat push
Pushing to localhost:/tmp/fat-store
building file list ...
1 file to consider

sent 61 bytes  received 12 bytes  48.67 bytes/sec
total size is 6449  speedup is 88.34

上述過程完畢后,對應的二進制文件就已經保存好了,那么后面如何使用呢?

$ cd ..
$ git clone repo repo2
Cloning into 'repo2'...
done.
$ cd repo2
$ git fat init                          # don't forget:注意一旦clone了git repo后就要做這個動作,否則你修改了你的image文件后,git fat push時並不會主動將更行后的問題件上傳到文件服務器上
$ ls -l                                 # file is just a placeholder
total 4
-rw-r--r--  1 jed  users  53 Nov 25 22:42 master.tar.gz
$ cat master.tar.gz                     # holds the SHA1 of the file
#$# git-fat 1f218834a137f7b185b498924e7a030008aee2ae
$ git fat pull
receiving file list ...
1 file to consider
1f218834a137f7b185b498924e7a030008aee2ae
        6449 100%    6.15MB/s    0:00:00 (xfer#1, to-check=0/1)

sent 30 bytes  received 6558 bytes  4392.00 bytes/sec
total size is 6449  speedup is 0.98
Restoring 1f218834a137f7b185b498924e7a030008aee2ae -> master.tar.gz
git-fat filter-smudge: restoring from /tmp/repo2/.git/fat/objects/1f218834a137f7b185b498924e7a030008aee2ae
$ git status
git-fat filter-clean: caching to /tmp/repo2/.git/fat/objects/1f218834a137f7b185b498924e7a030008aee2ae
# On branch master
nothing to commit, working directory clean
$ ls -l                                 # recovered the full file
total 8
-rw-r--r-- 1 jed users 6449 Nov 25 17:10 master.tar.gz

 如果出現了以下錯誤,可能是和文件權限為600有關,可以考慮使用sudo rsync xxx來執行,或者有可能部分文件不存在??

rsync -zr userA@remoteServer:/var/www/website/ /home/user/Documents/webSiteBackup/website/www/
rsync: send_files failed to open "/var/www/website/wp-config.php": Permission denied (13)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1655) [generator=3.1.0]
rsync: link_stat "/home/gitfat/gitfatlibs/da39a3ee5e6b4b0d3255bfef95601890afd80709" failed: No such file or directory (2)                                                  
0 files to consider                                                                                                                                                        
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1655) [Receiver=3.1.0]   

 如何在執行rsync時保存相應log?

rsync -avz --log-file=$HOME/.rsyncd.log' -e ssh /home/adm/ adm@plog01:/home/adm

如果出現以下錯誤,則可以考慮在official git fat 網站上raw copy重新生成git-fat,設置777權限

[cabox@box-codeanywhere gitfattest]$ git fat init                                                                                                                          
: No such file or directory                                                                                                                                                
fatal: 'fat' appears to be a git command, but we were not                                                                                                                  
able to execute it. Maybe git-fat is broken?  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM