Skip to content

Commit b065269

Browse files
committed
add some command line examples
1 parent e452200 commit b065269

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

book/03-git-branching/1-git-branching.asc

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
== Git Branching
1+
[[_git_branching]]
2+
== Git Branching(((git, branching)))
23

34
Nearly every VCS has some form of branching support.
45
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
78
Some people refer to Git's branching model as its ``killer feature,'' and it certainly sets Git apart in the VCS community.
89
Why is it so special?
910
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.
1213

13-
=== What a Branch Is
14+
=== Branches in a Nutshell
1415

1516
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.
1717

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.
1922

2023
To visualize this, let's assume that you have a directory containing three files, and you stage them all and commit.
2124
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`.
4447
As you start making commits, you're given a master branch that points to the last commit you made.
4548
Every time you commit, it moves forward automatically.
4649

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+
4755
.A branch and its commit history
4856
image::images/18333fig0303-tn.png[A branch and its commit history.]
4957

58+
==== Creating a new branch
59+
5060
What happens if you create a new branch?
5161
Well, doing so creates a new pointer for you to move around.
5262
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
7282
.HEAD pointing to a branch
7383
image::images/18333fig0305-tn.png[HEAD pointing to a branch.]
7484

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+
7599
To switch to an existing branch, you run the `git checkout` command.
76100
Let's switch to the new testing branch:
77101

@@ -129,6 +153,19 @@ And you did all that with simple `branch`, `checkout`, and `commit` commands.
129153
.Divergent history
130154
image::images/18333fig0309-tn.png[Divergent history.]
131155

156+
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+
132169
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.
133170
Creating a new branch is as quick and simple as writing 41 bytes to a file (40 characters and a newline).
134171

0 commit comments

Comments
 (0)