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
+15-9Lines changed: 15 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -137,6 +137,12 @@ It moved the HEAD pointer back to point to the master branch, and it reverted th
137
137
This also means the changes you make from this point forward will diverge from an older version of the project.
138
138
It essentially rewinds the work you've done in your testing branch so you can go in a different direction.
139
139
140
+
[NOTE]
141
+
.Switching branches changes files in your working directory
142
+
====
143
+
It's important to note that when you switch branches in Git, files in your working directory will change. 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. If Git cannot do it cleanly, it will not let you switch at all.
144
+
====
145
+
140
146
Let's make a few changes and commit again:
141
147
142
148
[source,shell]
@@ -158,12 +164,12 @@ You can also see this easily with the `git log` command. If you run `git log --o
158
164
[source,shell]
159
165
----
160
166
$ git log --oneline --decorate --graph --all
161
-
* e6a539b (HEAD, master) made other changes
162
-
| * a41d33e (testing) made a change
167
+
* c2b9e (HEAD, master) made other changes
168
+
| * 87ab2 (testing) made a change
163
169
|/
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
170
+
* f30ab add feature #32 - ability to add new formats to the central library
171
+
* 34ac2 fixed bug #1328 - stack overflow under certain conditions
172
+
* 98ca9 initial commit of my project
167
173
----
168
174
169
175
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.
@@ -238,7 +244,7 @@ All you have to do is switch back to your `master` branch.
238
244
239
245
However, before you do that, note that if your working directory or staging area has uncommitted changes that conflict with the branch you're checking out, Git won't let you switch branches.
240
246
It's best to have a clean working state when you switch branches.
241
-
There are ways to get around this (namely, stashing and commit amending) that we'll cover later.
247
+
There are ways to get around this (namely, stashing and commit amending) that we'll cover <<_git_stashing, later>>.
242
248
For now, let's assume you've committed all your changes, so you can switch back to your master branch:
243
249
244
250
[source,shell]
@@ -248,15 +254,15 @@ Switched to branch 'master'
248
254
----
249
255
250
256
At this point, your project working directory is exactly the way it was before you started working on issue #53, and you can concentrate on your hotfix.
251
-
This is an important point to remember: Git resets your working directory to look like the snapshot of the commit that the branch your checkout points to.
257
+
This is an important point to remember: when you switch branches, Git resets your working directory to look like it did the last time you committed on that branch.
252
258
It adds, removes, and modifies files automatically to make sure your working copy is what the branch looked like on your last commit to it.
253
259
254
260
Next, you have a hotfix to make.
255
261
Let's create a hotfix branch on which to work until it's completed:
256
262
257
263
[source,shell]
258
264
----
259
-
$ git checkout -b 'hotfix'
265
+
$ git checkout -b hotfix
260
266
Switched to a new branch 'hotfix'
261
267
$ vim index.html
262
268
$ git commit -a -m 'fixed the broken email address'
@@ -281,7 +287,7 @@ Fast-forward
281
287
----
282
288
283
289
You'll notice the phrase ``fast-forward'' in that merge.
284
-
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.
290
+
Because the commit pointed to by the branch you merged in was directly upstream of the commit you're on, Git simply moves the pointer forward.
285
291
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.''
286
292
287
293
Your change is now in the snapshot of the commit pointed to by the `master` branch, and you can deploy the fix.
0 commit comments