[TOC] #### .git 目录内容 --- 本文记录平时开发中遇到的 .git 目录下的内容及其作用,持续更新 ! ``` .git ├── COMMIT_EDITMSG ├── FETCH_HEAD ├── HEAD ├── ORIG_HEAD ├── TAG_EDITMSG ├── config ├── description ├── hooks │ ├── applypatch-msg.sample │ ├── ... ├── info │ └── exclude ├── objects │ ├── info │ └── pack └── refs ├── heads └── tags ``` #### .git/COMMIT_EDITMSG --- 保存着最近一次的提交信息,git 不会用到这个文件,只是给用户一个参考  #### .git/FETCH_HEAD --- 当我们执行 `git fetch` 时会自动创建这个文件 执行 `git pull` 也会创建这个文件,因为 `git pull` 相当于 `git fetch && git merge` FETCH_HEAD 是一个短暂的 ref,用于记录从远程库拉取下来的内容。 git pull 首先调用 git fetch 从远程库获取分支, FETCH_HEAD 指向分支的尖端(也就是该文本内容的第一行是当前分支),然后调用 git merge 合并 FETCH_HEAD 到当前分支 #### .git/HEAD --- `.git/HEAD` 该文件中记录了当前指针指向的是哪个分支 ``` # 当前在 master 分支 ref: refs/heads/master # 当前在 liang 分支 ref: refs/heads/liang ``` #### .git/ORIG_HEAD --- 使用 `git merge <branch>` 合并分支,会产生这个文件 因为合并分支是个比较危险的操作,所以 git 提供了 ORIG_HEAD 这个文件,让其进行版本回滚 ``` git reset --hard ORIG_HEAD ``` #### .git/TAG_EDITMSG --- 执行下面命令会进入 vi 编辑模式,让输入标签描述,如果不填写内容,使用 `:wq` 推出会生成该文件 ``` git tag -a v1.0.0 ``` 查看文件内容 ``` $ cat .git/TAG_EDITMSG # # Write a message for tag: # v1.0.0 # Lines starting with '#' will be ignored. ``` #### .git/config ---  #### .git/description ---- 用于在 GitWeb 中展示项目的描述信息,GitWeb 是 git 版本库的图形化 web 浏览功能 在 [git 官方文档](https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain?spm=a2c6h.12873639.article-detail.8.59246941pSdNro "abcde")中有以下描述: ``` # description 仅由 GitWeb 程序使用,所以不用担心 The description file is used only by the GitWeb program, so don’t worry about it. ``` 安装 lighttpd ``` brew install lighttpd ``` 启动 lighttpd 服务 ``` git instaweb --start ``` 打开浏览器访问 `http://127.0.0.1:1234`  这段话就是 `.git/description` 文件的内容,可以编辑这个文件内容来让 GitWeb 描述更友好。除此之外没发现其它用处。 ``` Unnamed repository; edit this file 'description' to name the repository. ``` #### .git/refs/* ---- `git/refs/heads/*` 记录本地分支的指针指向的是哪一个 `commit id`  `git/refs/remotes/*` 记录远程分支的指针指向的是哪一个 `commit id`  `git/refs/tag/*` 记录本地标签对应的哪一个 `commit id`  #### 相关文章推荐 --- [.git文件夹探秘,理解git运作机制【阿里云开发者社区】](https://developer.aliyun.com/article/716483#slide-7 ".git文件夹探秘,理解git运作机制【阿里云开发者社区】")