Skip to content

Commit 58c95fe

Browse files
committed
Merge pull request #27 from YueLinHo/3.1-nutshell
3.1 nutshell
2 parents 90cb422 + 3a191aa commit 58c95fe

File tree

2 files changed

+80
-80
lines changed

2 files changed

+80
-80
lines changed
Lines changed: 79 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,81 @@
11
[[_git_branches_overview]]
2-
=== 分支的簡介
2+
=== 簡述分支
33

4-
To really understand the way Git does branching, we need to take a step back and examine how Git stores its data.
4+
為了理解 Git 分支(branch)的使用方式,我們需要回顧一下 Git 是如何保存資料的。
55

6-
As you may remember from <<_getting_started>>, Git doesn't store data as a series of changesets or differences, but instead as a series of snapshots.
6+
或許你還記得 <<_getting_started>> 的內容,Git 保存的不是變更集或者差異內容,而是一系列快照。
77

8-
When you make a commit, Git stores a commit object that contains a pointer to the snapshot of the content you staged.
9-
This object also contains the author's name and email, the message that you typed, and pointers to the commit or commits that directly came before this commit (its parent or parents): zero parents for the initial commit, one parent for a normal commit, and multiple parents for a commit that results from a merge of two or more branches.
8+
當你製造一個提交(commit)時,Git 會儲存一個提交物件,該物件內容包含一個指標,用來代表已預存的快照內容;
9+
這個物件內容還包含「作者名字和電子郵件」、「你輸入的訊息內容」、「指向前一個提交的指標(該提交的親代提交)」:沒有親代(parent)提交表示它是初始的第一個提交,一般情況下只有一個親代提交,超過一個親代提交表示它是從二個以上的分支合併而來的。
1010

11-
To visualize this, let's assume that you have a directory containing three files, and you stage them all and commit.
12-
Staging the files checksums each one (the SHA-1 hash we mentioned in <<_getting_started>>), stores that version of the file in the Git repository (Git refers to them as blobs), and adds that checksum to the staging area:
11+
為了具體說明,讓我們假設你有一個目錄包含了三個檔案,你預存(stage)並提交了它們;
12+
檔案預存操作會對每一個檔案內容(譯註:請注意,只有檔案「內容」)計算雜湊值(即 <<_getting_started>> 中提到的 SHA-1 雜湊值),然後把那個檔案內容版本保存到 Git 版本庫中(Git 把它們視為 blob 類型的物件),再將這個雜湊值寫入預存區(staging area):
1313

1414
[source,console]
1515
----
1616
$ git add README test.rb LICENSE
1717
$ git commit -m 'The initial commit of my project'
1818
----
1919

20-
When you create the commit by running `git commit`, Git checksums each subdirectory (in this case, just the root project directory) and stores those tree objects in the Git repository.
21-
Git then creates a commit object that has the metadata and a pointer to the root project tree so it can re-create that snapshot when needed.(((git commands, commit)))
20+
當使用 `git commit` 建立一個提交時,Git 會先計算每一個子目錄(本例中則只有專案根目錄)的雜湊值,然後在 Git 版本庫中將這些目錄記錄為樹(tree)物件;
21+
之後 Git 建立提交物件,它除了包含相關提交資訊以外,還包含著指向專案根目錄的樹物件指標,如此它就可以在需要的時候重建此次快照內容。(((git commands, commit)))
2222

23-
Your Git repository now contains five objects: one blob for the contents of each of your three files, one tree that lists the contents of the directory and specifies which file names are stored as which blobs, and one commit with the pointer to that root tree and all the commit metadata.
23+
你的 Git 版本庫現在有五個物件:三個 blob 物件用來儲存檔案內容、一個樹物件用來列出目錄的內容並紀錄各個檔案所對應的 blob 物件、一個提交用來記錄根目錄的樹物件和其他提交資訊。
2424

25-
.A commit and its tree
26-
image::images/commit-and-tree.png[A commit and its tree.]
25+
.單個提交在版本庫中的資料結構
26+
image::images/commit-and-tree.png[單個提交在版本庫中的資料結構。]
2727

