一、简介
从 1.7.0 开始,Git 引入 sparse checkout(稀疏检出) 机制,稀疏检出机制允许只检出指定目录或者文件,这在大型 Git 仓库中,将大幅度缩短 Git 执行命令的时间。
二、远程仓库
以下是我的远程仓库目录:
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git (master)
$ ls -R
.:
index.html static/ admin/
./static:
test1.txt test2.txt test3.txt
.admin:
test4.txt test5.txt
需求:现在我需要检出的是目录static以及文件index.html。
三、配置稀疏检出
要完成上面的需求,则需要通过以下配置来实现:
1、 首先新建一个目录,再初始化一个 Git 仓库,以便用稀疏检出。
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee
mkdir git_web
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/
cd git_web
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web
$ git init
Initialized empty Git repository in G:/NoSee/git_web/.git/
2、指定检出目录或文件
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ git config core.sparseCheckout true
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ echo /static/* >> .git/info/sparse-checkout
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ echo /index.html >> .git/info/sparse-checkout
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ cat .git/info/sparse-checkout
/static/*
/index.html
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ git config --list
core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
rebase.autosquash=true
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
credential.helper=manager
user.email=123nosee@gmail.com
user.name=Chan
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.sparsecheckout=true # 看到这一项说明稀疏检出配置成功
四、检出
从远程仓库地址检出指定目录和文件:
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ git remote -v
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ git remote add origin git@*.*.*.*:/home/git.git #这里是实际的远程仓库地址
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ git pull origin master
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 7(delta 1), reused 5(delta 0)
Receiving objects: 100% (7/7), 1.00 MiB | 38.00 KiB/s, done.
Resolving deltas: 100% (7/7), done.
From *.*.*.*:/home/git
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
Checking out files: 100% (7/7), done.
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ ls -R
.:
index.html static/
./static:
test1.txt test2.txt test3.txt
如果还需要再检出其它的目录或文件,则需要将其加入到 .git/info/sparse-checkout 文件中。具体步骤如下:
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ echo /admin/* >> .git/info/sparse-checkout
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ cat .git/info/sparse-checkout
/static/*
/admin/*
/index.html
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ git read-tree -mu HEAD
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ ls -R
.:
index.html static/ admin/
./static:
test1.txt test2.txt test3.txt
.admin:
test4.txt test5.txt
五、更新
更新到远程仓库:
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ touch static/test6.txt
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ git add static/test6.txt
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: static/test6.txt
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ git commit -m '配置稀疏检出'
[master 0f6ba72] 配置稀疏检出
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 static/test6.txt
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ git push
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
# 注意,第一次提交到远程仓库的时候需要关联远程分支,不能直接git push
# 使用git push --set-upstream origin master 关联远程分支
nosee@DESKTOP-FNAF3T1 MINGW64 /g/NoSee/git_web (master)
$ git push --set-upstream origin master
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 410 bytes | 410.00 KiB/s, done.
Total 5 (delta 3), reused 1 (delta 0)
To *.*.*.*:/home/git.git
22c0188..0f6ba72 master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.
# 下次更新仓库就可以直接git push了
《完》
网友评论