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: en/book/03-git-branching/chapter3.asc
+14-27Lines changed: 14 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,15 +44,13 @@ What happens if you create a new branch? Well, doing so creates a new pointer fo
44
44
$ git branch testing
45
45
----
46
46
47
-
This creates a new pointer at the same commit you’re currently on (see <<history_diagram_a>>).
47
+
This creates a new pointer at the same commit you’re currently on.
48
48
49
-
[[history_diagram_a]]
50
49
.Multiple branches pointing into the commit’s data history
51
50
image::images/18333fig0304-tn.png[Multiple branches pointing into the commit’s data history.]
52
51
53
-
How does Git know what branch you’re currently on? It keeps a special pointer called `HEAD`. 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. In Git, this is a pointer to the local branch you’re currently on. In this case, you’re still on master. The `git branch` command only _created_ a new branch – it didn’t switch to that branch (see <<history_diagram_b>>).
52
+
How does Git know what branch you’re currently on? It keeps a special pointer called `HEAD`. 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. In Git, this is a pointer to the local branch you’re currently on. In this case, you’re still on master. The `git branch` command only _created_ a new branch – it didn’t switch to that branch.
54
53
55
-
[[history_diagram_b]]
56
54
.HEAD file pointing to the branch you’re on
57
55
image::images/18333fig0305-tn.png[HEAD file pointing to the branch you’re on.]
58
56
@@ -63,9 +61,8 @@ To switch to an existing branch, you run the `git checkout` command. Let’s swi
63
61
$ git checkout testing
64
62
----
65
63
66
-
This moves `HEAD` to point to the `testing` branch (see <<history_diagram_c>>).
64
+
This moves `HEAD` to point to the `testing` branch.
67
65
68
-
[[history_diagram_c]]
69
66
.HEAD points to another branch when you switch branches
70
67
image::images/18333fig0306-tn.png[HEAD points to another branch when you switch branches.]
71
68
@@ -134,9 +131,8 @@ At this stage, you’ll receive a call that another issue is critical and you ne
134
131
135
132
==== Basic Branching
136
133
137
-
First, let’s say you’re working on your project and have a couple of commits already (see <<branch_diagram_a>>).
134
+
First, let’s say you’re working on your project and have a couple of commits already.
138
135
139
-
[[branch_diagram_a]]
140
136
.A short and simple commit history
141
137
image::images/18333fig0310-tn.png[A short and simple commit history.]
142
138
@@ -216,9 +212,8 @@ README | 1 -
216
212
217
213
You’ll notice the phrase ``Fast forward'' in that merge. Because the commit pointed to by the branch you merged in was directly upstream of the commit you’re on, Git moves the pointer forward. To phrase that another way, when you try to merge one commit with a commit that can be reached by following the first commit’s history, Git simplifies things by moving the pointer forward because there is no divergent work to merge together – this is called a ``fast forward''.
218
214
219
-
Your change is now in the snapshot of the commit pointed to by the `master` branch, and you can deploy your change (see <<branch_diagram_e>>).
215
+
Your change is now in the snapshot of the commit pointed to by the `master` branch, and you can deploy your change.
220
216
221
-
[[branch_diagram_e]]
222
217
.Your master branch points to the same place as your hotfix branch after the merge
223
218
image::images/18333fig0314-tn.png[Your master branch points to the same place as your hotfix branch after the merge.]
224
219
@@ -448,13 +443,12 @@ Because Git uses a simple three-way merge, merging from one branch into another
448
443
449
444
Many Git developers have a workflow that embraces this approach, such as having only code that is entirely stable in their `master` branch – possibly only code that has been or will be released. They have another parallel branch named develop or next that they work from or use to test stability – it isn’t necessarily always stable, but whenever it gets to a stable state, it can be merged into `master`. It’s used to pull in topic branches (short-lived branches, like your earlier `iss53` branch) when they’re ready, to make sure they pass all the tests and don’t introduce bugs.
450
445
451
-
In reality, we’re talking about pointers moving up the line of commits you’re making. The stable branches are farther down the line in your commit history, and the bleeding-edge branches are farther up the history (see <<lrbranch_a>>).
446
+
In reality, we’re talking about pointers moving up the line of commits you’re making. The stable branches are farther down the line in your commit history, and the bleeding-edge branches are farther up the history.
452
447
453
-
[[lrbranch_a]]
454
448
.More stable branches are generally farther down the commit history
455
449
image::images/18333fig0318-tn.png[More stable branches are generally farther down the commit history.]
456
450
457
-
It’s generally easier to think about them as work silos, where sets of commits graduate to a more stable silo when they’re fully tested (see <<lrbranch_b>>).
451
+
It’s generally easier to think about them as work silos, where sets of commits graduate to a more stable silo when they’re fully tested.
458
452
459
453
[[lrbranch_b]]
460
454
.It may be helpful to think of your branches as silos
@@ -489,33 +483,28 @@ Remote branches are references to the state of branches on your remote repositor
489
483
490
484
They take the form `(remote)/(branch)`. For instance, if you wanted to see what the `master` branch on your `origin` remote looked like as of the last time you communicated with it, you would check the `origin/master` branch. If you were working on an issue with a partner and they pushed up an `iss53` branch, you might have your own local `iss53` branch; but the branch on the server would point to the commit at `origin/iss53`.
491
485
492
-
This may be a bit confusing, so let’s look at an example. Let’s say you have a Git server on your network at `git.ourcompany.com`. If you clone from this, Git automatically names it `origin` for you, pulls down all its data, creates a pointer to where its `master` branch is, and names it `origin/master` locally; and you can’t move it. Git also gives you your own `master` branch starting at the same place as origin’s `master` branch, so you have something to work from (see <<rembranch_a>>).
486
+
This may be a bit confusing, so let’s look at an example. Let’s say you have a Git server on your network at `git.ourcompany.com`. If you clone from this, Git automatically names it `origin` for you, pulls down all its data, creates a pointer to where its `master` branch is, and names it `origin/master` locally; and you can’t move it. Git also gives you your own `master` branch starting at the same place as origin’s `master` branch, so you have something to work from.
493
487
494
-
[[rembranch_a]]
495
488
.A Git clone gives you your own master branch and origin/master pointing to origin’s master branch
496
489
image::images/18333fig0322-tn.png[A Git clone gives you your own master branch and origin/master pointing to origin’s master branch.]
497
490
498
-
If you do some work on your local master branch, and, in the meantime, someone else pushes to `git.ourcompany.com` and updates its master branch, then your histories move forward differently. Also, as long as you stay out of contact with your origin server, your `origin/master` pointer doesn’t move (see <<rembranch_b>>).
491
+
If you do some work on your local master branch, and, in the meantime, someone else pushes to `git.ourcompany.com` and updates its master branch, then your histories move forward differently. Also, as long as you stay out of contact with your origin server, your `origin/master` pointer doesn’t move.
499
492
500
-
[[rembranch_b]]
501
493
.Working locally and having someone push to your remote server makes each history move forward differently
502
494
image::images/18333fig0323-tn.png[Working locally and having someone push to your remote server makes each history move forward differently.]
503
495
504
-
To synchronize your work, you run a `git fetch origin` command. This command looks up which server origin is (in this case, it’s `git.ourcompany.com`), fetches any data from it that you don’t yet have, and updates your local database, moving your `origin/master` pointer to its new, more up-to-date position (see <<rembranch_c>>).
496
+
To synchronize your work, you run a `git fetch origin` command. This command looks up which server origin is (in this case, it’s `git.ourcompany.com`), fetches any data from it that you don’t yet have, and updates your local database, moving your `origin/master` pointer to its new, more up-to-date position.
505
497
506
-
[[rembranch_c]]
507
498
.The git fetch command updates your remote references
508
499
image::images/18333fig0324-tn.png[The git fetch command updates your remote references.]
509
500
510
-
To demonstrate having multiple remote servers and what remote branches for those remote projects look like, let’s assume you have another internal Git server that is used only for development by one of your sprint teams. This server is at `git.team1.ourcompany.com`. You can add it as a new remote reference to the project you’re currently working on by running the `git remote add` command as we covered in <<_git_basics_chapter>>. Name this remote `teamone`, which will be your shortname for that whole URL (see <<rembranch_d>>).
501
+
To demonstrate having multiple remote servers and what remote branches for those remote projects look like, let’s assume you have another internal Git server that is used only for development by one of your sprint teams. This server is at `git.team1.ourcompany.com`. You can add it as a new remote reference to the project you’re currently working on by running the `git remote add` command as we covered in <<_git_basics_chapter>>. Name this remote `teamone`, which will be your shortname for that whole URL.
511
502
512
-
[[rembranch_d]]
513
503
.Adding another server as a remote
514
504
image::images/18333fig0325-tn.png[Adding another server as a remote.]
515
505
516
-
Now, you can run `git fetch teamone` to fetch everything the remote `teamone` server has that you don’t have yet. Because that server is a subset of the data your `origin` server has right now, Git fetches no data but sets a remote branch called `teamone/master` to point to the commit that `teamone` has as its `master` branch (see <<rembranch_e>>).
506
+
Now, you can run `git fetch teamone` to fetch everything the remote `teamone` server has that you don’t have yet. Because that server is a subset of the data your `origin` server has right now, Git fetches no data but sets a remote branch called `teamone/master` to point to the commit that `teamone` has as its `master` branch.
517
507
518
-
[[rembranch_e]]
519
508
.You get a reference to teamone’s master branch position locally
520
509
image::images/18333fig0326-tn.png[You get a reference to teamone’s master branch position locally.]
521
510
@@ -637,9 +626,8 @@ It works by going to the common ancestor of the two branches (the one you’re o
637
626
.Rebasing the change introduced in C3 onto C4
638
627
image::images/18333fig0329-tn.png[Rebasing the change introduced in C3 onto C4.]
639
628
640
-
At this point, you can go back to the master branch and do a fast-forward merge (see <<rbdiag_d>>).
629
+
At this point, you can go back to the master branch and do a fast-forward merge.
641
630
642
-
[[rbdiag_d]]
643
631
.Fast-forwarding the master branch
644
632
image::images/18333fig0330-tn.png[Fast-forwarding the master branch.]
645
633
@@ -742,9 +730,8 @@ Next, the person who pushed the merged work decides to go back and rebase their
742
730
.Someone pushes rebased commits, abandoning commits you’ve based your work on
743
731
image::images/18333fig0338-tn.png[Someone pushes rebased commits, abandoning commits you’ve based your work on.]
744
732
745
-
At this point, you have to merge this work in again, even though you’ve already done so. Rebasing changes the SHA-1 hashes of these commits so to Git they look like new commits, when in fact you already have the C4 work in your history (see <<rbdiag_l>>).
733
+
At this point, you have to merge this work in again, even though you’ve already done so. Rebasing changes the SHA-1 hashes of these commits so to Git they look like new commits, when in fact you already have the C4 work in your history.
746
734
747
-
[[rbdiag_l]]
748
735
.You merge in the same work again into a new merge commit
749
736
image::images/18333fig0339-tn.png[You merge in the same work again into a new merge commit.]
0 commit comments