git分析
首先在创建一个目录,进入目录后使用git init命令,查看.git目录:
r1chie@DESKTOP-QFN5N0R:~/git/store$ tree .git/
.git/
├── branches
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
刚开始,objects目录下是没有任何对象的哈希的,当我们进行一次正常的commit:
touch 1.txt
git add.
git commit -m "add 1.txt"
查看log:
commit 2b4381939b2339267c1fcbef1966ce9e9a5b4bbe
Author: r1chie <r1chie@ramboai.com>
Date: Fri Mar 19 10:58:33 2021 +0800
add 1.txt
说明commit是成功的,我们再来查看一下.git目录
.git/
├── branches
├── COMMIT_EDITMSG
├── config
├── description
├── HEAD
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── prepare-commit-msg.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ └── update.sample
├── index
├── info
│ └── exclude
├── logs
│ ├── HEAD
│ └── refs
│ └── heads
│ └── master
├── objects
│ ├── 2b
│ │ └── 4381939b2339267c1fcbef1966ce9e9a5b4bbe
│ ├── 34
│ │ └── 1e54913a3a43069f2927cc0f703e5a9f730df1
│ ├── e6
│ │ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│ ├── info
│ └── pack
└── refs
├── heads
│ └── master
└── tags
发现objects目录下面多了三个目录,发现2b目录和log信息的哈希是一致的,可以推出这个哈希是一个提交对象(包含着指向前述树对象的指针和所有提交信息),事实上这三个哈希对应着commit、tree、blog,如下图
下面进行第二次commit:
touch 2.txt
git add .
git commit -m "add 2.txt"
查看log:
commit 45cd3055058fca923b2f93015822e4e6fbeeb5f1
Author: r1chie <r1chie@ramboai.com>
Date: Fri Mar 19 11:01:00 2021 +0800
add 2.txt
commit 2b4381939b2339267c1fcbef1966ce9e9a5b4bbe
Author: r1chie <r1chie@ramboai.com>
Date: Fri Mar 19 10:58:33 2021 +0800
add 1.txt
查看git目录:
├── objects
│ ├── 2b
│ │ └── 4381939b2339267c1fcbef1966ce9e9a5b4bbe
│ ├── 34
│ │ └── 1e54913a3a43069f2927cc0f703e5a9f730df1
│ ├── 37
│ │ └── e0c822b2ed7db5f1c422b9258808e4fb64aaf3
│ ├── 45
│ │ └── cd3055058fca923b2f93015822e4e6fbeeb5f1
│ ├── e6
│ │ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
│ ├── info
│ └── pack
└── refs
├── heads
│ └── master
└── tags
不难推断,新增了三个哈希,一个是commit的,一个是tree的,一个是文件快照,
首次commit的对象是没有父节点的,第二次commit才拥有父节点的,如下图:
这时,我们添加一个分支:
git branch testing
就会发现.git/refs/heads目录下面多了一个testing,但目前只是创建了一个testing分支,还并未切换到testing分支,需要使用命令切换分支:
git checkout testing
接着在该分支下面,创建一个新文件,并且commit,打印log:
git log --decorate
就会发现,最新的commit后面是testing,第二个commit是master,如果此时切换回master分支,那么工作区中就不会存在testing的文件。
接下来切换到master,然后添加一个新文件,再commit,打印log:
git log --oneline --decorate --graph --all
* commit f892240f2b539a810871ee25a846e4b6d8558bd9 (HEAD -> master)
| Author: r1chie <r1chie@ramboai.com>
| Date: Fri Mar 19 11:43:42 2021 +0800
|
| made other changes
|
| * commit 6d088f734856a3ae23005b11239eacd9fb5ce08f (testing)
|/ Author: r1chie <r1chie@ramboai.com>
| Date: Fri Mar 19 11:39:43 2021 +0800
|
| add 3.txt
|
* commit 45cd3055058fca923b2f93015822e4e6fbeeb5f1
| Author: r1chie <r1chie@ramboai.com>
| Date: Fri Mar 19 11:01:00 2021 +0800
|
| add 2.txt
|
* commit 2b4381939b2339267c1fcbef1966ce9e9a5b4bbe
Author: r1chie <r1chie@ramboai.com>
Date: Fri Mar 19 10:58:33 2021 +0800
add 1.txt