Skip to content

Commit

Permalink
fixes #65
Browse files Browse the repository at this point in the history
  • Loading branch information
doggy8088 committed Nov 18, 2023
1 parent 1293ff1 commit 4d9d2eb
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 108 deletions.
102 changes: 48 additions & 54 deletions zh-cn/07.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
第 07 天:解析 Git 资料结构 - 索引结构
=====================================================
# 第 07 天:解析 Git 资料结构 - 索引结构

我们知道在 Git 里两个重要的资料结构,分別是「物件」与「索引」,这篇文章主要用来解说「索引」的细节。使用 Git 版本控制的过程中,或许你可以很轻易的了解 git 指令的用法,不过那很容易流于死记,无法灵活运用,连 Linus Torvalds 都在邮件清单(Mailing List)中提到:「在使用者了解索引的意义之前,是无法完整了解 Git 的能力的」,因此,了解「索引」的用途十分重要。
我们知道在 Git 里两个重要的资料结构,分別是「物件」与「索引」,这篇文章主要用来解说「索引」的细节。使用 Git 版本控制的过程中,或许你可以很轻易的了解 git 指令的用法,不过那很容易流于死记,无法灵活运用,连 Linus Torvalds 都在邮件清单 (Mailing List) 中提到:「在使用者了解索引的意义之前,是无法完整了解 Git 的能力的」,因此,了解「索引」的用途十分重要。

关于索引
-------
## 关于索引

简单来说,「索引」的目的主要用来记录「有哪些文件即将要被提交到下一个 commit 版本中」。

Expand All @@ -21,8 +19,7 @@

举个例子来说,指令 `git diff --cached` 就与 `git diff --staged` 是完全同义的。

操作索引的指令
-------------
## 操作索引的指令

由于「索引」对 Git 来说十分重要,在大多数的指令中都会有跟 Git 索引相关的参数可用,不过我们大致列出几个直接与「索引」相关的指令来解说。

Expand All @@ -37,58 +34,60 @@
* modified (已修改的,代表文件已经被编辑过,或是文件内容与 HEAD 内容不一致的状态)
* staged (等待被 commit 的,代表下次执行 git commit 会将这些文件全部送入版本库)



### git status

取得 **工作目录** (working tree) 下的状态。

由于先前已经讲过仓库、工作目录、物件与索引之间的关系,我们用一句话说明这关系:

Git 仓库的运作,是将工作目录里的变化,通过更新索引的方式,将资料写入成 Git 物件。
```txt
Git 仓库的运作,是将工作目录里的变化,通过更新索引的方式,将资料写入成 Git 物件。
```

这里的 `git status` 指令,目的是显示出 **目前最新版****索引档** 之间的差异,这当中的差异包含了一些微妙的关系,我们用一个例子来解释这层关系。

以下是执行 git status 的结果:

G:\git-demo>git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: c.txt
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# b.txt
```sh
G:\git-demo>git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: c.txt
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# b.txt
```

这里你会看到有三种不同的分组,分別是:

* Changes to be committed (准备提交的变更)
* 这区有个 `new file: c.txt` 文件,代表 c.txt 是一个新文件,而且已经被标示可提交。
* 这代表着几件事:
1. **目前最新版** 并没有 c.txt 这个文件
2. **索引档** 已经加入了这个 c.txt 文件
3. 所以该文件会在执行 git commit 之后被存入下一个版本
* 这区有个 `new file: c.txt` 文件,代表 c.txt 是一个新文件,而且已经被标示可提交。
* 这代表着几件事:
1. **目前最新版** 并没有 c.txt 这个文件
2. **索引档** 已经加入了这个 c.txt 文件
3. 所以该文件会在执行 git commit 之后被存入下一个版本
* Changes not staged for commit (尚未准备提交的变更)
* 这区有个 `modified: a.txt` 文件,代表 a.txt 已经被变更,但尚未标示可提交。 (not staged)
* 这代表着几件事:
1. **目前最新版** 也有 a.txt 这个文件
2. **索引档** 尚未加入 a.txt 这个文件
3. 所以该文件就算执行了 git commit 也不会在下一版中出现
* 这区有个 `modified: a.txt` 文件,代表 a.txt 已经被变更,但尚未标示可提交。 (not staged)
* 这代表着几件事:
1. **目前最新版** 也有 a.txt 这个文件
2. **索引档** 尚未加入 a.txt 这个文件
3. 所以该文件就算执行了 git commit 也不会在下一版中出现
* Untracked files (未追踪的变更)
* 这区有个 `b.txt` 文件,代表 b.txt 尚未被追踪。(untracked)
* 这代表着几件事:
1. **目前最新版** 没有 b.txt 这个文件
2. **索引档** 也没有 b.txt 这个文件
3. 所以该文件就算执行了 git commit 也不会在下一版中出现
* 这区有个 `b.txt` 文件,代表 b.txt 尚未被追踪。(untracked)
* 这代表着几件事:
1. **目前最新版** 没有 b.txt 这个文件
2. **索引档** 也没有 b.txt 这个文件
3. 所以该文件就算执行了 git commit 也不会在下一版中出现