28-
If you make some changes and commit again, the next commit stores a pointer to the commit that came immediately before it.
28+
如果你做一些修改並再次提交,這次的提交會再包含一個指向上次提交的指標(譯注:即下圖中的 parent 欄位)。
2929

30-
.Commits and their parents
31-
image::images/commits-and-parents.png[Commits and their parents.]
30+
.提交和它們的親代提交
31+
image::images/commits-and-parents.png[提交和它們的親代提交。]
3232

33-
A branch in Git is simply a lightweight movable pointer to one of these commits.
34-
The default branch name in Git is `master`.
35-
As you start making commits, you're given a `master` branch that points to the last commit you made.
36-
Every time you commit, it moves forward automatically.
33+
Git 分支其實只是一個指向某提交的可移動輕量級指標,
34+
Git 預設分支名稱是 `master`
35+
隨著不斷地製作提交,`master` 分支會為你一直指向最後一個提交,
36+
它在每次提交的時候都會自動向前移動。
3737

3838
[NOTE]
3939
====
40-
The ``master'' branch in Git is not a special branch.(((master)))
41-
It is exactly like any other branch.
42-
The only reason nearly every repository has one is that the `git init` command creates it by default and most people don't bother to change it.
40+
master」在 Git 中並不是一個特殊的分支,(((master)))
41+
它和其它分支並無分別,
42+
之所以幾乎每個版本庫裡都會有這個分支的原因是 `git init` 命令的預設行為會產生它,而大部分的人就這麼直接使用它。
4343
====
4444

45-
.A branch and its commit history
46-
image::images/branch-and-history.png[A branch and its commit history.]
45+
.分支及其提交歷史
46+
image::images/branch-and-history.png[分支及其提交歷史。]
4747

4848
[[_create_new_branch]]
4949
==== 建立一個新的分支
5050

5151
(((branches, creating)))
52-
What happens if you create a new branch?
53-
Well, doing so creates a new pointer for you to move around.
54-
Let's say you create a new branch called testing.
55-
You do this with the `git branch` command:(((git commands, branch)))
52+
建立一個新分支會發生什麼事呢?
53+
答案很簡單,建立一個新的、可移動的指標;
54+
比如新建一個 testing 分支,
55+
可以使用 `git branch` 命令:(((git commands, branch)))
5656

5757
[source,console]
5858
----
5959
$ git branch testing
6060
----
6161

62-
This creates a new pointer to the same commit you're currently on.
62+
這會在目前提交上新建一個指標。
6363

64-
.Two branches pointing into the same series of commits
65-
image::images/two-branches.png[Two branches pointing into the same series of commits.]
64+
.二個分支都指向同一系列的提交歷史
65+
image::images/two-branches.png[二個分支都指向同一系列的提交歷史。]
6666

67-
How does Git know what branch you're currently on?
68-
It keeps a special pointer called `HEAD`.
69-
Note that this is a lot different than the concept of `HEAD` in other VCSs you may be used to, such as Subversion or CVS.
70-
In Git, this is a pointer to the local branch you're currently on.
71-
In this case, you're still on `master`.
72-
The `git branch` command only _created_ a new branch – it didn't switch to that branch.
67+
Git 如何知道你目前在哪個分支上工作的呢?
68+
其實它保存了一個名為 `HEAD` 的特別指標;
69+
請注意它和你可能慣用的其他 VCSs 裡的 `HEAD` 概念大不相同,比如 Subversion CVS
70+
Git 中,它就是一個指向你正在工作中的本地分支的指標(譯注:HEAD 等於「目前的」),
71+
所以在這個例子中,你仍然在 `master` 分支上工作;
72+
執行 `git branch` 命令,只是「建立」一個新的分支——它並不會切換到這個分支。
7373

74-
.HEAD pointing to a branch
75-
image::images/head-to-master.png[HEAD pointing to a branch.]
74+
.HEAD 指向一個分支
75+
image::images/head-to-master.png[HEAD 指向一個分支。]
7676

77-
You can easily see this by running a simple `git log` command that shows you where the branch pointers are pointing.
78-
This option is called `--decorate`.
77+
你可以很輕鬆地看到分支指標指向何處,只需透過一個簡單的 `git log` 命令,
78+
加上 `--decorate` 選項。
7979

