# Outline
一、前言
二、實驗
A、待敘項目
# TL;DR
透過觀察常用的 `git add`、`git commit` 流程觀察 `.git` 內會發生什麼事。
# Updated
2019-10-06: 更新標題與文章結構
在雙十連假前,此系列文每天的發文時都會以最簡陳述為主,以求在繁忙的日常中,至少能先維持挑戰鐵人賽的進度,並且逐漸拓展思路與系列結構。預期會在國慶連假將本篇文章論述完整。
從昨天的實驗中,我們得知最基礎的 .git
目錄結構如下。
# [Repo] 61d0acc @ dot-git
# Location: ~/how-git-works-lab
$ tree .git
.git/
├── HEAD
├── objects/
└── refs/
我們今天就嘗試使用 git add
、git commit
增加一個檔案進去,看會發生什麼事。
如果只是在專案目錄下作變動,儘管 git status
有偵測到有多一個新的檔案,但是 .git
目錄是沒有任何變動的。
# Loacation: ~/how-git-works-lab
$ echo '# How Git Works Lab' > README.md
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
$ tree .git/
.git/
├── HEAD
├── objects/
└── refs/
但是當我們透過 git add
把 README.md
加進來後,就會發現 .git/objects
目錄下多了一個子目錄和一個檔案。
# Loacation: ~/how-git-works-lab
$ git add README.md
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
$ tree .git/
.git/
├── HEAD
├── index
├── objects
│ └── 8b
│ └── 0aa0b5577953e2a6b9a6a138008229e638f56c
└── refs
若是直接透過 cat
指令想顯示檔案內容,只會顯示亂碼。唯有透過 git cat-file
指令才有辦法顯示實際的檔案內容,我們發現和 README.md
的內文是一樣的。
$ cat .git/objects/8b/0aa0b5577953e2a6b9a6a138008229e638f56c
xK��OR02`PV��/Wp�,�/�.V�IL�uF⏎
$ git cat-file -p 8b0aa0b5577953e2a6b9a6a138008229e638f56c
# How Git Works Lab
之後,我們再透過 git commit
進行這個專案的第一次提交。會發現多了不少子目錄和檔案。
# Loacation: ~/how-git-works-lab
$ git commit -m "add README"
[master (root-commit) 5626a0e] add README
1 file changed, 1 insertion(+)
create mode 100644 README.md
# [Repo] f47eca7 @ dot-git
# [Repo] 5626a0e @ lab
# Location: ~/how-git-works-lab
$ git log --pretty=format:"%h %s %d" --graph
* 5626a0e [Day04] add README (HEAD -> master)
$ tree .git/
.git/
├── COMMIT_EDITMSG
├── HEAD
├── index
├── logs
│ ├── HEAD
│ └── refs
│ └── heads
│ └── master
├── objects
│ ├── a2
│ │ └── beefd59223ea16000788d77e62f96bdaf23c7c
│ ├── f4
│ │ └── 7eca7b5d0a157583076c66d470b1eb032822ac
│ └── fa
│ └── 5e6af79e30fc26ab4acbd96388fde22b4c2f36
└── refs
└── heads
└── master
.git/objects
目錄下多出來的檔案內容如下:
# [Repo] f47eca7 @ dot-git
# [Repo] 5626a0e @ lab
# Location: ~/how-git-works-lab
$ git cat-file -p f47eca7b5d0a157583076c66d470b1eb032822ac
tree fa5e6af79e30fc26ab4acbd96388fde22b4c2f36
author Ruoshi Lin <me@fntsr.tw> 1568993969 +0800
committer Ruoshi Lin <me@fntsr.tw> 1568993969 +0800
$ git cat-file -p fa5e6af79e30fc26ab4acbd96388fde22b4c2f36
100644 blob a2beefd59223ea16000788d77e62f96bdaf23c7c README.md
.git/refs
目錄下多出來的檔案內容如下:
# [Repo] f47eca7 @ dot-git
# [Repo] 5626a0e @ lab
# Location: ~/how-git-works-lab
$ cat .git/refs/heads/master
f47eca7b5d0a157583076c66d470b1eb032822ac
.git/logs
目錄下中的檔案內容如下:
# [Repo] f47eca7 @ dot-git
# [Repo] 5626a0e @ lab
# Location: ~/how-git-works-lab
cat .git/logs/HEAD
0000000000000000000000000000000000000000 f47eca7b5d0a157583076c66d470b1eb032822ac Ruoshi Lin <me@fntsr.tw> 1568993969 +0800 commit (initial): add README
cat .git/logs/refs/heads/master
0000000000000000000000000000000000000000 f47eca7b5d0a157583076c66d470b1eb032822ac Ruoshi Lin <me@fntsr.tw> 1568993969 +0800 commit (initial): add README
git reflog
f47eca7 (HEAD -> master) HEAD@{0}: commit (initial): add README