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/07-git-tools/sections/revision-selection.asc
+42-37Lines changed: 42 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,19 @@
1
1
[[_revision_selection]]
2
2
=== Revision Selection
3
3
4
-
Git allows you to specify specific commits or a range of commits in several ways.
4
+
Git allows you to refer to a set of commits or a range of commits in several ways.
5
5
They aren’t necessarily obvious but are helpful to know.
6
6
7
7
==== Single Revisions
8
8
9
-
You can obviously refer to a commit by the SHA-1 hash that it’s given, but there are more human-friendly ways to refer to commits as well.
10
-
This section outlines the various ways you can refer to a single commit.
9
+
You can obviously refer to any single commit by its full, 40-character SHA-1 hash, but there are more human-friendly ways to refer to commits as well.
10
+
This section outlines the various ways you can refer to any commit.
11
11
12
12
==== Short SHA-1
13
13
14
-
Git is smart enough to figure out what commit you meant to type if you provide the first few characters, as long as your partial SHA-1 is at least four characters long and unambiguous – that is, only one object in the current repository begins with that partial SHA-1.
14
+
Git is smart enough to figure out what commit you're referring to if you provide the first few characters of the SHA-1 hash, as long as that partial hash is at least four characters long and unambiguous; that is, no other object in the object database can have a hash that begins with the same prefix.
15
15
16
-
For example, to see a specific commit, suppose you run a `git log` command and identify the commit where you added certain functionality:
16
+
For example, to examine a specific commit where you know you added certain functionality, you might first run `git log` command to locate the commit:
In this case, choose `1c002dd...`. If you `git show` that commit, the following commands are equivalent (assuming the shorter versions are unambiguous):
41
+
In this case, say you're interested in the commit whose hash begins with `1c002dd...`.
42
+
You can inspect that commit with any of the following variations of `git show` (assuming the shorter versions are unambiguous):
42
43
43
44
[source,console]
44
45
----
@@ -59,17 +60,16 @@ a11bef0 first commit
59
60
----
60
61
61
62
Generally, eight to ten characters are more than enough to be unique within a project.
62
-
63
-
As an example, the Linux kernel, which is a pretty large project with over 450k commits and 3.6 million objects, has no two objects whose SHA-1s overlap more than the first 11 characters.
63
+
For example, as of October 2017, the Linux kernel (which is a fairly sizable project) has over 700,000 commits and almost six million objects, with no two objects whose SHA-1s are identical in the first 11 characters.
64
64
65
65
[NOTE]
66
66
.A SHORT NOTE ABOUT SHA-1
67
67
====
68
68
69
-
A lot of people become concerned at some point that they will, by random happenstance, have two objects in their repository that hash to the same SHA-1 value.
69
+
A lot of people become concerned at some point that they will, by random happenstance, have two distinct objects in their repository that hash to the same SHA-1 value.
70
70
What then?
71
71
72
-
If you do happen to commit an object that hashes to the same SHA-1 value as a previous object in your repository, Git will see the previous object already in your Git database and assume it was already written.
72
+
If you do happen to commit an object that hashes to the same SHA-1 value as a previous _different_ object in your repository, Git will see the previous object already in your Git database, assume it was already written and simply reuse it.
73
73
If you try to check out that object again at some point, you’ll always get the data of the first object.
74
74
75
75
However, you should be aware of how ridiculously unlikely this scenario is.
@@ -89,9 +89,8 @@ Thus, a SHA-1 collision is less likely than every member of your programming tea
89
89
[[_branch_references]]
90
90
==== Branch References
91
91
92
-
The most straightforward way to specify a commit requires that it has a branch reference pointed at it.
93
-
Then, you can use a branch name in any Git command that expects a commit object or SHA-1 value.
94
-
For instance, if you want to show the last commit object on a branch, the following commands are equivalent, assuming that the `topic1` branch points to `ca82a6d`:
92
+
One straightforward way to refer to a particular commit is if it's the commit at the tip of a branch; in that case, you can simply use the branch name in any Git command that expects a reference to a commit.
93
+
For instance, if you want to examine the last commit object on a branch, the following commands are equivalent, assuming that the `topic1` branch points to commit `ca82a6d...`:
One of the things Git does in the background while you’re working away is keep a ``reflog'' – a log of where your HEAD and branch references have been for the last few months.
115
+
One of the things Git does in the background while you’re working away is keep a ``reflog'' -- a log of where your HEAD and branch references have been for the last few months.
117
116
118
117
You can see your reflog by using `git reflog`:
119
118
@@ -130,8 +129,8 @@ d921970 HEAD@{1}: merge phedders/rdocs: Merge made by the 'recursive' stategy.
130
129
----
131
130
132
131
Every time your branch tip is updated for any reason, Git stores that information for you in this temporary history.
133
-
And you can specify older commits with this data, as well.
134
-
If you want to see the fifth prior value of the HEAD of your repository, you can use the `@{5}` reference that you see in the reflog output:
132
+
You can use your reflog data to refer to older commits as well.
133
+
For example, if you want to see the fifth prior value of the HEAD of your repository, you can use the `@{5}` reference that you see in the reflog output:
135
134
136
135
[source,console]
137
136
----
@@ -146,7 +145,7 @@ For instance, to see where your `master` branch was yesterday, you can type
146
145
$ git show master@{yesterday}
147
146
----
148
147
149
-
That shows you where the branch tip was yesterday.
148
+
That would show you where tip of your `master` branch was yesterday.
150
149
This technique only works for data that’s still in your reflog, so you can’t use it to look for commits older than a few months.
151
150
152
151
To see reflog information formatted like the `git log` output, you can run `git log -g`:
It’s important to note that the reflog information is strictly local – it’s a log of what you’ve done in your repository.
175
-
The references won’t be the same on someone else’s copy of the repository; and right after you initially clone a repository, you'll have an empty reflog, as no activity has occurred yet in your repository.
176
-
Running `git show HEAD@{2.months.ago}` will work only if you cloned the project at least two months ago – if you cloned it five minutes ago, you’ll get no results.
173
+
It’s important to note that reflog information is strictly local -- it’s a log only of what _you've_ done in _your_ repository.
174
+
The references won’t be the same on someone else’s copy of the repository; also, right after you initially clone a repository, you'll have an empty reflog, as no activity has occurred yet in your repository.
175
+
Running `git show HEAD@{2.months.ago}` will show you the matching commit only if you cloned the project at least two months ago -- if you cloned it any more recently than that, you'll see only your first local commit.
176
+
177
+
[TIP]
178
+
.Think of the reflog as Git's version of shell history
179
+
====
180
+
If you have a UNIX or Linux background, you can think of the reflog as Git's version of shell history, which emphasizes that what's there is clearly relevant only for you and your ``session'', and has nothing to do with anyone else who might be working on the same machine.
181
+
====
177
182
178
183
==== Ancestry References
179
184
@@ -223,7 +228,7 @@ $ git show "HEAD^" # OK
223
228
====
224
229
225
230
You can also specify a number after the `^` – for example, `d921970^2` means ``the second parent of d921970.''
226
-
This syntax is only useful for merge commits, which have more than one parent.
231
+
This syntax is useful only for merge commits, which have more than one parent.
227
232
The first parent is the branch you were on when you merged, and the second is the commit on the branch that you merged in:
You can also combine these syntaxes – you can get the second parent of the previous reference (assuming it was a merge commit) by using `HEAD~3^2`, and so on.
279
+
You can also combine these syntaxes -- you can get the second parent of the previous reference (assuming it was a merge commit) by using `HEAD~3^2`, and so on.
275
280
276
281
[[_commit_ranges]]
277
282
==== Commit Ranges
278
283
279
284
Now that you can specify individual commits, let’s see how to specify ranges of commits.
280
-
This is particularly useful for managing your branches – if you have a lot of branches, you can use range specifications to answer questions such as, ``What work is on this branch that I haven’t yet merged into my main branch?''
285
+
This is particularly useful for managing your branches -- if you have a lot of branches, you can use range specifications to answer questions such as, ``What work is on this branch that I haven’t yet merged into my main branch?''
281
286
282
287
===== Double Dot
283
288
@@ -289,8 +294,8 @@ For example, say you have a commit history that looks like <<double_dot>>.
289
294
.Example history for range selection.
290
295
image::images/double-dot.png[Example history for range selection.]
291
296
292
-
You want to see what is in your experiment branch that hasn’t yet been merged into your master branch.
293
-
You can ask Git to show you a log of just those commits with `master..experiment` – that means ``all commits reachable by experiment that aren’t reachable by master.''
297
+
Say you want to see what is in your `experiment` branch that hasn’t yet been merged into your `master` branch.
298
+
You can ask Git to show you a log of just those commits with `master..experiment` -- that means ``all commits reachable from experiment that aren’t reachable from master.''
294
299
For the sake of brevity and clarity in these examples, the letters of the commit objects from the diagram are used in place of the actual log output in the order that they would display:
295
300
296
301
[source,console]
@@ -300,7 +305,7 @@ D
300
305
C
301
306
----
302
307
303
-
If, on the other hand, you want to see the opposite – all commits in `master` that aren’t in `experiment` – you can reverse the branch names.
308
+
If, on the other hand, you want to see the opposite -- all commits in `master` that aren’t in `experiment` -- you can reverse the branch names.
304
309
`experiment..master` shows you everything in `master` not reachable from `experiment`:
305
310
306
311
[source,console]
@@ -310,8 +315,8 @@ F
310
315
E
311
316
----
312
317
313
-
This is useful if you want to keep the `experiment` branch up to date and preview what you’re about to merge in.
314
-
Another very frequent use of this syntax is to see what you’re about to push to a remote:
318
+
This is useful if you want to keep the `experiment` branch up to date and preview what you’re about to merge.
319
+
Another frequent use of this syntax is to see what you’re about to push to a remote:
This command shows you any commits in your current branch that aren’t in the `master` branch on your `origin` remote.
322
327
If you run a `git push` and your current branch is tracking `origin/master`, the commits listed by `git log origin/master..HEAD` are the commits that will be transferred to the server.
323
-
You can also leave off one side of the syntax to have Git assume HEAD.
324
-
For example, you can get the same results as in the previous example by typing `git log origin/master..` – Git substitutes HEAD if one side is missing.
328
+
You can also leave off one side of the syntax to have Git assume `HEAD`.
329
+
For example, you can get the same results as in the previous example by typing `git log origin/master..` -- Git substitutes `HEAD` if one side is missing.
325
330
326
331
===== Multiple Points
327
332
328
-
The double-dot syntax is useful as a shorthand; but perhaps you want to specify more than two branches to indicate your revision, such as seeing what commits are in any of several branches that aren’t in the branch you’re currently on.
333
+
The double-dot syntax is useful as a shorthand, but perhaps you want to specify more than two branches to indicate your revision, such as seeing what commits are in any of several branches that aren’t in the branch you’re currently on.
329
334
Git allows you to do this by using either the `^` character or `--not` before any reference from which you don’t want to see reachable commits.
330
-
Thus these three commands are equivalent:
335
+
Thus, the following three commands are equivalent:
331
336
332
337
[source,console]
333
338
----
@@ -337,7 +342,7 @@ $ git log refB --not refA
337
342
----
338
343
339
344
This is nice because with this syntax you can specify more than two references in your query, which you cannot do with the double-dot syntax.
340
-
For instance, if you want to see all commits that are reachable from `refA` or `refB` but not from `refC`, you can type one of these:
345
+
For instance, if you want to see all commits that are reachable from `refA` or `refB` but not from `refC`, you can use either of:
341
346
342
347
[source,console]
343
348
----
@@ -350,9 +355,9 @@ This makes for a very powerful revision query system that should help you figure
350
355
[[_triple_dot]]
351
356
===== Triple Dot
352
357
353
-
The last major range-selection syntax is the triple-dot syntax, which specifies all the commits that are reachable by either of two references but not by both of them.
358
+
The last major range-selection syntax is the triple-dot syntax, which specifies all the commits that are reachable by _either_ of two references but not by both of them.
354
359
Look back at the example commit history in <<double_dot>>.
355
-
If you want to see what is in `master` or `experiment` but not any common references, you can run
360
+
If you want to see what is in `master` or `experiment` but not any common references, you can run:
356
361
357
362
[source,console]
358
363
----
@@ -366,7 +371,7 @@ C
366
371
Again, this gives you normal `log` output but shows you only the commit information for those four commits, appearing in the traditional commit date ordering.
367
372
368
373
A common switch to use with the `log` command in this case is `--left-right`, which shows you which side of the range each commit is in.
0 commit comments