8080
[source,console]
8181
----
@@ -85,80 +85,80 @@ f30ab (HEAD -> master, testing) add feature #32 - ability to add new formats to
8585
98ca9 The initial commit of my project
8686
----
8787

88-
You can see the ``master'' and ``testing'' branches that are right there next to the `f30ab` commit.
88+
你可以看到「master」和「testing」分支就顯示在 `f30ab` 提交旁邊。
8989

9090
[[_switching_branches]]
9191
==== 在分支之間切換
9292

9393
(((branches, switching)))
94-
To switch to an existing branch, you run the `git checkout` command.(((git commands, checkout)))
95-
Let's switch to the new `testing` branch:
94+
要切換到一個已經存在的分支,你可以執行 `git checkout` 命令,(((git commands, checkout)))
95+
讓我們切換到新的 `testing` 分支:
9696

9797
[source,console]
9898
----
9999
$ git checkout testing
100100
----
101101

102-
This moves `HEAD` to point to the `testing` branch.
102+
這會移動 `HEAD` 並指向 `testing` 分支。
103103

104-
.HEAD points to the current branch
105-
image::images/head-to-testing.png[HEAD points to the current branch.]
104+
.HEAD 指向的分支是目前分支
105+
image::images/head-to-testing.png[HEAD 指向的分支是目前分支。]
106106

107-
What is the significance of that?
108-
Well, let's do another commit:
107+
這樣做有什麼意義呢?
108+
好吧!讓我們再提交一次:
109109

110110
[source,console]
111111
----
112112
$ vim test.rb
113113
$ git commit -a -m 'made a change'
114114
----
115115

116-
.The HEAD branch moves forward when a commit is made
117-
image::images/advance-testing.png[The HEAD branch moves forward when a commit is made.]
116+
.當再次提交時,被 HEAD 指向的分支會往前走
117+
image::images/advance-testing.png[當再次提交時,被 HEAD 指向的分支會往前走。]
118118

119-
This is interesting, because now your `testing` branch has moved forward, but your `master` branch still points to the commit you were on when you ran `git checkout` to switch branches.
120-
Let's switch back to the `master` branch:
119+
非常有趣,現在 `testing` 分支向前移動了,而 `master` 分支仍然指向當初在執行 `git checkout` 時所在的提交,
120+
讓我們切回 `master` 分支看看:
121121

122122
[source,console]
123123
----
124124
$ git checkout master
125125
----
126126

127-
.HEAD moves when you checkout
128-
image::images/checkout-master.png[HEAD moves when you checkout.]
127+
.當你檢出時,HEAD 會移動
128+
image::images/checkout-master.png[當你檢出時,HEAD 會移動。]
129129

130-
That command did two things.
131-
It moved the HEAD pointer back to point to the `master` branch, and it reverted the files in your working directory back to the snapshot that `master` points to.
132-
This also means the changes you make from this point forward will diverge from an older version of the project.
133-
It essentially rewinds the work you've done in your `testing` branch so you can go in a different direction.
130+
這條命令做了兩件事,
131+
它把 HEAD 指標移回去並指向 `master` 分支,然後把工作目錄中的檔案換成 `master` 分支所指向的快照內容;
132+
也就是說,現在開始所做的改動,將基於專案中較舊的版本,然後與其它提交歷史分離開來;
133+
它實際上是取消你在 `testing` 分支裡所做的修改,這樣你就可以往不同方向前進。
134134

135135
[NOTE]
136-
.Switching branches changes files in your working directory
136+
.切換分支會修改工作目錄裡的檔案
137137
====
138-
It's important to note that when you switch branches in Git, files in your working directory will change.
139-
If you switch to an older branch, your working directory will be reverted to look like it did the last time you committed on that branch.
140-
If Git cannot do it cleanly, it will not let you switch at all.
138+
重要的是要注意:當你在 Git 中切換分支時,工作目錄內的檔案將會被修改;
139+
如果切換到舊分支,你的工作目錄會回復到看起來就像當初你最後一次在這個分支提交時的樣子。
140+
如果 Git 無法很乾淨地切換過去,它就不會讓你切換過去。
141141
====
142142

