UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

279 lines (192 loc) 18.6 kB
<?xml version='1.0' encoding='utf-8'?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Pro Git - professional version control</title> <meta content="http://www.w3.org/1999/xhtml; charset=utf-8" http-equiv="Content-Type"/> <link href="stylesheet.css" type="text/css" rel="stylesheet"/> <style type="text/css"> @page { margin-bottom: 5.000000pt; margin-top: 5.000000pt; }</style> </head> <body class="calibre"> <h2 class="calibre4" id="calibre_pb_49">Revision Selection</h2> <p class="calibre3">Git allows you to specify specific commits or a range of commits in several ways. They aren't necessarily obvious but are helpful to know.</p> <h3 class="calibre5">Single Revisions</h3> <p class="calibre3">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. This section outlines the various ways you can refer to a single commit.</p> <h3 class="calibre5">Short SHA</h3> <p class="calibre3">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.</p> <p class="calibre3">For example, to see a specific commit, suppose you run a <code class="calibre10">git log</code> command and identify the commit where you added certain functionality:</p> <pre class="calibre9"><code class="calibre10">$ git log commit 734713bc047d87bf7eac9674765ae793478c50d3 Author: Scott Chacon &lt;schacon@gmail.com&gt; Date: Fri Jan 2 18:32:33 2009 -0800 fixed refs handling, added gc auto, updated tests commit d921970aadf03b3cf0e71becdaab3147ba71cdef Merge: 1c002dd... 35cfb2b... Author: Scott Chacon &lt;schacon@gmail.com&gt; Date: Thu Dec 11 15:08:43 2008 -0800 Merge commit 'phedders/rdocs' commit 1c002dd4b536e7479fe34593e72e6c6c1819e53b Author: Scott Chacon &lt;schacon@gmail.com&gt; Date: Thu Dec 11 14:58:32 2008 -0800 added some blame and merge stuff </code></pre> <p class="calibre3">In this case, choose <code class="calibre10">1c002dd....</code> If you <code class="calibre10">git show</code> that commit, the following commands are equivalent (assuming the shorter versions are unambiguous):</p> <pre class="calibre9"><code class="calibre10">$ git show 1c002dd4b536e7479fe34593e72e6c6c1819e53b $ git show 1c002dd4b536e7479f $ git show 1c002d </code></pre> <p class="calibre3">Git can figure out a short, unique abbreviation for your SHA-1 values. If you pass <code class="calibre10">--abbrev-commit</code> to the <code class="calibre10">git log</code> command, the output will use shorter values but keep them unique; it defaults to using seven characters but makes them longer if necessary to keep the SHA-1 unambiguous:</p> <pre class="calibre9"><code class="calibre10">$ git log --abbrev-commit --pretty=oneline ca82a6d changed the version number 085bb3b removed unnecessary test code a11bef0 first commit </code></pre> <p class="calibre3">Generally, eight to ten characters are more than enough to be unique within a project. One of the largest Git projects, the Linux kernel, is beginning to need 12 characters out of the possible 40 to stay unique.</p> <h3 class="calibre5">A SHORT NOTE ABOUT SHA-1</h3> <p class="calibre3">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. What then?</p> <p class="calibre3">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. If you try to check out that object again at some point, you'll always get the data of the first object. </p> <p class="calibre3">However, you should be aware of how ridiculously unlikely this scenario is. The SHA-1 digest is 20 bytes or 160 bits. The number of randomly hashed objects needed to ensure a 50% probability of a single collision is about 2^80 (the formula for determining collision probability is <code class="calibre10">p = (n(n-1)/2) * (1/2^160))</code>. 2^80 is 1.2 x 10^24 or 1 million billion billion. That's 1,200 times the number of grains of sand on the earth.</p> <p class="calibre3">Here's an example to give you an idea of what it would take to get a SHA-1 collision. If all 6.5 billion humans on Earth were programming, and every second, each one was producing code that was the equivalent of the entire Linux kernel history (1 million Git objects) and pushing it into one enormous Git repository, it would take 5 years until that repository contained enough objects to have a 50% probability of a single SHA-1 object collision. A higher probability exists that every member of your programming team will be attacked and killed by wolves in unrelated incidents on the same night.</p> <h3 class="calibre5">Branch References</h3> <p class="calibre3">The most straightforward way to specify a commit requires that it have a branch reference pointed at it. Then, you can use a branch name in any Git command that expects a commit object or SHA-1 value. For instance, if you want to show the last commit object on a branch, the following commands are equivalent, assuming that the <code class="calibre10">topic1</code> branch points to <code class="calibre10">ca82a6d</code>:</p> <pre class="calibre9"><code class="calibre10">$ git show ca82a6dff817ec66f44342007202690a93763949 $ git show topic1 </code></pre> <p class="calibre3">If you want to see which specific SHA a branch points to, or if you want to see what any of these examples boils down to in terms of SHAs, you can use a Git plumbing tool called <code class="calibre10">rev-parse</code>. You can see Chapter 9 for more information about plumbing tools; basically, <code class="calibre10">rev-parse</code> exists for lower-level operations and isn't designed to be used in day-to-day operations. However, it can be helpful sometimes when you need to see what's really going on. Here you can run <code class="calibre10">rev-parse</code> on your branch.</p> <pre class="calibre9"><code class="calibre10">$ git rev-parse topic1 ca82a6dff817ec66f44342007202690a93763949 </code></pre> <h3 class="calibre5">RefLog Shortnames</h3> <p class="calibre3">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.</p> <p class="calibre3">You can see your reflog by using <code class="calibre10">git reflog</code>:</p> <pre class="calibre9"><code class="calibre10">$ git reflog 734713b... HEAD@{0}: commit: fixed refs handling, added gc auto, updated d921970... HEAD@{1}: merge phedders/rdocs: Merge made by recursive. 1c002dd... HEAD@{2}: commit: added some blame and merge stuff 1c36188... HEAD@{3}: rebase -i (squash): updating HEAD 95df984... HEAD@{4}: commit: # This is a combination of two commits. 1c36188... HEAD@{5}: rebase -i (squash): updating HEAD 7e05da5... HEAD@{6}: rebase -i (pick): updating HEAD </code></pre> <p class="calibre3">Every time your branch tip is updated for any reason, Git stores that information for you in this temporary history. And you can specify older commits with this data, as well. If you want to see the fifth prior value of the HEAD of your repository, you can use the <code class="calibre10">@{n}</code> reference that you see in the reflog output:</p> <pre class="calibre9"><code class="calibre10">$ git show HEAD@{5} </code></pre> <p class="calibre3">You can also use this syntax to see where a branch was some specific amount of time ago. For instance, to see where your <code class="calibre10">master</code> branch was yesterday, you can type</p> <pre class="calibre9"><code class="calibre10">$ git show master@{yesterday} </code></pre> <p class="calibre3">That shows you where the branch tip was yesterday. 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.</p> <p class="calibre3">To see reflog information formatted like the <code class="calibre10">git log</code> output, you can run <code class="calibre10">git log -g</code>:</p> <pre class="calibre9"><code class="calibre10">$ git log -g master commit 734713bc047d87bf7eac9674765ae793478c50d3 Reflog: master@{0} (Scott Chacon &lt;schacon@gmail.com&gt;) Reflog message: commit: fixed refs handling, added gc auto, updated Author: Scott Chacon &lt;schacon@gmail.com&gt; Date: Fri Jan 2 18:32:33 2009 -0800 fixed refs handling, added gc auto, updated tests commit d921970aadf03b3cf0e71becdaab3147ba71cdef Reflog: master@{1} (Scott Chacon &lt;schacon@gmail.com&gt;) Reflog message: merge phedders/rdocs: Merge made by recursive. Author: Scott Chacon &lt;schacon@gmail.com&gt; Date: Thu Dec 11 15:08:43 2008 -0800 Merge commit 'phedders/rdocs' </code></pre> <p class="calibre3">It's important to note that the reflog information is strictly local - it's a log of what you've done in your repository. 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. Running <code class="calibre10">git show HEAD@{2.months.ago}</code> 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.</p> <h3 class="calibre5">Ancestry References</h3> <p class="calibre3">The other main way to specify a commit is via its ancestry. If you place a <code class="calibre10">^</code> at the end of a reference, Git resolves it to mean the parent of that commit. Suppose you look at the history of your project:</p> <pre class="calibre9"><code class="calibre10">$ git log --pretty=format:'%h %s' --graph * 734713b fixed refs handling, added gc auto, updated tests * d921970 Merge commit 'phedders/rdocs' |\ | * 35cfb2b Some rdoc changes * | 1c002dd added some blame and merge stuff |/ * 1c36188 ignore *.gem * 9b29157 add open3_detach to gemspec file list </code></pre> <p class="calibre3">Then, you can see the previous commit by specifying <code class="calibre10">HEAD^</code>, which means "the parent of HEAD":</p> <pre class="calibre9"><code class="calibre10">$ git show HEAD^ commit d921970aadf03b3cf0e71becdaab3147ba71cdef Merge: 1c002dd... 35cfb2b... Author: Scott Chacon &lt;schacon@gmail.com&gt; Date: Thu Dec 11 15:08:43 2008 -0800 Merge commit 'phedders/rdocs' </code></pre> <p class="calibre3">You can also specify a number after the <code class="calibre10">^</code> - for example, <code class="calibre10">d921970^2</code> means "the second parent of d921970." This syntax is only useful for merge commits, which have more than one parent. 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:</p> <pre class="calibre9"><code class="calibre10">$ git show d921970^ commit 1c002dd4b536e7479fe34593e72e6c6c1819e53b Author: Scott Chacon &lt;schacon@gmail.com&gt; Date: Thu Dec 11 14:58:32 2008 -0800 added some blame and merge stuff $ git show d921970^2 commit 35cfb2b795a55793d7cc56a6cc2060b4bb732548 Author: Paul Hedderly &lt;paul+git@mjr.org&gt; Date: Wed Dec 10 22:22:03 2008 +0000 Some rdoc changes </code></pre> <p class="calibre3">The other main ancestry specification is the <code class="calibre10">~</code>. This also refers to the first parent, so <code class="calibre10">HEAD~</code> and <code class="calibre10">HEAD^</code> are equivalent. The difference becomes apparent when you specify a number. <code class="calibre10">HEAD~2</code> means "the first parent of the first parent," or "the grandparent" - it traverses the first parents the number of times you specify. For example, in the history listed earlier, <code class="calibre10">HEAD~3</code> would be</p> <pre class="calibre9"><code class="calibre10">$ git show HEAD~3 commit 1c3618887afb5fbcbea25b7c013f4e2114448b8d Author: Tom Preston-Werner &lt;tom@mojombo.com&gt; Date: Fri Nov 7 13:47:59 2008 -0500 ignore *.gem </code></pre> <p class="calibre3">This can also be written <code class="calibre10">HEAD^^^</code>, which again is the first parent of the first parent of the first parent:</p> <pre class="calibre9"><code class="calibre10">$ git show HEAD^^^ commit 1c3618887afb5fbcbea25b7c013f4e2114448b8d Author: Tom Preston-Werner &lt;tom@mojombo.com&gt; Date: Fri Nov 7 13:47:59 2008 -0500 ignore *.gem </code></pre> <p class="calibre3">You can also combine these syntaxes - you can get the second parent of the previous reference (assuming it was a merge commit) by using <code class="calibre10">HEAD~3^2</code>, and so on.</p> <h3 class="calibre5">Commit Ranges</h3> <p class="calibre3">Now that you can specify individual commits, let's see how to specify ranges of commits. 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?"</p> <h4 class="calibre14">Double Dot</h4> <p class="calibre3">The most common range specification is the double-dot syntax. This basically asks Git to resolve a range of commits that are reachable from one commit but aren't reachable from another. For example, say you have a commit history that looks like Figure 6-1.</p> <p class="calibre3"><img src="18333fig0601-tn.png" alt="Figure 6-1. Example history for range selection." title="Figure 6-1. Example history for range selection." class="calibre6"/></p> <p class="calibre3">You want to see what is in your experiment branch that hasn't yet been merged into your master branch. You can ask Git to show you a log of just those commits with <code class="calibre10">master..experiment</code> - that means "all commits reachable by experiment that aren't reachable by master." For the sake of brevity and clarity in these examples, I'll use the letters of the commit objects from the diagram in place of the actual log output in the order that they would display:</p> <pre class="calibre9"><code class="calibre10">$ git log master..experiment D C </code></pre> <p class="calibre3">If, on the other hand, you want to see the opposite - all commits in <code class="calibre10">master</code> that aren't in <code class="calibre10">experiment</code> - you can reverse the branch names. <code class="calibre10">experiment..master</code> shows you everything in <code class="calibre10">master</code> not reachable from <code class="calibre10">experiment</code>:</p> <pre class="calibre9"><code class="calibre10">$ git log experiment..master F E </code></pre> <p class="calibre3">This is useful if you want to keep the <code class="calibre10">experiment</code> branch up to date and preview what you're about to merge in. Another very frequent use of this syntax is to see what you're about to push to a remote:</p> <pre class="calibre9"><code class="calibre10">$ git log origin/master..HEAD </code></pre> <p class="calibre3">This command shows you any commits in your current branch that aren't in the <code class="calibre10">master</code> branch on your <code class="calibre10">origin</code> remote. If you run a <code class="calibre10">git push</code> and your current branch is tracking <code class="calibre10">origin/master</code>, the commits listed by <code class="calibre10">git log origin/master..HEAD</code> are the commits that will be transferred to the server. You can also leave off one side of the syntax to have Git assume HEAD. For example, you can get the same results as in the previous example by typing <code class="calibre10">git log origin/master..</code> - Git substitutes HEAD if one side is missing.</p> <h4 class="calibre14">Multiple Points</h4> <p class="calibre3">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. Git allows you to do this by using either the <code class="calibre10">^</code> character or <code class="calibre10">--not</code> before any reference from which you don't want to see reachable commits. Thus these three commands are equivalent:</p> <pre class="calibre9"><code class="calibre10">$ git log refA..refB $ git log ^refA refB $ git log refB --not refA </code></pre> <p class="calibre3">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. For instance, if you want to see all commits that are reachable from <code class="calibre10">refA</code> or <code class="calibre10">refB</code> but not from <code class="calibre10">refC</code>, you can type one of these:</p> <pre class="calibre9"><code class="calibre10">$ git log refA refB ^refC $ git log refA refB --not refC </code></pre> <p class="calibre3">This makes for a very powerful revision query system that should help you figure out what is in your branches.</p> <h4 class="calibre14">Triple Dot</h4> <p class="calibre3">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. Look back at the example commit history in Figure 6-1. If you want to see what is in <code class="calibre10">master</code> or <code class="calibre10">experiment</code> but not any common references, you can run</p> <pre class="calibre9"><code class="calibre10">$ git log master...experiment F E D C </code></pre> <p class="calibre3">Again, this gives you normal <code class="calibre10">log</code> output but shows you only the commit information for those four commits, appearing in the traditional commit date ordering.</p> <p class="calibre3">A common switch to use with the <code class="calibre10">log</code> command in this case is <code class="calibre10">--left-right</code>, which shows you which side of the range each commit is in. This helps make the data more useful:</p> <pre class="calibre9"><code class="calibre10">$ git log --left-right master...experiment &lt; F &lt; E &gt; D &gt; C </code></pre> <p class="calibre3">With these tools, you can much more easily let Git know what commit or commits you want to inspect. </p> </body> </html>