标签:
由于git在commit时保存整个文件系统,如果在文件系统中有一些大的二进制文件,比如图片,视频,那么很快你的repo就将变得很大,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
下面是在本地保存文件的使用流程和相关命令:
$ 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 $ 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
标签:
原文地址:http://www.cnblogs.com/kidsitcn/p/4637691.html