CINXE.COM

Git - Revision Selection

<!DOCTYPE html> <html lang="it"> <head> <meta charset='utf-8'> <meta content='IE=edge,chrome=1' http-equiv='X-UA-Compatible'> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Git - Revision Selection</title> <link href="/favicon.ico" rel='shortcut icon' type='image/x-icon'> <link rel="stylesheet" href="/application.min.css"> <script src="/js/modernizr.js"></script> <script src="/js/modernize.js"></script> </head> <body id="documentation"> <div class="inner"> <header> <a href="/"><img src="/images/logo@2x.png" width="110" height="46" alt="Git" /></a> <span id="tagline"></span> <script type="text/javascript"> const taglines = [ "fast-version-control", "everything-is-local", "distributed-even-if-your-workflow-isnt", "local-branching-on-the-cheap", "distributed-is-the-new-centralized" ]; var tagline = taglines[Math.floor(Math.random() * taglines.length)]; document.getElementById('tagline').innerHTML = '--' + tagline; </script> <form id="search" action="/search/results"> <input id="search-text" name="search" placeholder="Type / to search entire site…" autocomplete="off" type="text" /> </form> <div id="search-results"></div> </header> </div> <div class="inner"> <div id="content-wrapper"> <div tabindex="1" class="sidebar-btn"></div> <aside class="sidebar" id="sidebar"> <nav> <ul> <li> <a href="/about">About</a> <ul> </ul> </li> <li> <a href="/doc" class="active">Documentation</a> <ul class="expanded"> <li> <a href="/docs">Reference</a> </li> <li> <a href="/book" class="active">Book</a> </li> <li> <a href="/videos">Videos</a> </li> <li> <a href="/doc/ext">External Links</a> </li> </ul> </li> <li> <a href="/downloads">Downloads</a> <ul > <li> <a href="/downloads/guis">GUI Clients</a> </li> <li> <a href="/downloads/logos">Logos</a> </li> </ul> </li> <li> <a href="/community">Community</a> </li> </ul> <hr class="sidebar"> <p> This book is available in <a href="/book/en/v2/Git-Tools-Revision-Selection">English</a>. </p> <p> Full translation available in <table> <tr><td><a href="/book/az/v2/Git-Al%c9%99tl%c9%99ri-Reviziya-Se%c3%a7imi">azərbaycan dili</a>,</td></tr> <tr><td><a href="/book/bg/v2/Git-%d0%b8-%d0%b4%d1%80%d1%83%d0%b3%d0%b8-%d1%81%d0%b8%d1%81%d1%82%d0%b5%d0%bc%d0%b8-Git-%d0%ba%d0%b0%d1%82%d0%be-%d0%ba%d0%bb%d0%b8%d0%b5%d0%bd%d1%82">български език</a>,</td></tr> <tr><td><a href="/book/de/v2/Git-Tools-Revisions-Auswahl">Deutsch</a>,</td></tr> <tr><td><a href="/book/es/v2/Herramientas-de-Git-Revisi%c3%b3n-por-selecci%c3%b3n">Español</a>,</td></tr> <tr><td><a href="/book/fr/v2/Utilitaires-Git-S%c3%a9lection-des-versions">Français</a>,</td></tr> <tr><td><a href="/book/gr">Ελληνικά</a>,</td></tr> <tr><td><a href="/book/ja/v2/Git-%e3%81%ae%e3%81%95%e3%81%be%e3%81%96%e3%81%be%e3%81%aa%e3%83%84%e3%83%bc%e3%83%ab-%e3%83%aa%e3%83%93%e3%82%b8%e3%83%a7%e3%83%b3%e3%81%ae%e9%81%b8%e6%8a%9e">日本語</a>,</td></tr> <tr><td><a href="/book/ko/v2/Git-%eb%8f%84%ea%b5%ac-%eb%a6%ac%eb%b9%84%ec%a0%84-%ec%a1%b0%ed%9a%8c%ed%95%98%ea%b8%b0">한국어</a>,</td></tr> <tr><td><a href="/book/nl/v2/Git-Tools-Revisie-Selectie">Nederlands</a>,</td></tr> <tr><td><a href="/book/ru/v2/%d0%98%d0%bd%d1%81%d1%82%d1%80%d1%83%d0%bc%d0%b5%d0%bd%d1%82%d1%8b-Git-%d0%92%d1%8b%d0%b1%d0%be%d1%80-%d1%80%d0%b5%d0%b2%d0%b8%d0%b7%d0%b8%d0%b8">Русский</a>,</td></tr> <tr><td><a href="/book/sl/v2/Orodja-Git-Izbira-revizije">Slovenščina</a>,</td></tr> <tr><td><a href="/book/tl/v2/Mga-Git-na-Kasangkapan-Pagpipili-ng-Rebisyon">Tagalog</a>,</td></tr> <tr><td><a href="/book/uk/v2/%d0%86%d0%bd%d1%81%d1%82%d1%80%d1%83%d0%bc%d0%b5%d0%bd%d1%82%d0%b8-Git-%d0%92%d0%b8%d0%b1%d1%96%d1%80-%d1%80%d0%b5%d0%b2%d1%96%d0%b7%d1%96%d0%b9">Українська</a></td></tr> <tr><td><a href="/book/zh/v2/Git-%e5%b7%a5%e5%85%b7-%e9%80%89%e6%8b%a9%e4%bf%ae%e8%ae%a2%e7%89%88%e6%9c%ac">简体中文</a>,</td></tr> </table> </p> <p> Partial translations available in <table> <tr><td><a href="/book/cs/v2/Git-Tools-Revision-Selection">Čeština</a>,</td></tr> <tr><td><a href="/book/mk/v2/Git-%d0%90%d0%bb%d0%b0%d1%82%d0%ba%d0%b8-Revision-Selection">Македонски</a>,</td></tr> <tr><td><a href="/book/pl/v2/Narz%c4%99dzia-Gita-Wskazywanie-rewizji">Polski</a>,</td></tr> <tr><td><a href="/book/sr/v2/%d0%93%d0%b8%d1%82-%d0%b0%d0%bb%d0%b0%d1%82%d0%b8-%d0%98%d0%b7%d0%b1%d0%be%d1%80-%d1%80%d0%b5%d0%b2%d0%b8%d0%b7%d0%b8%d1%98%d0%b0">Српски</a>,</td></tr> <tr><td><a href="/book/uz/v2/Git-Tools-Revision-Selection">Ўзбекча</a>,</td></tr> <tr><td><a href="/book/zh-tw/v2/Git-%e5%b7%a5%e5%85%b7-Revision-Selection">繁體中文</a>,</td></tr> </table> </p> <p> Translations started for <table> <tr><td><a href="/book/be/v2/Git-Tools-Revision-Selection">Беларуская</a>,</td></tr> <tr><td><a href="/book/fa/v2/Git-Tools-Revision-Selection" dir="rtl">فارسی</a>,</td></tr> <tr><td><a href="/book/id/v2/Git-Tools-Revision-Selection">Indonesian</a>,</td></tr> <tr><td><a href="/book/it/v2/Git-Tools-Revision-Selection">Italiano</a>,</td></tr> <tr><td><a href="/book/ms/v2/Git-Tools-Revision-Selection">Bahasa Melayu</a>,</td></tr> <tr><td><a href="/book/pt-br/v2/Git-Tools-Revision-Selection">Português (Brasil)</a>,</td></tr> <tr><td><a href="/book/pt-pt/v2/Ferramentas-do-Git-Revision-Selection">Português (Portugal)</a>,</td></tr> <tr><td><a href="/book/sv/v2/Git-Tools-Revision-Selection">Svenska</a>,</td></tr> <tr><td><a href="/book/tr/v2/Git-Ara%c3%a7lar%c4%b1-D%c3%bczeltme-Se%c3%a7imi">Türkçe</a>.</td></tr> </table> </p> <hr class="sidebar"/> <p> The source of this book is <a href="https://github.com/progit/progit2-it">hosted on GitHub.</a></br> Patches, suggestions and comments are welcome. </p> </nav> </aside> <div id="content"> <div id="book-chapters"> <a class="dropdown-trigger" id="book-chapters-trigger" data-panel-id="chapters-dropdown" href="#">Chapters ▾</a> <div class='dropdown-panel' id='chapters-dropdown'> <div class='three-column'> <div class="column-left"> <ol class='book-toc'> <li class='chapter'> <h2>1. <a href="/book/it/v2/Per-Iniziare-Il-Controllo-di-Versione">Per Iniziare</a></h2> <ol> <li> 1.1 <a href="/book/it/v2/Per-Iniziare-Il-Controllo-di-Versione">Il Controllo di Versione</a> </li> <li> 1.2 <a href="/book/it/v2/Per-Iniziare-Una-Breve-Storia-di-Git">Una Breve Storia di Git</a> </li> <li> 1.3 <a href="/book/it/v2/Per-Iniziare-Cos%e2%80%99%c3%a9-Git%3F">Cos’é Git?</a> </li> <li> 1.4 <a href="/book/it/v2/Per-Iniziare-La-riga-di-comando">La riga di comando</a> </li> <li> 1.5 <a href="/book/it/v2/Per-Iniziare-Installing-Git">Installing Git</a> </li> <li> 1.6 <a href="/book/it/v2/Per-Iniziare-First-Time-Git-Setup">First-Time Git Setup</a> </li> <li> 1.7 <a href="/book/it/v2/Per-Iniziare-Chiedere-aiuto">Chiedere aiuto</a> </li> <li> 1.8 <a href="/book/it/v2/Per-Iniziare-Sommario">Sommario</a> </li> </ol> </li> <li class='chapter'> <h2>2. <a href="/book/it/v2/Git-Basics-Getting-a-Git-Repository">Git Basics</a></h2> <ol> <li> 2.1 <a href="/book/it/v2/Git-Basics-Getting-a-Git-Repository">Getting a Git Repository</a> </li> <li> 2.2 <a href="/book/it/v2/Git-Basics-Recording-Changes-to-the-Repository">Recording Changes to the Repository</a> </li> <li> 2.3 <a href="/book/it/v2/Git-Basics-Viewing-the-Commit-History">Viewing the Commit History</a> </li> <li> 2.4 <a href="/book/it/v2/Git-Basics-Undoing-Things">Undoing Things</a> </li> <li> 2.5 <a href="/book/it/v2/Git-Basics-Working-with-Remotes">Working with Remotes</a> </li> <li> 2.6 <a href="/book/it/v2/Git-Basics-Tagging">Tagging</a> </li> <li> 2.7 <a href="/book/it/v2/Git-Basics-Git-Aliases">Git Aliases</a> </li> <li> 2.8 <a href="/book/it/v2/Git-Basics-Sommario">Sommario</a> </li> </ol> </li> <li class='chapter'> <h2>3. <a href="/book/it/v2/Git-Branching-Branches-in-a-Nutshell">Git Branching</a></h2> <ol> <li> 3.1 <a href="/book/it/v2/Git-Branching-Branches-in-a-Nutshell">Branches in a Nutshell</a> </li> <li> 3.2 <a href="/book/it/v2/Git-Branching-Basic-Branching-and-Merging">Basic Branching and Merging</a> </li> <li> 3.3 <a href="/book/it/v2/Git-Branching-Branch-Management">Branch Management</a> </li> <li> 3.4 <a href="/book/it/v2/Git-Branching-Branching-Workflows">Branching Workflows</a> </li> <li> 3.5 <a href="/book/it/v2/Git-Branching-Remote-Branches">Remote Branches</a> </li> <li> 3.6 <a href="/book/it/v2/Git-Branching-Rebasing">Rebasing</a> </li> <li> 3.7 <a href="/book/it/v2/Git-Branching-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>4. <a href="/book/it/v2/Git-on-the-Server-The-Protocols">Git on the Server</a></h2> <ol> <li> 4.1 <a href="/book/it/v2/Git-on-the-Server-The-Protocols">The Protocols</a> </li> <li> 4.2 <a href="/book/it/v2/Git-on-the-Server-Getting-Git-on-a-Server">Getting Git on a Server</a> </li> <li> 4.3 <a href="/book/it/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key">Generating Your SSH Public Key</a> </li> <li> 4.4 <a href="/book/it/v2/Git-on-the-Server-Setting-Up-the-Server">Setting Up the Server</a> </li> <li> 4.5 <a href="/book/it/v2/Git-on-the-Server-Git-Daemon">Git Daemon</a> </li> <li> 4.6 <a href="/book/it/v2/Git-on-the-Server-Smart-HTTP">Smart HTTP</a> </li> <li> 4.7 <a href="/book/it/v2/Git-on-the-Server-GitWeb">GitWeb</a> </li> <li> 4.8 <a href="/book/it/v2/Git-on-the-Server-GitLab">GitLab</a> </li> <li> 4.9 <a href="/book/it/v2/Git-on-the-Server-Third-Party-Hosted-Options">Third Party Hosted Options</a> </li> <li> 4.10 <a href="/book/it/v2/Git-on-the-Server-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>5. <a href="/book/it/v2/Distributed-Git-Distributed-Workflows">Distributed Git</a></h2> <ol> <li> 5.1 <a href="/book/it/v2/Distributed-Git-Distributed-Workflows">Distributed Workflows</a> </li> <li> 5.2 <a href="/book/it/v2/Distributed-Git-Contributing-to-a-Project">Contributing to a Project</a> </li> <li> 5.3 <a href="/book/it/v2/Distributed-Git-Maintaining-a-Project">Maintaining a Project</a> </li> <li> 5.4 <a href="/book/it/v2/Distributed-Git-Summary">Summary</a> </li> </ol> </li> </ol> </div> <div class='column-middle'> <ol class='book-toc'> <li class='chapter'> <h2>6. <a href="/book/it/v2/GitHub-Account-Setup-and-Configuration">GitHub</a></h2> <ol> <li> 6.1 <a href="/book/it/v2/GitHub-Account-Setup-and-Configuration">Account Setup and Configuration</a> </li> <li> 6.2 <a href="/book/it/v2/GitHub-Contributing-to-a-Project">Contributing to a Project</a> </li> <li> 6.3 <a href="/book/it/v2/GitHub-Maintaining-a-Project">Maintaining a Project</a> </li> <li> 6.4 <a href="/book/it/v2/GitHub-Managing-an-organization">Managing an organization</a> </li> <li> 6.5 <a href="/book/it/v2/GitHub-Scripting-GitHub">Scripting GitHub</a> </li> <li> 6.6 <a href="/book/it/v2/GitHub-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>7. <a href="/book/it/v2/Git-Tools-Revision-Selection">Git Tools</a></h2> <ol> <li> 7.1 <a href="/book/it/v2/Git-Tools-Revision-Selection" class="active">Revision Selection</a> </li> <li> 7.2 <a href="/book/it/v2/Git-Tools-Interactive-Staging">Interactive Staging</a> </li> <li> 7.3 <a href="/book/it/v2/Git-Tools-Stashing-and-Cleaning">Stashing and Cleaning</a> </li> <li> 7.4 <a href="/book/it/v2/Git-Tools-Signing-Your-Work">Signing Your Work</a> </li> <li> 7.5 <a href="/book/it/v2/Git-Tools-Searching">Searching</a> </li> <li> 7.6 <a href="/book/it/v2/Git-Tools-Rewriting-History">Rewriting History</a> </li> <li> 7.7 <a href="/book/it/v2/Git-Tools-Reset-Demystified">Reset Demystified</a> </li> <li> 7.8 <a href="/book/it/v2/Git-Tools-Advanced-Merging">Advanced Merging</a> </li> <li> 7.9 <a href="/book/it/v2/Git-Tools-Rerere">Rerere</a> </li> <li> 7.10 <a href="/book/it/v2/Git-Tools-Debugging-with-Git">Debugging with Git</a> </li> <li> 7.11 <a href="/book/it/v2/Git-Tools-Submodules">Submodules</a> </li> <li> 7.12 <a href="/book/it/v2/Git-Tools-Bundling">Bundling</a> </li> <li> 7.13 <a href="/book/it/v2/Git-Tools-Replace">Replace</a> </li> <li> 7.14 <a href="/book/it/v2/Git-Tools-Credential-Storage">Credential Storage</a> </li> <li> 7.15 <a href="/book/it/v2/Git-Tools-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>8. <a href="/book/it/v2/Customizing-Git-Git-Configuration">Customizing Git</a></h2> <ol> <li> 8.1 <a href="/book/it/v2/Customizing-Git-Git-Configuration">Git Configuration</a> </li> <li> 8.2 <a href="/book/it/v2/Customizing-Git-Git-Attributes">Git Attributes</a> </li> <li> 8.3 <a href="/book/it/v2/Customizing-Git-Git-Hooks">Git Hooks</a> </li> <li> 8.4 <a href="/book/it/v2/Customizing-Git-An-Example-Git-Enforced-Policy">An Example Git-Enforced Policy</a> </li> <li> 8.5 <a href="/book/it/v2/Customizing-Git-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>9. <a href="/book/it/v2/Git-and-Other-Systems-Git-as-a-Client">Git and Other Systems</a></h2> <ol> <li> 9.1 <a href="/book/it/v2/Git-and-Other-Systems-Git-as-a-Client">Git as a Client</a> </li> <li> 9.2 <a href="/book/it/v2/Git-and-Other-Systems-Migrating-to-Git">Migrating to Git</a> </li> <li> 9.3 <a href="/book/it/v2/Git-and-Other-Systems-Summary">Summary</a> </li> </ol> </li> <li class='chapter'> <h2>10. <a href="/book/it/v2/Git-Internals-Plumbing-and-Porcelain">Git Internals</a></h2> <ol> <li> 10.1 <a href="/book/it/v2/Git-Internals-Plumbing-and-Porcelain">Plumbing and Porcelain</a> </li> <li> 10.2 <a href="/book/it/v2/Git-Internals-Git-Objects">Git Objects</a> </li> <li> 10.3 <a href="/book/it/v2/Git-Internals-Git-References">Git References</a> </li> <li> 10.4 <a href="/book/it/v2/Git-Internals-Packfiles">Packfiles</a> </li> <li> 10.5 <a href="/book/it/v2/Git-Internals-The-Refspec">The Refspec</a> </li> <li> 10.6 <a href="/book/it/v2/Git-Internals-Transfer-Protocols">Transfer Protocols</a> </li> <li> 10.7 <a href="/book/it/v2/Git-Internals-Maintenance-and-Data-Recovery">Maintenance and Data Recovery</a> </li> <li> 10.8 <a href="/book/it/v2/Git-Internals-Environment-Variables">Environment Variables</a> </li> <li> 10.9 <a href="/book/it/v2/Git-Internals-Summary">Summary</a> </li> </ol> </li> </ol> </div> <div class='column-right'> <ol class='book-toc'> <li class='chapter'> <h2>A1. <a href="/book/it/v2/Appendice-A:-Git-in-altri-contesti-Graphical-Interfaces">Appendice A: Git in altri contesti</a></h2> <ol> <li> A1.1 <a href="/book/it/v2/Appendice-A:-Git-in-altri-contesti-Graphical-Interfaces">Graphical Interfaces</a> </li> <li> A1.2 <a href="/book/it/v2/Appendice-A:-Git-in-altri-contesti-Git-in-Visual-Studio">Git in Visual Studio</a> </li> <li> A1.3 <a href="/book/it/v2/Appendice-A:-Git-in-altri-contesti-Git-in-Eclipse">Git in Eclipse</a> </li> <li> A1.4 <a href="/book/it/v2/Appendice-A:-Git-in-altri-contesti-Git-in-Bash">Git in Bash</a> </li> <li> A1.5 <a href="/book/it/v2/Appendice-A:-Git-in-altri-contesti-Git-in-Zsh">Git in Zsh</a> </li> <li> A1.6 <a href="/book/it/v2/Appendice-A:-Git-in-altri-contesti-Git-in-Powershell">Git in Powershell</a> </li> <li> A1.7 <a href="/book/it/v2/Appendice-A:-Git-in-altri-contesti-Riassunto">Riassunto</a> </li> </ol> </li> <li class='chapter'> <h2>A2. <a href="/book/it/v2/Appendice-B:-Embedding-Git-in-your-Applications-Command-line-Git">Appendice B: Embedding Git in your Applications</a></h2> <ol> <li> A2.1 <a href="/book/it/v2/Appendice-B:-Embedding-Git-in-your-Applications-Command-line-Git">Command-line Git</a> </li> <li> A2.2 <a href="/book/it/v2/Appendice-B:-Embedding-Git-in-your-Applications-Libgit2">Libgit2</a> </li> <li> A2.3 <a href="/book/it/v2/Appendice-B:-Embedding-Git-in-your-Applications-JGit">JGit</a> </li> </ol> </li> <li class='chapter'> <h2>A3. <a href="/book/it/v2/Appendice-C:-Git-Commands-Setup-and-Config">Appendice C: Git Commands</a></h2> <ol> <li> A3.1 <a href="/book/it/v2/Appendice-C:-Git-Commands-Setup-and-Config">Setup and Config</a> </li> <li> A3.2 <a href="/book/it/v2/Appendice-C:-Git-Commands-Getting-and-Creating-Projects">Getting and Creating Projects</a> </li> <li> A3.3 <a href="/book/it/v2/Appendice-C:-Git-Commands-Basic-Snapshotting">Basic Snapshotting</a> </li> <li> A3.4 <a href="/book/it/v2/Appendice-C:-Git-Commands-Branching-and-Merging">Branching and Merging</a> </li> <li> A3.5 <a href="/book/it/v2/Appendice-C:-Git-Commands-Sharing-and-Updating-Projects">Sharing and Updating Projects</a> </li> <li> A3.6 <a href="/book/it/v2/Appendice-C:-Git-Commands-Inspection-and-Comparison">Inspection and Comparison</a> </li> <li> A3.7 <a href="/book/it/v2/Appendice-C:-Git-Commands-Debugging">Debugging</a> </li> <li> A3.8 <a href="/book/it/v2/Appendice-C:-Git-Commands-Patching">Patching</a> </li> <li> A3.9 <a href="/book/it/v2/Appendice-C:-Git-Commands-Email">Email</a> </li> <li> A3.10 <a href="/book/it/v2/Appendice-C:-Git-Commands-External-Systems">External Systems</a> </li> <li> A3.11 <a href="/book/it/v2/Appendice-C:-Git-Commands-Administration">Administration</a> </li> <li> A3.12 <a href="/book/it/v2/Appendice-C:-Git-Commands-Plumbing-Commands">Plumbing Commands</a> </li> </ol> </li> </ol> </div> </div> </div> <span class="light" id="edition"> 2nd Edition </span> </div> <div id="main" data-pagefind-filter="category:book" data-pagefind-meta="category:Book" data-pagefind-weight="0.05" data-pagefind-body class="book edition2"> <h1>7.1 Git Tools - Revision Selection</h1> <div> <p>By now, you’ve learned most of the day-to-day commands and workflows that you need to manage or maintain a Git repository for your source code control. You’ve accomplished the basic tasks of tracking and committing files, and you’ve harnessed the power of the staging area and lightweight topic branching and merging.</p><p>Now you’ll explore a number of very powerful things that Git can do that you may not necessarily use on a day-to-day basis but that you may need at some point.</p> <h2 id="r_revision_selection">Revision Selection</h2> <div class="paragraph"> <p>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> </div> <div class="sect3"> <h3 id="_single_revisions">Single Revisions</h3> <div class="paragraph"> <p>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> </div> </div> <div class="sect3"> <h3 id="_short_sha">Short SHA</h3> <div class="paragraph"> <p>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> </div> <div class="paragraph"> <p>For example, to see a specific commit, suppose you run a <code>git log</code> command and identify the commit where you added certain functionality:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ 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> </div> </div> <div class="paragraph"> <p>In this case, choose <code>1c002dd....</code> If you <code>git show</code> that commit, the following commands are equivalent (assuming the shorter versions are unambiguous):</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git show 1c002dd4b536e7479fe34593e72e6c6c1819e53b $ git show 1c002dd4b536e7479f $ git show 1c002d</code></pre> </div> </div> <div class="paragraph"> <p>Git can figure out a short, unique abbreviation for your SHA-1 values. If you pass <code>--abbrev-commit</code> to the <code>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> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git log --abbrev-commit --pretty=oneline ca82a6d changed the version number 085bb3b removed unnecessary test code a11bef0 first commit</code></pre> </div> </div> <div class="paragraph"> <p>Generally, eight to ten characters are more than enough to be unique within a project.</p> </div> <div class="paragraph"> <p>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 SHAs overlap more than the first 11 characters.</p> </div> <div class="admonitionblock note"> <table> <tr> <td class="icon"> <div class="title">Nota</div> </td> <td class="content"> <div class="title">A SHORT NOTE ABOUT SHA-1</div> <div class="paragraph"> <p>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> </div> <div class="paragraph"> <p>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> </div> <div class="paragraph"> <p>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>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> </div> <div class="paragraph"> <p>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 (3.6 million Git objects) and pushing it into one enormous Git repository, it would take roughly 2 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> </div> </td> </tr> </table> </div> </div> <div class="sect3"> <h3 id="r_branch_references">Branch References</h3> <div class="paragraph"> <p>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>topic1</code> branch points to <code>ca82a6d</code>:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git show ca82a6dff817ec66f44342007202690a93763949 $ git show topic1</code></pre> </div> </div> <div class="paragraph"> <p>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>rev-parse</code>. You can see <a href="/book/it/v2/ch00/ch10-git-internals">[ch10-git-internals]</a> for more information about plumbing tools; basically, <code>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>rev-parse</code> on your branch.</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git rev-parse topic1 ca82a6dff817ec66f44342007202690a93763949</code></pre> </div> </div> </div> <div class="sect3"> <h3 id="r_git_reflog">RefLog Shortnames</h3> <div class="paragraph"> <p>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> </div> <div class="paragraph"> <p>You can see your reflog by using <code>git reflog</code>:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ 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> </div> </div> <div class="paragraph"> <p>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>@{n}</code> reference that you see in the reflog output:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git show HEAD@{5}</code></pre> </div> </div> <div class="paragraph"> <p>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>master</code> branch was yesterday, you can type</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git show master@{yesterday}</code></pre> </div> </div> <div class="paragraph"> <p>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> </div> <div class="paragraph"> <p>To see reflog information formatted like the <code>git log</code> output, you can run <code>git log -g</code>:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ 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> </div> </div> <div class="paragraph"> <p>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>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> </div> </div> <div class="sect3"> <h3 id="_ancestry_references">Ancestry References</h3> <div class="paragraph"> <p>The other main way to specify a commit is via its ancestry. If you place a <code>^</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> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ 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> </div> </div> <div class="paragraph"> <p>Then, you can see the previous commit by specifying <code>HEAD^</code>, which means “the parent of HEAD”:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ 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> </div> </div> <div class="paragraph"> <p>You can also specify a number after the <code>^</code> – for example, <code>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> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ 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> </div> </div> <div class="paragraph"> <p>The other main ancestry specification is the <code>~</code>. This also refers to the first parent, so <code>HEAD~</code> and <code>HEAD^</code> are equivalent. The difference becomes apparent when you specify a number. <code>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>HEAD~3</code> would be</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ 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> </div> </div> <div class="paragraph"> <p>This can also be written <code>HEAD^^^</code>, which again is the first parent of the first parent of the first parent:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ 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> </div> </div> <div class="paragraph"> <p>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>HEAD~3^2</code>, and so on.</p> </div> </div> <div class="sect3"> <h3 id="r_commit_ranges">Commit Ranges</h3> <div class="paragraph"> <p>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> </div> <div class="sect4"> <h4 id="_double_dot">Double Dot</h4> <div class="paragraph"> <p>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 <a href="/book/it/v2/ch00/rdouble_dot">Example history for range selection.</a>.</p> </div> <div id="rdouble_dot" class="imageblock"> <div class="content"> <img src="/book/it/v2/images/double-dot.png" alt="Example history for range selection."> </div> <div class="title">Figura 137. Example history for range selection.</div> </div> <div class="paragraph"> <p>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>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> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git log master..experiment D C</code></pre> </div> </div> <div class="paragraph"> <p>If, on the other hand, you want to see the opposite – all commits in <code>master</code> that aren’t in <code>experiment</code> – you can reverse the branch names. <code>experiment..master</code> shows you everything in <code>master</code> not reachable from <code>experiment</code>:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git log experiment..master F E</code></pre> </div> </div> <div class="paragraph"> <p>This is useful if you want to keep the <code>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> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git log origin/master..HEAD</code></pre> </div> </div> <div class="paragraph"> <p>This command shows you any commits in your current branch that aren’t in the <code>master</code> branch on your <code>origin</code> remote. If you run a <code>git push</code> and your current branch is tracking <code>origin/master</code>, the commits listed by <code>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>git log origin/master..</code> – Git substitutes HEAD if one side is missing.</p> </div> </div> <div class="sect4"> <h4 id="_multiple_points">Multiple Points</h4> <div class="paragraph"> <p>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>^</code> character or <code>--not</code> before any reference from which you don’t want to see reachable commits. Thus these three commands are equivalent:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git log refA..refB $ git log ^refA refB $ git log refB --not refA</code></pre> </div> </div> <div class="paragraph"> <p>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>refA</code> or <code>refB</code> but not from <code>refC</code>, you can type one of these:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git log refA refB ^refC $ git log refA refB --not refC</code></pre> </div> </div> <div class="paragraph"> <p>This makes for a very powerful revision query system that should help you figure out what is in your branches.</p> </div> </div> <div class="sect4"> <h4 id="r_triple_dot">Triple Dot</h4> <div class="paragraph"> <p>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 <a href="/book/it/v2/ch00/rdouble_dot">Example history for range selection.</a>. If you want to see what is in <code>master</code> or <code>experiment</code> but not any common references, you can run</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git log master...experiment F E D C</code></pre> </div> </div> <div class="paragraph"> <p>Again, this gives you normal <code>log</code> output but shows you only the commit information for those four commits, appearing in the traditional commit date ordering.</p> </div> <div class="paragraph"> <p>A common switch to use with the <code>log</code> command in this case is <code>--left-right</code>, which shows you which side of the range each commit is in. This helps make the data more useful:</p> </div> <div class="listingblock"> <div class="content"> <pre class="highlight"><code class="language-console" data-lang="console">$ git log --left-right master...experiment &lt; F &lt; E &gt; D &gt; C</code></pre> </div> </div> <div class="paragraph"> <p>With these tools, you can much more easily let Git know what commit or commits you want to inspect.</p> </div> </div> </div> <div id="nav"><a href="/book/it/v2/GitHub-Summary">prev</a> | <a href="/book/it/v2/Git-Tools-Interactive-Staging">next</a></div> </div> </div> </div> </div> <footer> <div class="site-source"> <a href="/site">About this site</a><br> Patches, suggestions, and comments are welcome. </div> <div class="sfc-member"> Git is a member of <a href="/sfc">Software Freedom Conservancy</a> </div> </footer> <a href="#top" class="no-js scrollToTop" id="scrollToTop" data-label="Scroll to top"> <img src="/images/icons/chevron-up@2x.png" width="20" height="20" alt="scroll-to-top"/> </a> <script src="/js/jquery-1.7.1.min.js"></script> <script src="/js/jquery-ui-1.8.18.custom.min.js"></script> <script src="/js/jquery.defaultvalue.js"></script> <script src="/js/session.min.js"></script> <script src="/js/application.min.js"></script> </div> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10