143-
Let's make a few changes and commit again:
143+
讓我們做一些修改並再次提交:
144144

145145
[source,console]
146146
----
147147
$ vim test.rb
148148
$ git commit -a -m 'made other changes'
149149
----
150150

151-
Now your project history has diverged (see <<divergent_history>>).
152-
You created and switched to a branch, did some work on it, and then switched back to your main branch and did other work.
153-
Both of those changes are isolated in separate branches: you can switch back and forth between the branches and merge them together when you're ready.
154-
And you did all that with simple `branch`, `checkout`, and `commit` commands.
151+
現在你的專案歷史開始分離了(詳見 <<divergent_history>>);
152+
你建立並切換到新分支,在上面進行了一些工作,然後切換回到主分支進行了另外一些工作,
153+
雙方的改變分別隔離在不同的分支裡:你可以在不同分支裡反覆切換,並在時機成熟時把它們合併到一起;
154+
而所有這些工作只需要簡單的 `branch``checkout``commit` 命令。
155155

156156
[[divergent_history]]
157-
.Divergent history
158-
image::images/advance-master.png[Divergent history.]
157+
.分離的歷史
158+
image::images/advance-master.png[分離的歷史。]
159159

160-
You can also see this easily with the `git log` command.
161-
If you run `git log --oneline --decorate --graph --all` it will print out the history of your commits, showing where your branch pointers are and how your history has diverged.
160+
你一樣可以從 `git log` 中輕鬆地看出這件事,
161+
執行 `git log --oneline --decorate --graph --all`,它會印出你的提交歷史,顯示你的分支指標在哪裡,以及歷史如何被分離開來。
162162

163163
[source,console]
164164
----
@@ -171,12 +171,12 @@ $ git log --oneline --decorate --graph --all
171171
* 98ca9 initial commit of my project
172172
----
173173

174-
Because a branch in Git is in actuality a simple file that contains the 40 character SHA-1 checksum of the commit it points to, branches are cheap to create and destroy.
175-
Creating a new branch is as quick and simple as writing 41 bytes to a file (40 characters and a newline).
174+
由於 Git 分支實際上只是一個檔案,該檔案內容是這個分支指向的提交的雜湊值(40 個字元長度的 SHA-1 字串),所以建立和銷毀一個分支就變得非常廉價;
175+
新建一個分支就是向一個檔寫入 41 個位元組(40 個字元外加一個換行符號)那樣地簡單和快速。
176176

177-
This is in sharp contrast to the way most older VCS tools branch, which involves copying all of the project's files into a second directory.
178-
This can take several seconds or even minutes, depending on the size of the project, whereas in Git the process is always instantaneous.
179-
Also, because we're recording the parents when we commit, finding a proper merge base for merging is automatically done for us and is generally very easy to do.
180-
These features help encourage developers to create and use branches often.
177+
這樣的分支功能和大多數舊 VCS 的分支功能形成了鮮明的對比,有些分支功能甚至需要複製專案中全部的檔案到另一個資料夾,
178+
而根據專案檔案數量和大小的不同,可能花費的時間快則幾秒,慢則數分鐘;而在 Git 中幾乎都在瞬間完成。
179+
還有,因為每次提交時都記錄了親代資訊,將來要合併分支時,它通常會幫我們自動並輕鬆地找到適當的合併基礎;
180+
這樣子的特性在無形間鼓勵了開發者頻繁地建立和使用分支。
181181

182-
Let's see why you should do so.
182+
讓我們來瞧一瞧為什麼你應該要這麼做。

status.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"1-git-branching.asc": 100,
2828
"sections/basic-branching-and-merging.asc": 1,
2929
"sections/branch-management.asc": 1,
30-
"sections/nutshell.asc": 1,
30+
"sections/nutshell.asc": 100,
3131
"sections/rebasing.asc": 1,
3232
"sections/remote-branches.asc": 1,
3333
"sections/workflows.asc": 1

0 commit comments

Comments
 (0)