Files
GitGuide/docs/git/advanced/工作目录和裸仓库分离.md
2021-03-18 20:08:24 +08:00

32 lines
965 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 工作目录和裸仓库分离
裸仓库指`.git`文件夹,工作目录指除`.git`文件夹外的其他文件
使用场景之一如下:
1. 本地将修改好的代码上传到远程裸仓库
2. 裸仓库获取到代码后,通过钩子生成工作目录,放置在其他路径下
3. 服务器再对工作目录进行操作,比如`nginx`对静态文件进行托管
`.git/hooks`文件夹下新建`post-receive`文件
$ vim post-receive
添加如下内容
#!/bin/sh
git --work-tree=工作目录 --git-dir=裸仓库 checkout -f
授予执行权限
$ sudo chmod +x post-receive
## 相关阅读
* [GIT_DIR和GIT_WORK_TREE的妙用工作区和仓储可隔离](http://www.yinqisen.cn/blog-718.html)
* [通过git自动部署WEB服务上的PHP代码提交即生效](http://www.yinqisen.cn/blog-675.html)
* [8.3 自定义 Git - Git 钩子](https://git-scm.com/book/zh/v2/%E8%87%AA%E5%AE%9A%E4%B9%89-Git-Git-%E9%92%A9%E5%AD%90)