所以你可以看到,执行 git status 就是为了查出 **目前最新版****索引档** 之间的差异,最终只有 **目前最新版****索引档** 之间有差异的变更,才会真正储存到下一个 commit 物件里。

Expand All @@ -104,7 +103,9 @@

在执行 `git rm filename` 的时候,除了更新索引档之外,连工作目录下的文件也会一并被删除。若你只想删除索引档中的该档,又要保留工作目录下的实体文件,那么你可以在指令列加上 `--cached` 参数,就能做到,例如:

git rm --cached a.txt
```sh
git rm --cached a.txt
```

### git mv

Expand All @@ -122,28 +123,21 @@

![image](figures/07/02.png)


今日小结
-------
## 今日小结

Git 里的「索引」是 Git 版控中最重要的观念,有了这层观念,也自然能得知,为什么每次提交变更都要打一些指令把变更给加进去。当然,也有许多好用的 GUI 工具可以帮你少打许多指令,不过在我们正式开始使用 Git 的 GUI 工具之前,我们还是多靠指令把观念给建立再说吧!


参考连结
-------
## 参考连结

* [Git - Recording Changes to the Repository](https://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository)
* [Pro Git Book](https://progit.org/)
* [Git Magic - 繁体中文版](https://www-cs-students.stanford.edu/~blynn/gitmagic/intl/zh_tw/)
* [Git (software) - Wikipedia, the free encyclopedia](https://en.wikipedia.org/wiki/Git_(software) "Git (software) - Wikipedia, the free encyclopedia")

---


-------
* [回目录](README.md)
* [前一天:解析 Git 资料结构 - 物件结构](06.md)
* [下一天:关于分支的基本观念与使用方式](08.md)

-------


---
Binary file modified zh-cn/figures/07/01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
102 changes: 48 additions & 54 deletions zh-tw/07.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
第 07 天:解析 Git 資料結構 - 索引結構
=====================================================
# 第 07 天:解析 Git 資料結構 - 索引結構

我們知道在 Git 裡兩個重要的資料結構,分別是「物件」與「索引」,這篇文章主要用來解說「索引」的細節。使用 Git 版本控管的過程中,或許你可以很輕易的了解 git 指令的用法,不過那很容易流於死記,無法靈活運用,連 Linus Torvalds 都在郵件清單(Mailing List)中提到:「在使用者了解索引的意義之前,是無法完整了解 Git 的能力的」,因此,了解「索引」的用途十分重要。
我們知道在 Git 裡兩個重要的資料結構,分別是「物件」與「索引」,這篇文章主要用來解說「索引」的細節。使用 Git 版本控管的過程中,或許你可以很輕易的了解 git 指令的用法,不過那很容易流於死記,無法靈活運用,連 Linus Torvalds 都在郵件清單 (Mailing List) 中提到:「在使用者了解索引的意義之前,是無法完整了解 Git 的能力的」,因此,了解「索引」的用途十分重要。

關於索引
-------
## 關於索引

簡單來說,「索引」的目的主要用來紀錄「有哪些檔案即將要被提交到下一個 commit 版本中」。

Expand All @@ -21,8 +19,7 @@

舉個例子來說,指令 `git diff --cached` 就與 `git diff --staged` 是完全同義的。

操作索引的指令
-------------
## 操作索引的指令

由於「索引」對 Git 來說十分重要,在大多數的指令中都會有跟 Git 索引相關的參數可用,不過我們大致列出幾個直接與「索引」相關的指令來解說。

Expand All @@ -37,58 +34,60 @@
* modified (已修改的,代表檔案已經被編輯過,或是檔案內容與 HEAD 內容不一致的狀態)
* staged (等待被 commit 的,代表下次執行 git commit 會將這些檔案全部送入版本庫)



### git status

取得 **工作目錄** (working tree) 下的狀態。

由於先前已經講過儲存庫、工作目錄、物件與索引之間的關係,我們用一句話說明這關係:

Git 儲存庫的運作,是將工作目錄裡的變化,透過更新索引的方式,將資料寫入成 Git 物件。
```txt
Git 儲存庫的運作,是將工作目錄裡的變化,透過更新索引的方式,將資料寫入成 Git 物件。
```

這裡的 `git status` 指令,目的是顯示出 **目前最新版****索引檔** 之間的差異,這當中的差異包含了一些微妙的關係,我們用一個例子來解釋這層關係。

以下是執行 git status 的結果:

G:\git-demo>git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: c.txt
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# b.txt
```sh
G:\git-demo>git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# new file: c.txt
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: a.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# b.txt
```

這裡你會看到有三種不同的分組,分別是:

* Changes to be committed (準備提交的變更)
* 這區有個 `new file: c.txt` 檔案,代表 c.txt 是一個新檔案,而且已經被標示可提交。
* 這代表著幾件事:
1. **目前最新版** 並沒有 c.txt 這個檔案
2. **索引檔** 已經加入了這個 c.txt 檔案
3. 所以該檔案會在執行 git commit 之後被存入下一個版本
* 這區有個 `new file: c.txt` 檔案,代表 c.txt 是一個新檔案,而且已經被標示可提交。
* 這代表著幾件事:
1. **目前最新版** 並沒有 c.txt 這個檔案
2. **索引檔** 已經加入了這個 c.txt 檔案
3. 所以該檔案會在執行 git commit 之後被存入下一個版本
* Changes not staged for commit (尚未準備提交的變更)
* 這區有個 `modified: a.txt` 檔案,代表 a.txt 已經被變更,但尚未標示可提交。 (not staged)
* 這代表著幾件事:
1. **目前最新版** 也有 a.txt 這個檔案
2. **索引檔** 尚未加入 a.txt 這個檔案
3. 所以該檔案就算執行了 git commit 也不會在下一版中出現
* 這區有個 `modified: a.txt` 檔案,代表 a.txt 已經被變更,但尚未標示可提交。 (not staged)
* 這代表著幾件事:
1. **目前最新版** 也有 a.txt 這個檔案
2. **索引檔** 尚未加入 a.txt 這個檔案
3. 所以該檔案就算執行了 git commit 也不會在下一版中出現
* Untracked files (未追蹤的變更)
* 這區有個 `b.txt` 檔案,代表 b.txt 尚未被追蹤。(untracked)
* 這代表著幾件事:
1. **目前最新版** 沒有 b.txt 這個檔案
2. **索引檔** 也沒有 b.txt 這個檔案
3. 所以該檔案就算執行了 git commit 也不會在下一版中出現
* 這區有個 `b.txt` 檔案,代表 b.txt 尚未被追蹤。(untracked)
* 這代表著幾件事:
1. **目前最新版** 沒有 b.txt 這個檔案
2. **索引檔** 也沒有 b.txt 這個檔案
3. 所以該檔案就算執行了 git commit 也不會在下一版中出現

所以你可以看到,執行 git status 就是為了查出 **目前最新版****索引檔** 之間的差異,最終只有 **目前最新版****索引檔** 之間有差異的變更,才會真正儲存到下一個 commit 物件裡。

Expand All @@ -104,7 +103,9 @@

在執行 `git rm filename` 的時候,除了更新索引檔之外,連工作目錄下的檔案也會一併被刪除。若你只想刪除索引檔中的該檔,又要保留工作目錄下的實體檔案,那麼你可以在指令列加上 `--cached` 參數,就能做到,例如:

git rm --cached a.txt
```sh
git rm --cached a.txt
```

### git mv

Expand All @@ -122,28 +123,21 @@

![image](figures/07/02.png)


今日小結
-------
## 今日小結

Git 裡的「索引」是 Git 版控中最重要的觀念,有了這層觀念,也自然能得知,為什麼每次提交變更都要打一些指令把變更給加進去。當然,也有許多好用的 GUI 工具可以幫你少打許多指令,不過在我們正式開始使用 Git 的 GUI 工具之前,我們還是多靠指令把觀念給建立再說吧!


參考連結
-------
## 參考連結

* [Git - Recording Changes to the Repository](https://git-scm.com/book/en/Git-Basics-Recording-Changes-to-the-Repository)
* [Pro Git Book](https://progit.org/)
* [Git Magic - 繁體中文版](https://www-cs-students.stanford.edu/~blynn/gitmagic/intl/zh_tw/)
* [Git (software) - Wikipedia, the free encyclopedia](https://en.wikipedia.org/wiki/Git_(software) "Git (software) - Wikipedia, the free encyclopedia")

---


-------
* [回目錄](README.md)
* [前一天:解析 Git 資料結構 - 物件結構](06.md)
* [下一天:關於分支的基本觀念與使用方式](08.md)

-------


---
Binary file modified zh-tw/figures/07/01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4d9d2eb

Please sign in to comment.