UNPKG

esy-bash

Version:

Cross-platform bash utilities - primed for Reason/OCaml

1,381 lines (1,298 loc) 100 kB
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" /> <meta name="generator" content="AsciiDoc 8.6.9" /> <title>gitcore-tutorial(7)</title> <style type="text/css"> /* Shared CSS for AsciiDoc xhtml11 and html5 backends */ /* Default font. */ body { font-family: Georgia,serif; } /* Title font. */ h1, h2, h3, h4, h5, h6, div.title, caption.title, thead, p.table.header, #toctitle, #author, #revnumber, #revdate, #revremark, #footer { font-family: Arial,Helvetica,sans-serif; } body { margin: 1em 5% 1em 5%; } a { color: blue; text-decoration: underline; } a:visited { color: fuchsia; } em { font-style: italic; color: navy; } strong { font-weight: bold; color: #083194; } h1, h2, h3, h4, h5, h6 { color: #527bbd; margin-top: 1.2em; margin-bottom: 0.5em; line-height: 1.3; } h1, h2, h3 { border-bottom: 2px solid silver; } h2 { padding-top: 0.5em; } h3 { float: left; } h3 + * { clear: left; } h5 { font-size: 1.0em; } div.sectionbody { margin-left: 0; } hr { border: 1px solid silver; } p { margin-top: 0.5em; margin-bottom: 0.5em; } ul, ol, li > p { margin-top: 0; } ul > li { color: #aaa; } ul > li > * { color: black; } .monospaced, code, pre { font-family: "Courier New", Courier, monospace; font-size: inherit; color: navy; padding: 0; margin: 0; } pre { white-space: pre-wrap; } #author { color: #527bbd; font-weight: bold; font-size: 1.1em; } #email { } #revnumber, #revdate, #revremark { } #footer { font-size: small; border-top: 2px solid silver; padding-top: 0.5em; margin-top: 4.0em; } #footer-text { float: left; padding-bottom: 0.5em; } #footer-badges { float: right; padding-bottom: 0.5em; } #preamble { margin-top: 1.5em; margin-bottom: 1.5em; } div.imageblock, div.exampleblock, div.verseblock, div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock, div.admonitionblock { margin-top: 1.0em; margin-bottom: 1.5em; } div.admonitionblock { margin-top: 2.0em; margin-bottom: 2.0em; margin-right: 10%; color: #606060; } div.content { /* Block element content. */ padding: 0; } /* Block element titles. */ div.title, caption.title { color: #527bbd; font-weight: bold; text-align: left; margin-top: 1.0em; margin-bottom: 0.5em; } div.title + * { margin-top: 0; } td div.title:first-child { margin-top: 0.0em; } div.content div.title:first-child { margin-top: 0.0em; } div.content + div.title { margin-top: 0.0em; } div.sidebarblock > div.content { background: #ffffee; border: 1px solid #dddddd; border-left: 4px solid #f0f0f0; padding: 0.5em; } div.listingblock > div.content { border: 1px solid #dddddd; border-left: 5px solid #f0f0f0; background: #f8f8f8; padding: 0.5em; } div.quoteblock, div.verseblock { padding-left: 1.0em; margin-left: 1.0em; margin-right: 10%; border-left: 5px solid #f0f0f0; color: #888; } div.quoteblock > div.attribution { padding-top: 0.5em; text-align: right; } div.verseblock > pre.content { font-family: inherit; font-size: inherit; } div.verseblock > div.attribution { padding-top: 0.75em; text-align: left; } /* DEPRECATED: Pre version 8.2.7 verse style literal block. */ div.verseblock + div.attribution { text-align: left; } div.admonitionblock .icon { vertical-align: top; font-size: 1.1em; font-weight: bold; text-decoration: underline; color: #527bbd; padding-right: 0.5em; } div.admonitionblock td.content { padding-left: 0.5em; border-left: 3px solid #dddddd; } div.exampleblock > div.content { border-left: 3px solid #dddddd; padding-left: 0.5em; } div.imageblock div.content { padding-left: 0; } span.image img { border-style: none; vertical-align: text-bottom; } a.image:visited { color: white; } dl { margin-top: 0.8em; margin-bottom: 0.8em; } dt { margin-top: 0.5em; margin-bottom: 0; font-style: normal; color: navy; } dd > *:first-child { margin-top: 0.1em; } ul, ol { list-style-position: outside; } ol.arabic { list-style-type: decimal; } ol.loweralpha { list-style-type: lower-alpha; } ol.upperalpha { list-style-type: upper-alpha; } ol.lowerroman { list-style-type: lower-roman; } ol.upperroman { list-style-type: upper-roman; } div.compact ul, div.compact ol, div.compact p, div.compact p, div.compact div, div.compact div { margin-top: 0.1em; margin-bottom: 0.1em; } tfoot { font-weight: bold; } td > div.verse { white-space: pre; } div.hdlist { margin-top: 0.8em; margin-bottom: 0.8em; } div.hdlist tr { padding-bottom: 15px; } dt.hdlist1.strong, td.hdlist1.strong { font-weight: bold; } td.hdlist1 { vertical-align: top; font-style: normal; padding-right: 0.8em; color: navy; } td.hdlist2 { vertical-align: top; } div.hdlist.compact tr { margin: 0; padding-bottom: 0; } .comment { background: yellow; } .footnote, .footnoteref { font-size: 0.8em; } span.footnote, span.footnoteref { vertical-align: super; } #footnotes { margin: 20px 0 20px 0; padding: 7px 0 0 0; } #footnotes div.footnote { margin: 0 0 5px 0; } #footnotes hr { border: none; border-top: 1px solid silver; height: 1px; text-align: left; margin-left: 0; width: 20%; min-width: 100px; } div.colist td { padding-right: 0.5em; padding-bottom: 0.3em; vertical-align: top; } div.colist td img { margin-top: 0.3em; } @media print { #footer-badges { display: none; } } #toc { margin-bottom: 2.5em; } #toctitle { color: #527bbd; font-size: 1.1em; font-weight: bold; margin-top: 1.0em; margin-bottom: 0.1em; } div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 { margin-top: 0; margin-bottom: 0; } div.toclevel2 { margin-left: 2em; font-size: 0.9em; } div.toclevel3 { margin-left: 4em; font-size: 0.9em; } div.toclevel4 { margin-left: 6em; font-size: 0.9em; } span.aqua { color: aqua; } span.black { color: black; } span.blue { color: blue; } span.fuchsia { color: fuchsia; } span.gray { color: gray; } span.green { color: green; } span.lime { color: lime; } span.maroon { color: maroon; } span.navy { color: navy; } span.olive { color: olive; } span.purple { color: purple; } span.red { color: red; } span.silver { color: silver; } span.teal { color: teal; } span.white { color: white; } span.yellow { color: yellow; } span.aqua-background { background: aqua; } span.black-background { background: black; } span.blue-background { background: blue; } span.fuchsia-background { background: fuchsia; } span.gray-background { background: gray; } span.green-background { background: green; } span.lime-background { background: lime; } span.maroon-background { background: maroon; } span.navy-background { background: navy; } span.olive-background { background: olive; } span.purple-background { background: purple; } span.red-background { background: red; } span.silver-background { background: silver; } span.teal-background { background: teal; } span.white-background { background: white; } span.yellow-background { background: yellow; } span.big { font-size: 2em; } span.small { font-size: 0.6em; } span.underline { text-decoration: underline; } span.overline { text-decoration: overline; } span.line-through { text-decoration: line-through; } div.unbreakable { page-break-inside: avoid; } /* * xhtml11 specific * * */ div.tableblock { margin-top: 1.0em; margin-bottom: 1.5em; } div.tableblock > table { border: 3px solid #527bbd; } thead, p.table.header { font-weight: bold; color: #527bbd; } p.table { margin-top: 0; } /* Because the table frame attribute is overriden by CSS in most browsers. */ div.tableblock > table[frame="void"] { border-style: none; } div.tableblock > table[frame="hsides"] { border-left-style: none; border-right-style: none; } div.tableblock > table[frame="vsides"] { border-top-style: none; border-bottom-style: none; } /* * html5 specific * * */ table.tableblock { margin-top: 1.0em; margin-bottom: 1.5em; } thead, p.tableblock.header { font-weight: bold; color: #527bbd; } p.tableblock { margin-top: 0; } table.tableblock { border-width: 3px; border-spacing: 0px; border-style: solid; border-color: #527bbd; border-collapse: collapse; } th.tableblock, td.tableblock { border-width: 1px; padding: 4px; border-style: solid; border-color: #527bbd; } table.tableblock.frame-topbot { border-left-style: hidden; border-right-style: hidden; } table.tableblock.frame-sides { border-top-style: hidden; border-bottom-style: hidden; } table.tableblock.frame-none { border-style: hidden; } th.tableblock.halign-left, td.tableblock.halign-left { text-align: left; } th.tableblock.halign-center, td.tableblock.halign-center { text-align: center; } th.tableblock.halign-right, td.tableblock.halign-right { text-align: right; } th.tableblock.valign-top, td.tableblock.valign-top { vertical-align: top; } th.tableblock.valign-middle, td.tableblock.valign-middle { vertical-align: middle; } th.tableblock.valign-bottom, td.tableblock.valign-bottom { vertical-align: bottom; } /* * manpage specific * * */ body.manpage h1 { padding-top: 0.5em; padding-bottom: 0.5em; border-top: 2px solid silver; border-bottom: 2px solid silver; } body.manpage h2 { border-style: none; } body.manpage div.sectionbody { margin-left: 3em; } @media print { body.manpage div#toc { display: none; } } </style> <script type="text/javascript"> /*<![CDATA[*/ var asciidoc = { // Namespace. ///////////////////////////////////////////////////////////////////// // Table Of Contents generator ///////////////////////////////////////////////////////////////////// /* Author: Mihai Bazon, September 2002 * http://students.infoiasi.ro/~mishoo * * Table Of Content generator * Version: 0.4 * * Feel free to use this script under the terms of the GNU General Public * License, as long as you do not remove or alter this notice. */ /* modified by Troy D. Hanson, September 2006. License: GPL */ /* modified by Stuart Rackham, 2006, 2009. License: GPL */ // toclevels = 1..4. toc: function (toclevels) { function getText(el) { var text = ""; for (var i = el.firstChild; i != null; i = i.nextSibling) { if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants. text += i.data; else if (i.firstChild != null) text += getText(i); } return text; } function TocEntry(el, text, toclevel) { this.element = el; this.text = text; this.toclevel = toclevel; } function tocEntries(el, toclevels) { var result = new Array; var re = new RegExp('[hH]([1-'+(toclevels+1)+'])'); // Function that scans the DOM tree for header elements (the DOM2 // nodeIterator API would be a better technique but not supported by all // browsers). var iterate = function (el) { for (var i = el.firstChild; i != null; i = i.nextSibling) { if (i.nodeType == 1 /* Node.ELEMENT_NODE */) { var mo = re.exec(i.tagName); if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") { result[result.length] = new TocEntry(i, getText(i), mo[1]-1); } iterate(i); } } } iterate(el); return result; } var toc = document.getElementById("toc"); if (!toc) { return; } // Delete existing TOC entries in case we're reloading the TOC. var tocEntriesToRemove = []; var i; for (i = 0; i < toc.childNodes.length; i++) { var entry = toc.childNodes[i]; if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") && entry.getAttribute("class").match(/^toclevel/)) tocEntriesToRemove.push(entry); } for (i = 0; i < tocEntriesToRemove.length; i++) { toc.removeChild(tocEntriesToRemove[i]); } // Rebuild TOC entries. var entries = tocEntries(document.getElementById("content"), toclevels); for (var i = 0; i < entries.length; ++i) { var entry = entries[i]; if (entry.element.id == "") entry.element.id = "_toc_" + i; var a = document.createElement("a"); a.href = "#" + entry.element.id; a.appendChild(document.createTextNode(entry.text)); var div = document.createElement("div"); div.appendChild(a); div.className = "toclevel" + entry.toclevel; toc.appendChild(div); } if (entries.length == 0) toc.parentNode.removeChild(toc); }, ///////////////////////////////////////////////////////////////////// // Footnotes generator ///////////////////////////////////////////////////////////////////// /* Based on footnote generation code from: * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html */ footnotes: function () { // Delete existing footnote entries in case we're reloading the footnodes. var i; var noteholder = document.getElementById("footnotes"); if (!noteholder) { return; } var entriesToRemove = []; for (i = 0; i < noteholder.childNodes.length; i++) { var entry = noteholder.childNodes[i]; if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote") entriesToRemove.push(entry); } for (i = 0; i < entriesToRemove.length; i++) { noteholder.removeChild(entriesToRemove[i]); } // Rebuild footnote entries. var cont = document.getElementById("content"); var spans = cont.getElementsByTagName("span"); var refs = {}; var n = 0; for (i=0; i<spans.length; i++) { if (spans[i].className == "footnote") { n++; var note = spans[i].getAttribute("data-note"); if (!note) { // Use [\s\S] in place of . so multi-line matches work. // Because JavaScript has no s (dotall) regex flag. note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1]; spans[i].innerHTML = "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n + "' title='View footnote' class='footnote'>" + n + "</a>]"; spans[i].setAttribute("data-note", note); } noteholder.innerHTML += "<div class='footnote' id='_footnote_" + n + "'>" + "<a href='#_footnoteref_" + n + "' title='Return to text'>" + n + "</a>. " + note + "</div>"; var id =spans[i].getAttribute("id"); if (id != null) refs["#"+id] = n; } } if (n == 0) noteholder.parentNode.removeChild(noteholder); else { // Process footnoterefs. for (i=0; i<spans.length; i++) { if (spans[i].className == "footnoteref") { var href = spans[i].getElementsByTagName("a")[0].getAttribute("href"); href = href.match(/#.*/)[0]; // Because IE return full URL. n = refs[href]; spans[i].innerHTML = "[<a href='#_footnote_" + n + "' title='View footnote' class='footnote'>" + n + "</a>]"; } } } }, install: function(toclevels) { var timerId; function reinstall() { asciidoc.footnotes(); if (toclevels) { asciidoc.toc(toclevels); } } function reinstallAndRemoveTimer() { clearInterval(timerId); reinstall(); } timerId = setInterval(reinstall, 500); if (document.addEventListener) document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false); else window.onload = reinstallAndRemoveTimer; } } asciidoc.install(); /*]]>*/ </script> </head> <body class="manpage"> <div id="header"> <h1> gitcore-tutorial(7) Manual Page </h1> <h2>NAME</h2> <div class="sectionbody"> <p>gitcore-tutorial - A Git core tutorial for developers </p> </div> </div> <div id="content"> <div class="sect1"> <h2 id="_synopsis">SYNOPSIS</h2> <div class="sectionbody"> <div class="paragraph"><p>git *</p></div> </div> </div> <div class="sect1"> <h2 id="_description">DESCRIPTION</h2> <div class="sectionbody"> <div class="paragraph"><p>This tutorial explains how to use the "core" Git commands to set up and work with a Git repository.</p></div> <div class="paragraph"><p>If you just need to use Git as a revision control system you may prefer to start with "A Tutorial Introduction to Git" (<a href="gittutorial.html">gittutorial(7)</a>) or <a href="user-manual.html">the Git User Manual</a>.</p></div> <div class="paragraph"><p>However, an understanding of these low-level tools can be helpful if you want to understand Git&#8217;s internals.</p></div> <div class="paragraph"><p>The core Git is often called "plumbing", with the prettier user interfaces on top of it called "porcelain". You may not want to use the plumbing directly very often, but it can be good to know what the plumbing does when the porcelain isn&#8217;t flushing.</p></div> <div class="paragraph"><p>Back when this document was originally written, many porcelain commands were shell scripts. For simplicity, it still uses them as examples to illustrate how plumbing is fit together to form the porcelain commands. The source tree includes some of these scripts in contrib/examples/ for reference. Although these are not implemented as shell scripts anymore, the description of what the plumbing layer commands do is still valid.</p></div> <div class="admonitionblock"> <table><tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content">Deeper technical details are often marked as Notes, which you can skip on your first reading.</td> </tr></table> </div> </div> </div> <div class="sect1"> <h2 id="_creating_a_git_repository">Creating a Git repository</h2> <div class="sectionbody"> <div class="paragraph"><p>Creating a new Git repository couldn&#8217;t be easier: all Git repositories start out empty, and the only thing you need to do is find yourself a subdirectory that you want to use as a working tree - either an empty one for a totally new project, or an existing working tree that you want to import into Git.</p></div> <div class="paragraph"><p>For our first example, we&#8217;re going to start a totally new repository from scratch, with no pre-existing files, and we&#8217;ll call it <em>git-tutorial</em>. To start up, create a subdirectory for it, change into that subdirectory, and initialize the Git infrastructure with <em>git init</em>:</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ mkdir git-tutorial $ cd git-tutorial $ git init</code></pre> </div></div> <div class="paragraph"><p>to which Git will reply</p></div> <div class="listingblock"> <div class="content"> <pre><code>Initialized empty Git repository in .git/</code></pre> </div></div> <div class="paragraph"><p>which is just Git&#8217;s way of saying that you haven&#8217;t been doing anything strange, and that it will have created a local <code>.git</code> directory setup for your new project. You will now have a <code>.git</code> directory, and you can inspect that with <em>ls</em>. For your new empty project, it should show you three entries, among other things:</p></div> <div class="ulist"><ul> <li> <p> a file called <code>HEAD</code>, that has <code>ref: refs/heads/master</code> in it. This is similar to a symbolic link and points at <code>refs/heads/master</code> relative to the <code>HEAD</code> file. </p> <div class="paragraph"><p>Don&#8217;t worry about the fact that the file that the <code>HEAD</code> link points to doesn&#8217;t even exist yet&#8201;&#8212;&#8201;you haven&#8217;t created the commit that will start your <code>HEAD</code> development branch yet.</p></div> </li> <li> <p> a subdirectory called <code>objects</code>, which will contain all the objects of your project. You should never have any real reason to look at the objects directly, but you might want to know that these objects are what contains all the real <em>data</em> in your repository. </p> </li> <li> <p> a subdirectory called <code>refs</code>, which contains references to objects. </p> </li> </ul></div> <div class="paragraph"><p>In particular, the <code>refs</code> subdirectory will contain two other subdirectories, named <code>heads</code> and <code>tags</code> respectively. They do exactly what their names imply: they contain references to any number of different <em>heads</em> of development (aka <em>branches</em>), and to any <em>tags</em> that you have created to name specific versions in your repository.</p></div> <div class="paragraph"><p>One note: the special <code>master</code> head is the default branch, which is why the <code>.git/HEAD</code> file was created points to it even if it doesn&#8217;t yet exist. Basically, the <code>HEAD</code> link is supposed to always point to the branch you are working on right now, and you always start out expecting to work on the <code>master</code> branch.</p></div> <div class="paragraph"><p>However, this is only a convention, and you can name your branches anything you want, and don&#8217;t have to ever even <em>have</em> a <code>master</code> branch. A number of the Git tools will assume that <code>.git/HEAD</code> is valid, though.</p></div> <div class="admonitionblock"> <table><tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content">An <em>object</em> is identified by its 160-bit SHA-1 hash, aka <em>object name</em>, and a reference to an object is always the 40-byte hex representation of that SHA-1 name. The files in the <code>refs</code> subdirectory are expected to contain these hex references (usually with a final <code>\n</code> at the end), and you should thus expect to see a number of 41-byte files containing these references in these <code>refs</code> subdirectories when you actually start populating your tree.</td> </tr></table> </div> <div class="admonitionblock"> <table><tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content">An advanced user may want to take a look at <a href="gitrepository-layout.html">gitrepository-layout(5)</a> after finishing this tutorial.</td> </tr></table> </div> <div class="paragraph"><p>You have now created your first Git repository. Of course, since it&#8217;s empty, that&#8217;s not very useful, so let&#8217;s start populating it with data.</p></div> </div> </div> <div class="sect1"> <h2 id="_populating_a_git_repository">Populating a Git repository</h2> <div class="sectionbody"> <div class="paragraph"><p>We&#8217;ll keep this simple and stupid, so we&#8217;ll start off with populating a few trivial files just to get a feel for it.</p></div> <div class="paragraph"><p>Start off with just creating any random files that you want to maintain in your Git repository. We&#8217;ll start off with a few bad examples, just to get a feel for how this works:</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ echo "Hello World" &gt;hello $ echo "Silly example" &gt;example</code></pre> </div></div> <div class="paragraph"><p>you have now created two files in your working tree (aka <em>working directory</em>), but to actually check in your hard work, you will have to go through two steps:</p></div> <div class="ulist"><ul> <li> <p> fill in the <em>index</em> file (aka <em>cache</em>) with the information about your working tree state. </p> </li> <li> <p> commit that index file as an object. </p> </li> </ul></div> <div class="paragraph"><p>The first step is trivial: when you want to tell Git about any changes to your working tree, you use the <em>git update-index</em> program. That program normally just takes a list of filenames you want to update, but to avoid trivial mistakes, it refuses to add new entries to the index (or remove existing ones) unless you explicitly tell it that you&#8217;re adding a new entry with the <code>--add</code> flag (or removing an entry with the <code>--remove</code>) flag.</p></div> <div class="paragraph"><p>So to populate the index with the two files you just created, you can do</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git update-index --add hello example</code></pre> </div></div> <div class="paragraph"><p>and you have now told Git to track those two files.</p></div> <div class="paragraph"><p>In fact, as you did that, if you now look into your object directory, you&#8217;ll notice that Git will have added two new objects to the object database. If you did exactly the steps above, you should now be able to do</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ ls .git/objects/??/*</code></pre> </div></div> <div class="paragraph"><p>and see two files:</p></div> <div class="listingblock"> <div class="content"> <pre><code>.git/objects/55/7db03de997c86a4a028e1ebd3a1ceb225be238 .git/objects/f2/4c74a2e500f5ee1332c86b94199f52b1d1d962</code></pre> </div></div> <div class="paragraph"><p>which correspond with the objects with names of <code>557db...</code> and <code>f24c7...</code> respectively.</p></div> <div class="paragraph"><p>If you want to, you can use <em>git cat-file</em> to look at those objects, but you&#8217;ll have to use the object name, not the filename of the object:</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git cat-file -t 557db03de997c86a4a028e1ebd3a1ceb225be238</code></pre> </div></div> <div class="paragraph"><p>where the <code>-t</code> tells <em>git cat-file</em> to tell you what the "type" of the object is. Git will tell you that you have a "blob" object (i.e., just a regular file), and you can see the contents with</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git cat-file blob 557db03</code></pre> </div></div> <div class="paragraph"><p>which will print out "Hello World". The object <code>557db03</code> is nothing more than the contents of your file <code>hello</code>.</p></div> <div class="admonitionblock"> <table><tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content">Don&#8217;t confuse that object with the file <code>hello</code> itself. The object is literally just those specific <strong>contents</strong> of the file, and however much you later change the contents in file <code>hello</code>, the object we just looked at will never change. Objects are immutable.</td> </tr></table> </div> <div class="admonitionblock"> <table><tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content">The second example demonstrates that you can abbreviate the object name to only the first several hexadecimal digits in most places.</td> </tr></table> </div> <div class="paragraph"><p>Anyway, as we mentioned previously, you normally never actually take a look at the objects themselves, and typing long 40-character hex names is not something you&#8217;d normally want to do. The above digression was just to show that <em>git update-index</em> did something magical, and actually saved away the contents of your files into the Git object database.</p></div> <div class="paragraph"><p>Updating the index did something else too: it created a <code>.git/index</code> file. This is the index that describes your current working tree, and something you should be very aware of. Again, you normally never worry about the index file itself, but you should be aware of the fact that you have not actually really "checked in" your files into Git so far, you&#8217;ve only <strong>told</strong> Git about them.</p></div> <div class="paragraph"><p>However, since Git knows about them, you can now start using some of the most basic Git commands to manipulate the files or look at their status.</p></div> <div class="paragraph"><p>In particular, let&#8217;s not even check in the two files into Git yet, we&#8217;ll start off by adding another line to <code>hello</code> first:</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ echo "It's a new day for git" &gt;&gt;hello</code></pre> </div></div> <div class="paragraph"><p>and you can now, since you told Git about the previous state of <code>hello</code>, ask Git what has changed in the tree compared to your old index, using the <em>git diff-files</em> command:</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git diff-files</code></pre> </div></div> <div class="paragraph"><p>Oops. That wasn&#8217;t very readable. It just spit out its own internal version of a <em>diff</em>, but that internal version really just tells you that it has noticed that "hello" has been modified, and that the old object contents it had have been replaced with something else.</p></div> <div class="paragraph"><p>To make it readable, we can tell <em>git diff-files</em> to output the differences as a patch, using the <code>-p</code> flag:</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git diff-files -p diff --git a/hello b/hello index 557db03..263414f 100644 --- a/hello +++ b/hello @@ -1 +1,2 @@ Hello World +It's a new day for git</code></pre> </div></div> <div class="paragraph"><p>i.e. the diff of the change we caused by adding another line to <code>hello</code>.</p></div> <div class="paragraph"><p>In other words, <em>git diff-files</em> always shows us the difference between what is recorded in the index, and what is currently in the working tree. That&#8217;s very useful.</p></div> <div class="paragraph"><p>A common shorthand for <code>git diff-files -p</code> is to just write <code>git diff</code>, which will do the same thing.</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git diff diff --git a/hello b/hello index 557db03..263414f 100644 --- a/hello +++ b/hello @@ -1 +1,2 @@ Hello World +It's a new day for git</code></pre> </div></div> </div> </div> <div class="sect1"> <h2 id="_committing_git_state">Committing Git state</h2> <div class="sectionbody"> <div class="paragraph"><p>Now, we want to go to the next stage in Git, which is to take the files that Git knows about in the index, and commit them as a real tree. We do that in two phases: creating a <em>tree</em> object, and committing that <em>tree</em> object as a <em>commit</em> object together with an explanation of what the tree was all about, along with information of how we came to that state.</p></div> <div class="paragraph"><p>Creating a tree object is trivial, and is done with <em>git write-tree</em>. There are no options or other input: <code>git write-tree</code> will take the current index state, and write an object that describes that whole index. In other words, we&#8217;re now tying together all the different filenames with their contents (and their permissions), and we&#8217;re creating the equivalent of a Git "directory" object:</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git write-tree</code></pre> </div></div> <div class="paragraph"><p>and this will just output the name of the resulting tree, in this case (if you have done exactly as I&#8217;ve described) it should be</p></div> <div class="listingblock"> <div class="content"> <pre><code>8988da15d077d4829fc51d8544c097def6644dbb</code></pre> </div></div> <div class="paragraph"><p>which is another incomprehensible object name. Again, if you want to, you can use <code>git cat-file -t 8988d...</code> to see that this time the object is not a "blob" object, but a "tree" object (you can also use <code>git cat-file</code> to actually output the raw object contents, but you&#8217;ll see mainly a binary mess, so that&#8217;s less interesting).</p></div> <div class="paragraph"><p>However&#8201;&#8212;&#8201;normally you&#8217;d never use <em>git write-tree</em> on its own, because normally you always commit a tree into a commit object using the <em>git commit-tree</em> command. In fact, it&#8217;s easier to not actually use <em>git write-tree</em> on its own at all, but to just pass its result in as an argument to <em>git commit-tree</em>.</p></div> <div class="paragraph"><p><em>git commit-tree</em> normally takes several arguments&#8201;&#8212;&#8201;it wants to know what the <em>parent</em> of a commit was, but since this is the first commit ever in this new repository, and it has no parents, we only need to pass in the object name of the tree. However, <em>git commit-tree</em> also wants to get a commit message on its standard input, and it will write out the resulting object name for the commit to its standard output.</p></div> <div class="paragraph"><p>And this is where we create the <code>.git/refs/heads/master</code> file which is pointed at by <code>HEAD</code>. This file is supposed to contain the reference to the top-of-tree of the master branch, and since that&#8217;s exactly what <em>git commit-tree</em> spits out, we can do this all with a sequence of simple shell commands:</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ tree=$(git write-tree) $ commit=$(echo 'Initial commit' | git commit-tree $tree) $ git update-ref HEAD $commit</code></pre> </div></div> <div class="paragraph"><p>In this case this creates a totally new commit that is not related to anything else. Normally you do this only <strong>once</strong> for a project ever, and all later commits will be parented on top of an earlier commit.</p></div> <div class="paragraph"><p>Again, normally you&#8217;d never actually do this by hand. There is a helpful script called <code>git commit</code> that will do all of this for you. So you could have just written <code>git commit</code> instead, and it would have done the above magic scripting for you.</p></div> </div> </div> <div class="sect1"> <h2 id="_making_a_change">Making a change</h2> <div class="sectionbody"> <div class="paragraph"><p>Remember how we did the <em>git update-index</em> on file <code>hello</code> and then we changed <code>hello</code> afterward, and could compare the new state of <code>hello</code> with the state we saved in the index file?</p></div> <div class="paragraph"><p>Further, remember how I said that <em>git write-tree</em> writes the contents of the <strong>index</strong> file to the tree, and thus what we just committed was in fact the <strong>original</strong> contents of the file <code>hello</code>, not the new ones. We did that on purpose, to show the difference between the index state, and the state in the working tree, and how they don&#8217;t have to match, even when we commit things.</p></div> <div class="paragraph"><p>As before, if we do <code>git diff-files -p</code> in our git-tutorial project, we&#8217;ll still see the same difference we saw last time: the index file hasn&#8217;t changed by the act of committing anything. However, now that we have committed something, we can also learn to use a new command: <em>git diff-index</em>.</p></div> <div class="paragraph"><p>Unlike <em>git diff-files</em>, which showed the difference between the index file and the working tree, <em>git diff-index</em> shows the differences between a committed <strong>tree</strong> and either the index file or the working tree. In other words, <em>git diff-index</em> wants a tree to be diffed against, and before we did the commit, we couldn&#8217;t do that, because we didn&#8217;t have anything to diff against.</p></div> <div class="paragraph"><p>But now we can do</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git diff-index -p HEAD</code></pre> </div></div> <div class="paragraph"><p>(where <code>-p</code> has the same meaning as it did in <em>git diff-files</em>), and it will show us the same difference, but for a totally different reason. Now we&#8217;re comparing the working tree not against the index file, but against the tree we just wrote. It just so happens that those two are obviously the same, so we get the same result.</p></div> <div class="paragraph"><p>Again, because this is a common operation, you can also just shorthand it with</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git diff HEAD</code></pre> </div></div> <div class="paragraph"><p>which ends up doing the above for you.</p></div> <div class="paragraph"><p>In other words, <em>git diff-index</em> normally compares a tree against the working tree, but when given the <code>--cached</code> flag, it is told to instead compare against just the index cache contents, and ignore the current working tree state entirely. Since we just wrote the index file to HEAD, doing <code>git diff-index --cached -p HEAD</code> should thus return an empty set of differences, and that&#8217;s exactly what it does.</p></div> <div class="admonitionblock"> <table><tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content"> <div class="paragraph"><p><em>git diff-index</em> really always uses the index for its comparisons, and saying that it compares a tree against the working tree is thus not strictly accurate. In particular, the list of files to compare (the "meta-data") <strong>always</strong> comes from the index file, regardless of whether the <code>--cached</code> flag is used or not. The <code>--cached</code> flag really only determines whether the file <strong>contents</strong> to be compared come from the working tree or not.</p></div> <div class="paragraph"><p>This is not hard to understand, as soon as you realize that Git simply never knows (or cares) about files that it is not told about explicitly. Git will never go <strong>looking</strong> for files to compare, it expects you to tell it what the files are, and that&#8217;s what the index is there for.</p></div> </td> </tr></table> </div> <div class="paragraph"><p>However, our next step is to commit the <strong>change</strong> we did, and again, to understand what&#8217;s going on, keep in mind the difference between "working tree contents", "index file" and "committed tree". We have changes in the working tree that we want to commit, and we always have to work through the index file, so the first thing we need to do is to update the index cache:</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git update-index hello</code></pre> </div></div> <div class="paragraph"><p>(note how we didn&#8217;t need the <code>--add</code> flag this time, since Git knew about the file already).</p></div> <div class="paragraph"><p>Note what happens to the different <em>git diff-&#42;</em> versions here. After we&#8217;ve updated <code>hello</code> in the index, <code>git diff-files -p</code> now shows no differences, but <code>git diff-index -p HEAD</code> still <strong>does</strong> show that the current state is different from the state we committed. In fact, now <em>git diff-index</em> shows the same difference whether we use the <code>--cached</code> flag or not, since now the index is coherent with the working tree.</p></div> <div class="paragraph"><p>Now, since we&#8217;ve updated <code>hello</code> in the index, we can commit the new version. We could do it by writing the tree by hand again, and committing the tree (this time we&#8217;d have to use the <code>-p HEAD</code> flag to tell commit that the HEAD was the <strong>parent</strong> of the new commit, and that this wasn&#8217;t an initial commit any more), but you&#8217;ve done that once already, so let&#8217;s just use the helpful script this time:</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git commit</code></pre> </div></div> <div class="paragraph"><p>which starts an editor for you to write the commit message and tells you a bit about what you have done.</p></div> <div class="paragraph"><p>Write whatever message you want, and all the lines that start with <em>#</em> will be pruned out, and the rest will be used as the commit message for the change. If you decide you don&#8217;t want to commit anything after all at this point (you can continue to edit things and update the index), you can just leave an empty message. Otherwise <code>git commit</code> will commit the change for you.</p></div> <div class="paragraph"><p>You&#8217;ve now made your first real Git commit. And if you&#8217;re interested in looking at what <code>git commit</code> really does, feel free to investigate: it&#8217;s a few very simple shell scripts to generate the helpful (?) commit message headers, and a few one-liners that actually do the commit itself (<em>git commit</em>).</p></div> </div> </div> <div class="sect1"> <h2 id="_inspecting_changes">Inspecting Changes</h2> <div class="sectionbody"> <div class="paragraph"><p>While creating changes is useful, it&#8217;s even more useful if you can tell later what changed. The most useful command for this is another of the <em>diff</em> family, namely <em>git diff-tree</em>.</p></div> <div class="paragraph"><p><em>git diff-tree</em> can be given two arbitrary trees, and it will tell you the differences between them. Perhaps even more commonly, though, you can give it just a single commit object, and it will figure out the parent of that commit itself, and show the difference directly. Thus, to get the same diff that we&#8217;ve already seen several times, we can now do</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git diff-tree -p HEAD</code></pre> </div></div> <div class="paragraph"><p>(again, <code>-p</code> means to show the difference as a human-readable patch), and it will show what the last commit (in <code>HEAD</code>) actually changed.</p></div> <div class="admonitionblock"> <table><tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content"> <div class="paragraph"><p>Here is an ASCII art by Jon Loeliger that illustrates how various <em>diff-&#42;</em> commands compare things.</p></div> <div class="literalblock"> <div class="content"> <pre><code> diff-tree +----+ | | | | V V +-----------+ | Object DB | | Backing | | Store | +-----------+ ^ ^ | | | | diff-index --cached | | diff-index | V | +-----------+ | | Index | | | "cache" | | +-----------+ | ^ | | | | diff-files | | V V +-----------+ | Working | | Directory | +-----------+</code></pre> </div></div> </td> </tr></table> </div> <div class="paragraph"><p>More interestingly, you can also give <em>git diff-tree</em> the <code>--pretty</code> flag, which tells it to also show the commit message and author and date of the commit, and you can tell it to show a whole series of diffs. Alternatively, you can tell it to be "silent", and not show the diffs at all, but just show the actual commit message.</p></div> <div class="paragraph"><p>In fact, together with the <em>git rev-list</em> program (which generates a list of revisions), <em>git diff-tree</em> ends up being a veritable fount of changes. You can emulate <code>git log</code>, <code>git log -p</code>, etc. with a trivial script that pipes the output of <code>git rev-list</code> to <code>git diff-tree --stdin</code>, which was exactly how early versions of <code>git log</code> were implemented.</p></div> </div> </div> <div class="sect1"> <h2 id="_tagging_a_version">Tagging a version</h2> <div class="sectionbody"> <div class="paragraph"><p>In Git, there are two kinds of tags, a "light" one, and an "annotated tag".</p></div> <div class="paragraph"><p>A "light" tag is technically nothing more than a branch, except we put it in the <code>.git/refs/tags/</code> subdirectory instead of calling it a <code>head</code>. So the simplest form of tag involves nothing more than</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git tag my-first-tag</code></pre> </div></div> <div class="paragraph"><p>which just writes the current <code>HEAD</code> into the <code>.git/refs/tags/my-first-tag</code> file, after which point you can then use this symbolic name for that particular state. You can, for example, do</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git diff my-first-tag</code></pre> </div></div> <div class="paragraph"><p>to diff your current state against that tag which at this point will obviously be an empty diff, but if you continue to develop and commit stuff, you can use your tag as an "anchor-point" to see what has changed since you tagged it.</p></div> <div class="paragraph"><p>An "annotated tag" is actually a real Git object, and contains not only a pointer to the state you want to tag, but also a small tag name and message, along with optionally a PGP signature that says that yes, you really did that tag. You create these annotated tags with either the <code>-a</code> or <code>-s</code> flag to <em>git tag</em>:</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git tag -s &lt;tagname&gt;</code></pre> </div></div> <div class="paragraph"><p>which will sign the current <code>HEAD</code> (but you can also give it another argument that specifies the thing to tag, e.g., you could have tagged the current <code>mybranch</code> point by using <code>git tag &lt;tagname&gt; mybranch</code>).</p></div> <div class="paragraph"><p>You normally only do signed tags for major releases or things like that, while the light-weight tags are useful for any marking you want to do&#8201;&#8212;&#8201;any time you decide that you want to remember a certain point, just create a private tag for it, and you have a nice symbolic name for the state at that point.</p></div> </div> </div> <div class="sect1"> <h2 id="_copying_repositories">Copying repositories</h2> <div class="sectionbody"> <div class="paragraph"><p>Git repositories are normally totally self-sufficient and relocatable. Unlike CVS, for example, there is no separate notion of "repository" and "working tree". A Git repository normally <strong>is</strong> the working tree, with the local Git information hidden in the <code>.git</code> subdirectory. There is nothing else. What you see is what you got.</p></div> <div class="admonitionblock"> <table><tr> <td class="icon"> <div class="title">Note</div> </td> <td class="content">You can tell Git to split the Git internal information from the directory that it tracks, but we&#8217;ll ignore that for now: it&#8217;s not how normal projects work, and it&#8217;s really only meant for special uses. So the mental model of "the Git information is always tied directly to the working tree that it describes" may not be technically 100% accurate, but it&#8217;s a good model for all normal use.</td> </tr></table> </div> <div class="paragraph"><p>This has two implications:</p></div> <div class="ulist"><ul> <li> <p> if you grow bored with the tutorial repository you created (or you&#8217;ve made a mistake and want to start all over), you can just do simple </p> <div class="listingblock"> <div class="content"> <pre><code>$ rm -rf git-tutorial</code></pre> </div></div> <div class="paragraph"><p>and it will be gone. There&#8217;s no external repository, and there&#8217;s no history outside the project you created.</p></div> </li> <li> <p> if you want to move or duplicate a Git repository, you can do so. There is <em>git clone</em> command, but if all you want to do is just to create a copy of your repository (with all the full history that went along with it), you can do so with a regular <code>cp -a git-tutorial new-git-tutorial</code>. </p> <div class="paragraph"><p>Note that when you&#8217;ve moved or copied a Git repository, your Git index file (which caches various information, notably some of the "stat" information for the files involved) will likely need to be refreshed. So after you do a <code>cp -a</code> to create a new copy, you&#8217;ll want to do</p></div> <div class="listingblock"> <div class="content"> <pre><code>$ git update-index --refresh</code></pre> </div></div> <div class="paragraph"><p>in the new repository to make sure that the index file is up to date.</p></div> </li> </ul></div> <div class="paragraph"><p>Note that the second point is true even across machines. You can duplicate a remote Git repository with <strong>any</strong> regular copy mechanism, be it <em>scp</em>, <em>rsync</em> or <em>wget</em>.</p></div> <div class="paragraph"><p>When copying a remote repository, you&#8217;ll want to at a minimum update the index cache when you do this, and especially with other peoples' repositories you often want to make sure that the index cache is in some known state (you don&#8217;t know <strong>what</strong> they&#8217;ve done and not yet checked in), so usually you&#8217;ll precede the <em>git update-index</em> with a</p></div> <div clas