You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/03-git-branching/1-git-branching.asc
+43-6Lines changed: 43 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
-
== Git Branching
1
+
[[_git_branching]]
2
+
== Git Branching(((git, branching)))
2
3
3
4
Nearly every VCS has some form of branching support.
4
5
Branching means you diverge from the main line of development and continue to do work without messing with that main line.
@@ -7,15 +8,17 @@ In many VCS tools, this is a somewhat expensive process, often requiring you to
7
8
Some people refer to Git's branching model as its ``killer feature,'' and it certainly sets Git apart in the VCS community.
8
9
Why is it so special?
9
10
The way Git branches is incredibly lightweight, making branching operations nearly instantaneous, and switching back and forth between branches generally just as fast.
10
-
Unlike many other VCSs, Git encourages a workflow that branches and merges often, even multiple times in a day.
11
-
Understanding and mastering this feature gives you a powerful and unique tool and can literally change the way that you develop.
11
+
Unlike many other VCSs, Git encourages workflows that branch and merge often, even multiple times in a day.
12
+
Understanding and mastering this feature gives you a powerful and unique tool and can entirely change the way that you develop.
12
13
13
-
=== What a Branch Is
14
+
=== Branches in a Nutshell
14
15
15
16
To really understand the way Git does branching, we need to take a step back and examine how Git stores its data.
16
-
As you may remember from <<_getting_started>>, Git doesn't store data as a series of changesets or deltas, but instead as a series of snapshots.
17
17
18
-
When you make a commit, Git stores a commit object that contains a pointer to the snapshot of the content you staged, the author and message metadata, and zero or more pointers to the commit or commits that were the direct parents of this commit: zero parents for the first commit, one parent for a normal commit, and multiple parents for a commit that results from a merge of two or more branches.
18
+
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.
19
+
20
+
When you make a commit, Git stores a commit object that contains a pointer to the snapshot of the content you staged.
21
+
This object also contains the authors name and email, the message that you typed, and pointers to the commit or commits that directly came bfore this commit (it's 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.
19
22
20
23
To visualize this, let's assume that you have a directory containing three files, and you stage them all and commit.
21
24
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:
@@ -44,9 +47,16 @@ The default branch name in Git is `master`.
44
47
As you start making commits, you're given a master branch that points to the last commit you made.
45
48
Every time you commit, it moves forward automatically.
46
49
50
+
[NOTE]
51
+
====
52
+
The ``master'' branch in Git is not a special branch. It is exactly like any other branch. 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.
53
+
====
54
+
47
55
.A branch and its commit history
48
56
image::images/18333fig0303-tn.png[A branch and its commit history.]
49
57
58
+
==== Creating a new branch
59
+
50
60
What happens if you create a new branch?
51
61
Well, doing so creates a new pointer for you to move around.
52
62
Let's say you create a new branch called testing.
@@ -72,6 +82,20 @@ The `git branch` command only _created_ a new branch – it didn't switch to tha
72
82
.HEAD pointing to a branch
73
83
image::images/18333fig0305-tn.png[HEAD pointing to a branch.]
74
84
85
+
You can easily see this by running a simple `git log` command that shows you where the branch pointers are pointing. This option is called `--decorate`.
86
+
87
+
[source,shell]
88
+
----
89
+
$ git log --oneline --decorate
90
+
f30ab (HEAD, master, testing) add feature #32 - ability to add new formats to the central library
91
+
34ac2 fixed bug #1328 - stack overflow under certain conditions
92
+
98ca9 initial commit of my project
93
+
----
94
+
95
+
You can see the ``master'' and ``testing'' branches that are right there next to the `f30ab` commit.
96
+
97
+
==== Switching branches
98
+
75
99
To switch to an existing branch, you run the `git checkout` command.
76
100
Let's switch to the new testing branch:
77
101
@@ -129,6 +153,19 @@ And you did all that with simple `branch`, `checkout`, and `commit` commands.
You can also see this easily with the `git log` command. 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.
157
+
158
+
[source,shell]
159
+
----
160
+
$ git log --oneline --decorate --graph --all
161
+
* e6a539b (HEAD, master) made other changes
162
+
| * a41d33e (testing) made a change
163
+
|/
164
+
* b1d18f0 add feature #32 - ability to add new formats to the central library
165
+
* 9eb453a fixed bug #1328 - stack overflow under certain conditions
166
+
* b8a510a initial commit of my project
167
+
----
168
+
132
169
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.
133
170
Creating a new branch is as quick and simple as writing 41 bytes to a file (40 characters and a newline).
0 commit comments