git文件目录

前面我们通过git init在当前文件目录下创建了一个git仓库,下面我们进入到.git文件看看。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 G:\mygitea\GitLearn\learn01   master 
$ ls -al
total 21
drwxr-xr-x 1 WhiteCookies 197121 0 5月 7 13:15 ./
drwxr-xr-x 1 WhiteCookies 197121 0 5月 5 09:10 ../
drwxr-xr-x 1 WhiteCookies 197121 0 5月 7 13:15 .git/
drwxr-xr-x 1 WhiteCookies 197121 0 5月 5 09:50 images/
-rw-r--r-- 1 WhiteCookies 197121 1470 5月 5 09:50 index.html
drwxr-xr-x 1 WhiteCookies 197121 0 5月 5 09:50 js/
-rw-r--r-- 1 WhiteCookies 197121 5662 5月 5 09:50 LICENSE
-rw-r--r-- 1 WhiteCookies 197121 29 5月 5 19:32 readme
drwxr-xr-x 1 WhiteCookies 197121 0 5月 5 09:50 styles/

G:\mygitea\GitLearn\learn01   master 
$ cd .git\

G:\mygitea\GitLearn\learn01\.git   master 
$ ls
COMMIT_EDITMSG config description FETCH_HEAD HEAD hooks/ index info/ logs/ objects/ ORIG_HEAD refs/

查看HEAD文件

这里的ref是一个引用,指向refs/heads/master,即我们当前工作所处的分支。

1
2
3
4
5
6
7
8
9
 G:\mygitea\GitLearn\learn01\.git   master 
$ cat HEAD
ref: refs/heads/master

G:\mygitea\GitLearn\learn01\.git   master 
$ git branch -av
* master 5bc7fdf [ahead 2] rename test
temp e1514cf test1
remotes/origin/master b390c28 add modified html css

下面我们切换到temp分支查看:

1
2
3
4
5
6
7
 G:\mygitea\GitLearn\learn01   master 
$ git checkout temp
Switched to branch 'temp'

G:\mygitea\GitLearn\learn01   temp 
$ cat .git\HEAD
ref: refs/heads/temp

Config

此文件存储当前git仓库下的配置信息,可以在此文件修改相应配置信息,相对应的仓库配置信息也会修改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 G:\mygitea\GitLearn\learn01   temp 
$ cat .git\config
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[user]
name = Jabari
email = innocenfox@gmail.com
[remote "origin"]
url = ssh://git@gitea.everweekup.com:222/Jabari/GitLearn.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

refs

refs文件夹存储引用信息,其中tags文件夹意味着git仓库可以有很多标签,这些标签又被称为里程碑,即项目开发到一定程度是一个关键的成果。我们可以针对某个关键性版本在commit时打上标签,表示是某一阶段的成果。

heads对应的就是分支,就是一个独立的开发空间,比如有时候你要做一个前端开发和后端开发,那么我们可以分别为这个仓库中的前端相关和后端相关代码分别建立两个分支,彼此在不同的分支工作,互不影响,当需要集成时再将两者的分支集成即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 G:\mygitea\GitLearn\learn01   temp 
$ ls .git\refs\
heads/ remotes/ tags/

G:\mygitea\GitLearn\learn01   temp 
$ tree /f .git\refs\
Folder PATH listing for volume 新加卷
Volume serial number is 406B-D0C3
G:\MYGITEA\GITLEARN\LEARN01\.GIT\REFS
├───heads
│ master
│ temp

├───remotes
│ └───origin
│ master

└───tags

分支文件

我们来看看refs里的master文件存储信息:

1
2
3
 G:\mygitea\GitLearn\learn01   temp 
$ cat .git\refs\heads\master
5bc7fdf00745054e6ad99bbc6aceb14cdaa672d4

这里的一串数值其实是一个对象。我们可以通过git cat-file -t 5bc7fdf00来查看该文件的类型:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 G:\mygitea\GitLearn\learn01   temp 
$ git cat-file -t 5bc7fdf00
commit

G:\mygitea\GitLearn\learn01   temp 
$ cat .git\refs\heads\temp
e1514cf78ae952ecbdbd5c3af3ed3bcc52afd846

G:\mygitea\GitLearn\learn01   temp 
$ git cat-file -t e1514cf7
commit

G:\mygitea\GitLearn\learn01   temp 
$ git branch -av
master 5bc7fdf [ahead 2] rename test
* temp e1514cf test1
remotes/origin/master b390c28 add modified html css

可以看到该文件是一个commit类型,即master文件存放的就是master指针指向的commit对象。

