由於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時儲存相應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?