针对5bc7fdf00进行说明,git里的对象hash值是唯一的时候,可以输入commit对象编号前面一小段来确定commit对象,如果hash值不唯一,git无法识别短id,则需要输入完整的编号来确认commit对象。

tags

![[Pasted image 20220509135327.png]]

object

该文件是git仓库文件系统的核心内容。object里面存放了多个两个字符文件夹,其中pack文件夹是对这些两个字符文件夹打包存储的地方

1
2
3
4
 G:\mygitea\GitLearn\learn01   temp 
$ ls .git\objects\
01/ 12/ 1d/ 2f/ 38/ 3f/ 58/ 69/ 6b/ 74/ 87/ 96/ a1/ a5/ ae/ cd/ d4/ db/ df/ e5/ e8/ info/
10/ 15/ 2e/ 34/ 3c/ 42/ 5b/ 6a/ 6d/ 75/ 94/ 9b/ a4/ ab/ b3/ cf/ da/ dc/ e1/ e6/ ef/ pack/

进入到一个文件夹,将文件夹名和文件夹下的对象编号拼接查看类型,发现有commit对象和tree对象两种类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 G:\mygitea\GitLearn\learn01   temp 
$ cd .git\objects\01\

G:\mygitea\GitLearn\learn01\.git\objects\01   temp 
$ ls -al
total 6
drwxr-xr-x 1 WhiteCookies 197121 0 5月 5 19:31 ./
drwxr-xr-x 1 WhiteCookies 197121 0 5月 7 13:15 ../
-r--r--r-- 1 WhiteCookies 197121 151 5月 5 19:31 2ae4631218bd877eb609f99f9d78c27dc49bdb
-r--r--r-- 1 WhiteCookies 197121 85 5月 5 09:50 4d5087404210502134ae6e1109021782c83cb4

G:\mygitea\GitLearn\learn01\.git\objects\01   temp 
$ git cat-file -t 012ae4631218bd877eb609f99f9d78c27dc49bdb
commit

G:\mygitea\GitLearn\learn01\.git\objects\01   temp 
$ git cat-file -t 014d5087404210502134ae6e1109021782c83cb4
tree

查看下树和commit对象内容中的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# tree对象
G:\mygitea\GitLearn\learn01\.git\objects\01   temp 
$ git cat-file -p 014d5087404210502134ae6e1109021782c83cb4
100644 blob a58a8525f82c3e32934633d838f364039e68f44d LICENSE
100644 blob a437f1bf2beebbfae63bb067c5addc3739029b8f README.md

# commit对象
G:\mygitea\GitLearn\learn01\.git\objects\01   temp 
$ git cat-file -p 012ae4631218bd877eb609f99f9d78c27dc49bdb
tree 2f1f708f640234d11ca755e605e6144befd4a2cc
parent b390c282f3372968413b619b0747cef133fde257
author Jabari <innocenfox@gmail.com> 1651750273 +0800
committer Jabari <innocenfox@gmail.com> 1651750273 +0800

e1

可以看到树类型内容里包括了两个文件类型(blob)文件README.md和LICENSE。

我们查看文件对应编号的对象类型和内容:

1
2
3
4
# blob a58a8525f82c3e32934633d838f364039e68f44d    LICENSE
G:\mygitea\GitLearn\learn01\.git\objects\01   temp 
$ git cat-file -t a58a8525f8
blob

![[Pasted image 20220509140157.png]]

在git里,任何两个相同内容的文件都是同一个blob。

总结

.git目录下各文件的意义

文件名 意义 备注
HEAD 存储当前仓库所处分支的引用信息 对HEAD文件内容编辑与git checkout 分支命令效果一致
config 存储当前仓库的配置信息 编辑此文件信息与git config —local user.xxx “xxx”效果一致
refs 引用文件,存储分支和tags文件信息
objects git仓库核心内容存储文件

// 09|探秘.git目录
//cat命令主要用来查看文件内容,创建文件,文件合并,追加文件内容等功能。
cat HEAD 查看HEAD文件的内容
git cat-file 命令 显示版本库对象的内容、类型及大小信息。
git cat-file -t b44dd71d62a5a8ed3 显示版本库对象的类型
git cat-file -s b44dd71d62a5a8ed3 显示版本库对象的大小
git cat-file -p b44dd71d62a5a8ed3 显示版本库对象的内容

HEAD:指向当前的工作路径
config:存放本地仓库(local)相关的配置信息。
refs/heads:存放分支
refs/tags:存放tag,又叫里程牌 (当这次commit是具有里程碑意义的 比如项目1.0的时候 就可以打tag)
objects:存放所有git对象 .git/objects/ 文件夹中的子文件夹都是以哈希值的前两位字符命名 每个object由40位字符组成,前两位字符用来当文件夹,后38位做文件。