Autogenerated HTML docs for v1.7.7-290-gc9e7aa
[git/jnareb-git.git] / gittutorial.html
blob89236c38c0551890382e94bf38582ecb1c2b9edc
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6 <meta name="generator" content="AsciiDoc 8.5.2" />
7 <title>gittutorial(7)</title>
8 <style type="text/css">
9 /* Debug borders */
10 p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
12 border: 1px solid red;
16 body {
17 margin: 1em 5% 1em 5%;
20 a {
21 color: blue;
22 text-decoration: underline;
24 a:visited {
25 color: fuchsia;
28 em {
29 font-style: italic;
30 color: navy;
33 strong {
34 font-weight: bold;
35 color: #083194;
38 tt {
39 color: navy;
42 h1, h2, h3, h4, h5, h6 {
43 color: #527bbd;
44 font-family: sans-serif;
45 margin-top: 1.2em;
46 margin-bottom: 0.5em;
47 line-height: 1.3;
50 h1, h2, h3 {
51 border-bottom: 2px solid silver;
53 h2 {
54 padding-top: 0.5em;
56 h3 {
57 float: left;
59 h3 + * {
60 clear: left;
63 div.sectionbody {
64 font-family: serif;
65 margin-left: 0;
68 hr {
69 border: 1px solid silver;
72 p {
73 margin-top: 0.5em;
74 margin-bottom: 0.5em;
77 ul, ol, li > p {
78 margin-top: 0;
81 pre {
82 padding: 0;
83 margin: 0;
86 span#author {
87 color: #527bbd;
88 font-family: sans-serif;
89 font-weight: bold;
90 font-size: 1.1em;
92 span#email {
94 span#revnumber, span#revdate, span#revremark {
95 font-family: sans-serif;
98 div#footer {
99 font-family: sans-serif;
100 font-size: small;
101 border-top: 2px solid silver;
102 padding-top: 0.5em;
103 margin-top: 4.0em;
105 div#footer-text {
106 float: left;
107 padding-bottom: 0.5em;
109 div#footer-badges {
110 float: right;
111 padding-bottom: 0.5em;
114 div#preamble {
115 margin-top: 1.5em;
116 margin-bottom: 1.5em;
118 div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
119 div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
120 div.admonitionblock {
121 margin-top: 1.0em;
122 margin-bottom: 1.5em;
124 div.admonitionblock {
125 margin-top: 2.0em;
126 margin-bottom: 2.0em;
127 margin-right: 10%;
128 color: #606060;
131 div.content { /* Block element content. */
132 padding: 0;
135 /* Block element titles. */
136 div.title, caption.title {
137 color: #527bbd;
138 font-family: sans-serif;
139 font-weight: bold;
140 text-align: left;
141 margin-top: 1.0em;
142 margin-bottom: 0.5em;
144 div.title + * {
145 margin-top: 0;
148 td div.title:first-child {
149 margin-top: 0.0em;
151 div.content div.title:first-child {
152 margin-top: 0.0em;
154 div.content + div.title {
155 margin-top: 0.0em;
158 div.sidebarblock > div.content {
159 background: #ffffee;
160 border: 1px solid silver;
161 padding: 0.5em;
164 div.listingblock > div.content {
165 border: 1px solid silver;
166 background: #f4f4f4;
167 padding: 0.5em;
170 div.quoteblock, div.verseblock {
171 padding-left: 1.0em;
172 margin-left: 1.0em;
173 margin-right: 10%;
174 border-left: 5px solid #dddddd;
175 color: #777777;
178 div.quoteblock > div.attribution {
179 padding-top: 0.5em;
180 text-align: right;
183 div.verseblock > div.content {
184 white-space: pre;
186 div.verseblock > div.attribution {
187 padding-top: 0.75em;
188 text-align: left;
190 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
191 div.verseblock + div.attribution {
192 text-align: left;
195 div.admonitionblock .icon {
196 vertical-align: top;
197 font-size: 1.1em;
198 font-weight: bold;
199 text-decoration: underline;
200 color: #527bbd;
201 padding-right: 0.5em;
203 div.admonitionblock td.content {
204 padding-left: 0.5em;
205 border-left: 3px solid #dddddd;
208 div.exampleblock > div.content {
209 border-left: 3px solid #dddddd;
210 padding-left: 0.5em;
213 div.imageblock div.content { padding-left: 0; }
214 span.image img { border-style: none; }
215 a.image:visited { color: white; }
217 dl {
218 margin-top: 0.8em;
219 margin-bottom: 0.8em;
221 dt {
222 margin-top: 0.5em;
223 margin-bottom: 0;
224 font-style: normal;
225 color: navy;
227 dd > *:first-child {
228 margin-top: 0.1em;
231 ul, ol {
232 list-style-position: outside;
234 ol.arabic {
235 list-style-type: decimal;
237 ol.loweralpha {
238 list-style-type: lower-alpha;
240 ol.upperalpha {
241 list-style-type: upper-alpha;
243 ol.lowerroman {
244 list-style-type: lower-roman;
246 ol.upperroman {
247 list-style-type: upper-roman;
250 div.compact ul, div.compact ol,
251 div.compact p, div.compact p,
252 div.compact div, div.compact div {
253 margin-top: 0.1em;
254 margin-bottom: 0.1em;
257 div.tableblock > table {
258 border: 3px solid #527bbd;
260 thead, p.table.header {
261 font-family: sans-serif;
262 font-weight: bold;
264 tfoot {
265 font-weight: bold;
267 td > div.verse {
268 white-space: pre;
270 p.table {
271 margin-top: 0;
273 /* Because the table frame attribute is overriden by CSS in most browsers. */
274 div.tableblock > table[frame="void"] {
275 border-style: none;
277 div.tableblock > table[frame="hsides"] {
278 border-left-style: none;
279 border-right-style: none;
281 div.tableblock > table[frame="vsides"] {
282 border-top-style: none;
283 border-bottom-style: none;
287 div.hdlist {
288 margin-top: 0.8em;
289 margin-bottom: 0.8em;
291 div.hdlist tr {
292 padding-bottom: 15px;
294 dt.hdlist1.strong, td.hdlist1.strong {
295 font-weight: bold;
297 td.hdlist1 {
298 vertical-align: top;
299 font-style: normal;
300 padding-right: 0.8em;
301 color: navy;
303 td.hdlist2 {
304 vertical-align: top;
306 div.hdlist.compact tr {
307 margin: 0;
308 padding-bottom: 0;
311 .comment {
312 background: yellow;
315 .footnote, .footnoteref {
316 font-size: 0.8em;
319 span.footnote, span.footnoteref {
320 vertical-align: super;
323 #footnotes {
324 margin: 20px 0 20px 0;
325 padding: 7px 0 0 0;
328 #footnotes div.footnote {
329 margin: 0 0 5px 0;
332 #footnotes hr {
333 border: none;
334 border-top: 1px solid silver;
335 height: 1px;
336 text-align: left;
337 margin-left: 0;
338 width: 20%;
339 min-width: 100px;
343 @media print {
344 div#footer-badges { display: none; }
347 div#toc {
348 margin-bottom: 2.5em;
351 div#toctitle {
352 color: #527bbd;
353 font-family: sans-serif;
354 font-size: 1.1em;
355 font-weight: bold;
356 margin-top: 1.0em;
357 margin-bottom: 0.1em;
360 div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
361 margin-top: 0;
362 margin-bottom: 0;
364 div.toclevel2 {
365 margin-left: 2em;
366 font-size: 0.9em;
368 div.toclevel3 {
369 margin-left: 4em;
370 font-size: 0.9em;
372 div.toclevel4 {
373 margin-left: 6em;
374 font-size: 0.9em;
376 /* Overrides for manpage documents */
377 h1 {
378 padding-top: 0.5em;
379 padding-bottom: 0.5em;
380 border-top: 2px solid silver;
381 border-bottom: 2px solid silver;
383 h2 {
384 border-style: none;
386 div.sectionbody {
387 margin-left: 5%;
390 @media print {
391 div#toc { display: none; }
394 /* Workarounds for IE6's broken and incomplete CSS2. */
396 div.sidebar-content {
397 background: #ffffee;
398 border: 1px solid silver;
399 padding: 0.5em;
401 div.sidebar-title, div.image-title {
402 color: #527bbd;
403 font-family: sans-serif;
404 font-weight: bold;
405 margin-top: 0.0em;
406 margin-bottom: 0.5em;
409 div.listingblock div.content {
410 border: 1px solid silver;
411 background: #f4f4f4;
412 padding: 0.5em;
415 div.quoteblock-attribution {
416 padding-top: 0.5em;
417 text-align: right;
420 div.verseblock-content {
421 white-space: pre;
423 div.verseblock-attribution {
424 padding-top: 0.75em;
425 text-align: left;
428 div.exampleblock-content {
429 border-left: 3px solid #dddddd;
430 padding-left: 0.5em;
433 /* IE6 sets dynamically generated links as visited. */
434 div#toc a:visited { color: blue; }
435 </style>
436 <script type="text/javascript">
437 /*<![CDATA[*/
438 window.onload = function(){asciidoc.footnotes();}
439 var asciidoc = { // Namespace.
441 /////////////////////////////////////////////////////////////////////
442 // Table Of Contents generator
443 /////////////////////////////////////////////////////////////////////
445 /* Author: Mihai Bazon, September 2002
446 * http://students.infoiasi.ro/~mishoo
448 * Table Of Content generator
449 * Version: 0.4
451 * Feel free to use this script under the terms of the GNU General Public
452 * License, as long as you do not remove or alter this notice.
455 /* modified by Troy D. Hanson, September 2006. License: GPL */
456 /* modified by Stuart Rackham, 2006, 2009. License: GPL */
458 // toclevels = 1..4.
459 toc: function (toclevels) {
461 function getText(el) {
462 var text = "";
463 for (var i = el.firstChild; i != null; i = i.nextSibling) {
464 if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
465 text += i.data;
466 else if (i.firstChild != null)
467 text += getText(i);
469 return text;
472 function TocEntry(el, text, toclevel) {
473 this.element = el;
474 this.text = text;
475 this.toclevel = toclevel;
478 function tocEntries(el, toclevels) {
479 var result = new Array;
480 var re = new RegExp('[hH]([2-'+(toclevels+1)+'])');
481 // Function that scans the DOM tree for header elements (the DOM2
482 // nodeIterator API would be a better technique but not supported by all
483 // browsers).
484 var iterate = function (el) {
485 for (var i = el.firstChild; i != null; i = i.nextSibling) {
486 if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
487 var mo = re.exec(i.tagName);
488 if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
489 result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
491 iterate(i);
495 iterate(el);
496 return result;
499 var toc = document.getElementById("toc");
500 var entries = tocEntries(document.getElementById("content"), toclevels);
501 for (var i = 0; i < entries.length; ++i) {
502 var entry = entries[i];
503 if (entry.element.id == "")
504 entry.element.id = "_toc_" + i;
505 var a = document.createElement("a");
506 a.href = "#" + entry.element.id;
507 a.appendChild(document.createTextNode(entry.text));
508 var div = document.createElement("div");
509 div.appendChild(a);
510 div.className = "toclevel" + entry.toclevel;
511 toc.appendChild(div);
513 if (entries.length == 0)
514 toc.parentNode.removeChild(toc);
518 /////////////////////////////////////////////////////////////////////
519 // Footnotes generator
520 /////////////////////////////////////////////////////////////////////
522 /* Based on footnote generation code from:
523 * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
526 footnotes: function () {
527 var cont = document.getElementById("content");
528 var noteholder = document.getElementById("footnotes");
529 var spans = cont.getElementsByTagName("span");
530 var refs = {};
531 var n = 0;
532 for (i=0; i<spans.length; i++) {
533 if (spans[i].className == "footnote") {
534 n++;
535 // Use [\s\S] in place of . so multi-line matches work.
536 // Because JavaScript has no s (dotall) regex flag.
537 note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
538 noteholder.innerHTML +=
539 "<div class='footnote' id='_footnote_" + n + "'>" +
540 "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
541 n + "</a>. " + note + "</div>";
542 spans[i].innerHTML =
543 "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
544 "' title='View footnote' class='footnote'>" + n + "</a>]";
545 var id =spans[i].getAttribute("id");
546 if (id != null) refs["#"+id] = n;
549 if (n == 0)
550 noteholder.parentNode.removeChild(noteholder);
551 else {
552 // Process footnoterefs.
553 for (i=0; i<spans.length; i++) {
554 if (spans[i].className == "footnoteref") {
555 var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
556 href = href.match(/#.*/)[0]; // Because IE return full URL.
557 n = refs[href];
558 spans[i].innerHTML =
559 "[<a href='#_footnote_" + n +
560 "' title='View footnote' class='footnote'>" + n + "</a>]";
567 /*]]>*/
568 </script>
569 </head>
570 <body>
571 <div id="header">
572 <h1>
573 gittutorial(7) Manual Page
574 </h1>
575 <h2>NAME</h2>
576 <div class="sectionbody">
577 <p>gittutorial -
578 A tutorial introduction to git (for version 1.5.1 or newer)
579 </p>
580 </div>
581 </div>
582 <div id="content">
583 <h2 id="_synopsis">SYNOPSIS</h2>
584 <div class="sectionbody">
585 <div class="verseblock">
586 <div class="verseblock-content">git *</div>
587 <div class="verseblock-attribution">
588 </div></div>
589 </div>
590 <h2 id="_description">DESCRIPTION</h2>
591 <div class="sectionbody">
592 <div class="paragraph"><p>This tutorial explains how to import a new project into git, make
593 changes to it, and share changes with other developers.</p></div>
594 <div class="paragraph"><p>If you are instead primarily interested in using git to fetch a project,
595 for example, to test the latest version, you may prefer to start with
596 the first two chapters of <a href="user-manual.html">The Git User&#8217;s Manual</a>.</p></div>
597 <div class="paragraph"><p>First, note that you can get documentation for a command such as
598 <tt>git log --graph</tt> with:</p></div>
599 <div class="listingblock">
600 <div class="content">
601 <pre><tt>$ man git-log</tt></pre>
602 </div></div>
603 <div class="paragraph"><p>or:</p></div>
604 <div class="listingblock">
605 <div class="content">
606 <pre><tt>$ git help log</tt></pre>
607 </div></div>
608 <div class="paragraph"><p>With the latter, you can use the manual viewer of your choice; see
609 <a href="git-help.html">git-help(1)</a> for more information.</p></div>
610 <div class="paragraph"><p>It is a good idea to introduce yourself to git with your name and
611 public email address before doing any operation. The easiest
612 way to do so is:</p></div>
613 <div class="listingblock">
614 <div class="content">
615 <pre><tt>$ git config --global user.name "Your Name Comes Here"
616 $ git config --global user.email you@yourdomain.example.com</tt></pre>
617 </div></div>
618 </div>
619 <h2 id="_importing_a_new_project">Importing a new project</h2>
620 <div class="sectionbody">
621 <div class="paragraph"><p>Assume you have a tarball project.tar.gz with your initial work. You
622 can place it under git revision control as follows.</p></div>
623 <div class="listingblock">
624 <div class="content">
625 <pre><tt>$ tar xzf project.tar.gz
626 $ cd project
627 $ git init</tt></pre>
628 </div></div>
629 <div class="paragraph"><p>Git will reply</p></div>
630 <div class="listingblock">
631 <div class="content">
632 <pre><tt>Initialized empty Git repository in .git/</tt></pre>
633 </div></div>
634 <div class="paragraph"><p>You&#8217;ve now initialized the working directory&#8212;you may notice a new
635 directory created, named ".git".</p></div>
636 <div class="paragraph"><p>Next, tell git to take a snapshot of the contents of all files under the
637 current directory (note the <em>.</em>), with <em>git add</em>:</p></div>
638 <div class="listingblock">
639 <div class="content">
640 <pre><tt>$ git add .</tt></pre>
641 </div></div>
642 <div class="paragraph"><p>This snapshot is now stored in a temporary staging area which git calls
643 the "index". You can permanently store the contents of the index in the
644 repository with <em>git commit</em>:</p></div>
645 <div class="listingblock">
646 <div class="content">
647 <pre><tt>$ git commit</tt></pre>
648 </div></div>
649 <div class="paragraph"><p>This will prompt you for a commit message. You&#8217;ve now stored the first
650 version of your project in git.</p></div>
651 </div>
652 <h2 id="_making_changes">Making changes</h2>
653 <div class="sectionbody">
654 <div class="paragraph"><p>Modify some files, then add their updated contents to the index:</p></div>
655 <div class="listingblock">
656 <div class="content">
657 <pre><tt>$ git add file1 file2 file3</tt></pre>
658 </div></div>
659 <div class="paragraph"><p>You are now ready to commit. You can see what is about to be committed
660 using <em>git diff</em> with the --cached option:</p></div>
661 <div class="listingblock">
662 <div class="content">
663 <pre><tt>$ git diff --cached</tt></pre>
664 </div></div>
665 <div class="paragraph"><p>(Without --cached, <em>git diff</em> will show you any changes that
666 you&#8217;ve made but not yet added to the index.) You can also get a brief
667 summary of the situation with <em>git status</em>:</p></div>
668 <div class="listingblock">
669 <div class="content">
670 <pre><tt>$ git status
671 # On branch master
672 # Changes to be committed:
673 # (use "git reset HEAD &lt;file&gt;..." to unstage)
675 # modified: file1
676 # modified: file2
677 # modified: file3
678 #</tt></pre>
679 </div></div>
680 <div class="paragraph"><p>If you need to make any further adjustments, do so now, and then add any
681 newly modified content to the index. Finally, commit your changes with:</p></div>
682 <div class="listingblock">
683 <div class="content">
684 <pre><tt>$ git commit</tt></pre>
685 </div></div>
686 <div class="paragraph"><p>This will again prompt you for a message describing the change, and then
687 record a new version of the project.</p></div>
688 <div class="paragraph"><p>Alternatively, instead of running <em>git add</em> beforehand, you can use</p></div>
689 <div class="listingblock">
690 <div class="content">
691 <pre><tt>$ git commit -a</tt></pre>
692 </div></div>
693 <div class="paragraph"><p>which will automatically notice any modified (but not new) files, add
694 them to the index, and commit, all in one step.</p></div>
695 <div class="paragraph"><p>A note on commit messages: Though not required, it&#8217;s a good idea to
696 begin the commit message with a single short (less than 50 character)
697 line summarizing the change, followed by a blank line and then a more
698 thorough description. Tools that turn commits into email, for
699 example, use the first line on the Subject: line and the rest of the
700 commit in the body.</p></div>
701 </div>
702 <h2 id="_git_tracks_content_not_files">Git tracks content not files</h2>
703 <div class="sectionbody">
704 <div class="paragraph"><p>Many revision control systems provide an <tt>add</tt> command that tells the
705 system to start tracking changes to a new file. Git&#8217;s <tt>add</tt> command
706 does something simpler and more powerful: <em>git add</em> is used both for new
707 and newly modified files, and in both cases it takes a snapshot of the
708 given files and stages that content in the index, ready for inclusion in
709 the next commit.</p></div>
710 </div>
711 <h2 id="_viewing_project_history">Viewing project history</h2>
712 <div class="sectionbody">
713 <div class="paragraph"><p>At any point you can view the history of your changes using</p></div>
714 <div class="listingblock">
715 <div class="content">
716 <pre><tt>$ git log</tt></pre>
717 </div></div>
718 <div class="paragraph"><p>If you also want to see complete diffs at each step, use</p></div>
719 <div class="listingblock">
720 <div class="content">
721 <pre><tt>$ git log -p</tt></pre>
722 </div></div>
723 <div class="paragraph"><p>Often the overview of the change is useful to get a feel of
724 each step</p></div>
725 <div class="listingblock">
726 <div class="content">
727 <pre><tt>$ git log --stat --summary</tt></pre>
728 </div></div>
729 </div>
730 <h2 id="_managing_branches">Managing branches</h2>
731 <div class="sectionbody">
732 <div class="paragraph"><p>A single git repository can maintain multiple branches of
733 development. To create a new branch named "experimental", use</p></div>
734 <div class="listingblock">
735 <div class="content">
736 <pre><tt>$ git branch experimental</tt></pre>
737 </div></div>
738 <div class="paragraph"><p>If you now run</p></div>
739 <div class="listingblock">
740 <div class="content">
741 <pre><tt>$ git branch</tt></pre>
742 </div></div>
743 <div class="paragraph"><p>you&#8217;ll get a list of all existing branches:</p></div>
744 <div class="listingblock">
745 <div class="content">
746 <pre><tt> experimental
747 * master</tt></pre>
748 </div></div>
749 <div class="paragraph"><p>The "experimental" branch is the one you just created, and the
750 "master" branch is a default branch that was created for you
751 automatically. The asterisk marks the branch you are currently on;
752 type</p></div>
753 <div class="listingblock">
754 <div class="content">
755 <pre><tt>$ git checkout experimental</tt></pre>
756 </div></div>
757 <div class="paragraph"><p>to switch to the experimental branch. Now edit a file, commit the
758 change, and switch back to the master branch:</p></div>
759 <div class="listingblock">
760 <div class="content">
761 <pre><tt>(edit file)
762 $ git commit -a
763 $ git checkout master</tt></pre>
764 </div></div>
765 <div class="paragraph"><p>Check that the change you made is no longer visible, since it was
766 made on the experimental branch and you&#8217;re back on the master branch.</p></div>
767 <div class="paragraph"><p>You can make a different change on the master branch:</p></div>
768 <div class="listingblock">
769 <div class="content">
770 <pre><tt>(edit file)
771 $ git commit -a</tt></pre>
772 </div></div>
773 <div class="paragraph"><p>at this point the two branches have diverged, with different changes
774 made in each. To merge the changes made in experimental into master, run</p></div>
775 <div class="listingblock">
776 <div class="content">
777 <pre><tt>$ git merge experimental</tt></pre>
778 </div></div>
779 <div class="paragraph"><p>If the changes don&#8217;t conflict, you&#8217;re done. If there are conflicts,
780 markers will be left in the problematic files showing the conflict;</p></div>
781 <div class="listingblock">
782 <div class="content">
783 <pre><tt>$ git diff</tt></pre>
784 </div></div>
785 <div class="paragraph"><p>will show this. Once you&#8217;ve edited the files to resolve the
786 conflicts,</p></div>
787 <div class="listingblock">
788 <div class="content">
789 <pre><tt>$ git commit -a</tt></pre>
790 </div></div>
791 <div class="paragraph"><p>will commit the result of the merge. Finally,</p></div>
792 <div class="listingblock">
793 <div class="content">
794 <pre><tt>$ gitk</tt></pre>
795 </div></div>
796 <div class="paragraph"><p>will show a nice graphical representation of the resulting history.</p></div>
797 <div class="paragraph"><p>At this point you could delete the experimental branch with</p></div>
798 <div class="listingblock">
799 <div class="content">
800 <pre><tt>$ git branch -d experimental</tt></pre>
801 </div></div>
802 <div class="paragraph"><p>This command ensures that the changes in the experimental branch are
803 already in the current branch.</p></div>
804 <div class="paragraph"><p>If you develop on a branch crazy-idea, then regret it, you can always
805 delete the branch with</p></div>
806 <div class="listingblock">
807 <div class="content">
808 <pre><tt>$ git branch -D crazy-idea</tt></pre>
809 </div></div>
810 <div class="paragraph"><p>Branches are cheap and easy, so this is a good way to try something
811 out.</p></div>
812 </div>
813 <h2 id="_using_git_for_collaboration">Using git for collaboration</h2>
814 <div class="sectionbody">
815 <div class="paragraph"><p>Suppose that Alice has started a new project with a git repository in
816 /home/alice/project, and that Bob, who has a home directory on the
817 same machine, wants to contribute.</p></div>
818 <div class="paragraph"><p>Bob begins with:</p></div>
819 <div class="listingblock">
820 <div class="content">
821 <pre><tt>bob$ git clone /home/alice/project myrepo</tt></pre>
822 </div></div>
823 <div class="paragraph"><p>This creates a new directory "myrepo" containing a clone of Alice&#8217;s
824 repository. The clone is on an equal footing with the original
825 project, possessing its own copy of the original project&#8217;s history.</p></div>
826 <div class="paragraph"><p>Bob then makes some changes and commits them:</p></div>
827 <div class="listingblock">
828 <div class="content">
829 <pre><tt>(edit files)
830 bob$ git commit -a
831 (repeat as necessary)</tt></pre>
832 </div></div>
833 <div class="paragraph"><p>When he&#8217;s ready, he tells Alice to pull changes from the repository
834 at /home/bob/myrepo. She does this with:</p></div>
835 <div class="listingblock">
836 <div class="content">
837 <pre><tt>alice$ cd /home/alice/project
838 alice$ git pull /home/bob/myrepo master</tt></pre>
839 </div></div>
840 <div class="paragraph"><p>This merges the changes from Bob&#8217;s "master" branch into Alice&#8217;s
841 current branch. If Alice has made her own changes in the meantime,
842 then she may need to manually fix any conflicts.</p></div>
843 <div class="paragraph"><p>The "pull" command thus performs two operations: it fetches changes
844 from a remote branch, then merges them into the current branch.</p></div>
845 <div class="paragraph"><p>Note that in general, Alice would want her local changes committed before
846 initiating this "pull". If Bob&#8217;s work conflicts with what Alice did since
847 their histories forked, Alice will use her working tree and the index to
848 resolve conflicts, and existing local changes will interfere with the
849 conflict resolution process (git will still perform the fetch but will
850 refuse to merge --- Alice will have to get rid of her local changes in
851 some way and pull again when this happens).</p></div>
852 <div class="paragraph"><p>Alice can peek at what Bob did without merging first, using the "fetch"
853 command; this allows Alice to inspect what Bob did, using a special
854 symbol "FETCH_HEAD", in order to determine if he has anything worth
855 pulling, like this:</p></div>
856 <div class="listingblock">
857 <div class="content">
858 <pre><tt>alice$ git fetch /home/bob/myrepo master
859 alice$ git log -p HEAD..FETCH_HEAD</tt></pre>
860 </div></div>
861 <div class="paragraph"><p>This operation is safe even if Alice has uncommitted local changes.
862 The range notation "HEAD..FETCH_HEAD" means "show everything that is reachable
863 from the FETCH_HEAD but exclude anything that is reachable from HEAD".
864 Alice already knows everything that leads to her current state (HEAD),
865 and reviews what Bob has in his state (FETCH_HEAD) that she has not
866 seen with this command.</p></div>
867 <div class="paragraph"><p>If Alice wants to visualize what Bob did since their histories forked
868 she can issue the following command:</p></div>
869 <div class="listingblock">
870 <div class="content">
871 <pre><tt>$ gitk HEAD..FETCH_HEAD</tt></pre>
872 </div></div>
873 <div class="paragraph"><p>This uses the same two-dot range notation we saw earlier with <em>git log</em>.</p></div>
874 <div class="paragraph"><p>Alice may want to view what both of them did since they forked.
875 She can use three-dot form instead of the two-dot form:</p></div>
876 <div class="listingblock">
877 <div class="content">
878 <pre><tt>$ gitk HEAD...FETCH_HEAD</tt></pre>
879 </div></div>
880 <div class="paragraph"><p>This means "show everything that is reachable from either one, but
881 exclude anything that is reachable from both of them".</p></div>
882 <div class="paragraph"><p>Please note that these range notation can be used with both gitk
883 and "git log".</p></div>
884 <div class="paragraph"><p>After inspecting what Bob did, if there is nothing urgent, Alice may
885 decide to continue working without pulling from Bob. If Bob&#8217;s history
886 does have something Alice would immediately need, Alice may choose to
887 stash her work-in-progress first, do a "pull", and then finally unstash
888 her work-in-progress on top of the resulting history.</p></div>
889 <div class="paragraph"><p>When you are working in a small closely knit group, it is not
890 unusual to interact with the same repository over and over
891 again. By defining <em>remote</em> repository shorthand, you can make
892 it easier:</p></div>
893 <div class="listingblock">
894 <div class="content">
895 <pre><tt>alice$ git remote add bob /home/bob/myrepo</tt></pre>
896 </div></div>
897 <div class="paragraph"><p>With this, Alice can perform the first part of the "pull" operation
898 alone using the <em>git fetch</em> command without merging them with her own
899 branch, using:</p></div>
900 <div class="listingblock">
901 <div class="content">
902 <pre><tt>alice$ git fetch bob</tt></pre>
903 </div></div>
904 <div class="paragraph"><p>Unlike the longhand form, when Alice fetches from Bob using a
905 remote repository shorthand set up with <em>git remote</em>, what was
906 fetched is stored in a remote-tracking branch, in this case
907 <tt>bob/master</tt>. So after this:</p></div>
908 <div class="listingblock">
909 <div class="content">
910 <pre><tt>alice$ git log -p master..bob/master</tt></pre>
911 </div></div>
912 <div class="paragraph"><p>shows a list of all the changes that Bob made since he branched from
913 Alice&#8217;s master branch.</p></div>
914 <div class="paragraph"><p>After examining those changes, Alice
915 could merge the changes into her master branch:</p></div>
916 <div class="listingblock">
917 <div class="content">
918 <pre><tt>alice$ git merge bob/master</tt></pre>
919 </div></div>
920 <div class="paragraph"><p>This <tt>merge</tt> can also be done by <em>pulling from her own remote-tracking
921 branch</em>, like this:</p></div>
922 <div class="listingblock">
923 <div class="content">
924 <pre><tt>alice$ git pull . remotes/bob/master</tt></pre>
925 </div></div>
926 <div class="paragraph"><p>Note that git pull always merges into the current branch,
927 regardless of what else is given on the command line.</p></div>
928 <div class="paragraph"><p>Later, Bob can update his repo with Alice&#8217;s latest changes using</p></div>
929 <div class="listingblock">
930 <div class="content">
931 <pre><tt>bob$ git pull</tt></pre>
932 </div></div>
933 <div class="paragraph"><p>Note that he doesn&#8217;t need to give the path to Alice&#8217;s repository;
934 when Bob cloned Alice&#8217;s repository, git stored the location of her
935 repository in the repository configuration, and that location is
936 used for pulls:</p></div>
937 <div class="listingblock">
938 <div class="content">
939 <pre><tt>bob$ git config --get remote.origin.url
940 /home/alice/project</tt></pre>
941 </div></div>
942 <div class="paragraph"><p>(The complete configuration created by <em>git clone</em> is visible using
943 <tt>git config -l</tt>, and the <a href="git-config.html">git-config(1)</a> man page
944 explains the meaning of each option.)</p></div>
945 <div class="paragraph"><p>Git also keeps a pristine copy of Alice&#8217;s master branch under the
946 name "origin/master":</p></div>
947 <div class="listingblock">
948 <div class="content">
949 <pre><tt>bob$ git branch -r
950 origin/master</tt></pre>
951 </div></div>
952 <div class="paragraph"><p>If Bob later decides to work from a different host, he can still
953 perform clones and pulls using the ssh protocol:</p></div>
954 <div class="listingblock">
955 <div class="content">
956 <pre><tt>bob$ git clone alice.org:/home/alice/project myrepo</tt></pre>
957 </div></div>
958 <div class="paragraph"><p>Alternatively, git has a native protocol, or can use rsync or http;
959 see <a href="git-pull.html">git-pull(1)</a> for details.</p></div>
960 <div class="paragraph"><p>Git can also be used in a CVS-like mode, with a central repository
961 that various users push changes to; see <a href="git-push.html">git-push(1)</a> and
962 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>.</p></div>
963 </div>
964 <h2 id="_exploring_history">Exploring history</h2>
965 <div class="sectionbody">
966 <div class="paragraph"><p>Git history is represented as a series of interrelated commits. We
967 have already seen that the <em>git log</em> command can list those commits.
968 Note that first line of each git log entry also gives a name for the
969 commit:</p></div>
970 <div class="listingblock">
971 <div class="content">
972 <pre><tt>$ git log
973 commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
974 Author: Junio C Hamano &lt;junkio@cox.net&gt;
975 Date: Tue May 16 17:18:22 2006 -0700
977 merge-base: Clarify the comments on post processing.</tt></pre>
978 </div></div>
979 <div class="paragraph"><p>We can give this name to <em>git show</em> to see the details about this
980 commit.</p></div>
981 <div class="listingblock">
982 <div class="content">
983 <pre><tt>$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7</tt></pre>
984 </div></div>
985 <div class="paragraph"><p>But there are other ways to refer to commits. You can use any initial
986 part of the name that is long enough to uniquely identify the commit:</p></div>
987 <div class="listingblock">
988 <div class="content">
989 <pre><tt>$ git show c82a22c39c # the first few characters of the name are
990 # usually enough
991 $ git show HEAD # the tip of the current branch
992 $ git show experimental # the tip of the "experimental" branch</tt></pre>
993 </div></div>
994 <div class="paragraph"><p>Every commit usually has one "parent" commit
995 which points to the previous state of the project:</p></div>
996 <div class="listingblock">
997 <div class="content">
998 <pre><tt>$ git show HEAD^ # to see the parent of HEAD
999 $ git show HEAD^^ # to see the grandparent of HEAD
1000 $ git show HEAD~4 # to see the great-great grandparent of HEAD</tt></pre>
1001 </div></div>
1002 <div class="paragraph"><p>Note that merge commits may have more than one parent:</p></div>
1003 <div class="listingblock">
1004 <div class="content">
1005 <pre><tt>$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
1006 $ git show HEAD^2 # show the second parent of HEAD</tt></pre>
1007 </div></div>
1008 <div class="paragraph"><p>You can also give commits names of your own; after running</p></div>
1009 <div class="listingblock">
1010 <div class="content">
1011 <pre><tt>$ git tag v2.5 1b2e1d63ff</tt></pre>
1012 </div></div>
1013 <div class="paragraph"><p>you can refer to 1b2e1d63ff by the name "v2.5". If you intend to
1014 share this name with other people (for example, to identify a release
1015 version), you should create a "tag" object, and perhaps sign it; see
1016 <a href="git-tag.html">git-tag(1)</a> for details.</p></div>
1017 <div class="paragraph"><p>Any git command that needs to know a commit can take any of these
1018 names. For example:</p></div>
1019 <div class="listingblock">
1020 <div class="content">
1021 <pre><tt>$ git diff v2.5 HEAD # compare the current HEAD to v2.5
1022 $ git branch stable v2.5 # start a new branch named "stable" based
1023 # at v2.5
1024 $ git reset --hard HEAD^ # reset your current branch and working
1025 # directory to its state at HEAD^</tt></pre>
1026 </div></div>
1027 <div class="paragraph"><p>Be careful with that last command: in addition to losing any changes
1028 in the working directory, it will also remove all later commits from
1029 this branch. If this branch is the only branch containing those
1030 commits, they will be lost. Also, don&#8217;t use <em>git reset</em> on a
1031 publicly-visible branch that other developers pull from, as it will
1032 force needless merges on other developers to clean up the history.
1033 If you need to undo changes that you have pushed, use <em>git revert</em>
1034 instead.</p></div>
1035 <div class="paragraph"><p>The <em>git grep</em> command can search for strings in any version of your
1036 project, so</p></div>
1037 <div class="listingblock">
1038 <div class="content">
1039 <pre><tt>$ git grep "hello" v2.5</tt></pre>
1040 </div></div>
1041 <div class="paragraph"><p>searches for all occurrences of "hello" in v2.5.</p></div>
1042 <div class="paragraph"><p>If you leave out the commit name, <em>git grep</em> will search any of the
1043 files it manages in your current directory. So</p></div>
1044 <div class="listingblock">
1045 <div class="content">
1046 <pre><tt>$ git grep "hello"</tt></pre>
1047 </div></div>
1048 <div class="paragraph"><p>is a quick way to search just the files that are tracked by git.</p></div>
1049 <div class="paragraph"><p>Many git commands also take sets of commits, which can be specified
1050 in a number of ways. Here are some examples with <em>git log</em>:</p></div>
1051 <div class="listingblock">
1052 <div class="content">
1053 <pre><tt>$ git log v2.5..v2.6 # commits between v2.5 and v2.6
1054 $ git log v2.5.. # commits since v2.5
1055 $ git log --since="2 weeks ago" # commits from the last 2 weeks
1056 $ git log v2.5.. Makefile # commits since v2.5 which modify
1057 # Makefile</tt></pre>
1058 </div></div>
1059 <div class="paragraph"><p>You can also give <em>git log</em> a "range" of commits where the first is not
1060 necessarily an ancestor of the second; for example, if the tips of
1061 the branches "stable" and "master" diverged from a common
1062 commit some time ago, then</p></div>
1063 <div class="listingblock">
1064 <div class="content">
1065 <pre><tt>$ git log stable..master</tt></pre>
1066 </div></div>
1067 <div class="paragraph"><p>will list commits made in the master branch but not in the
1068 stable branch, while</p></div>
1069 <div class="listingblock">
1070 <div class="content">
1071 <pre><tt>$ git log master..stable</tt></pre>
1072 </div></div>
1073 <div class="paragraph"><p>will show the list of commits made on the stable branch but not
1074 the master branch.</p></div>
1075 <div class="paragraph"><p>The <em>git log</em> command has a weakness: it must present commits in a
1076 list. When the history has lines of development that diverged and
1077 then merged back together, the order in which <em>git log</em> presents
1078 those commits is meaningless.</p></div>
1079 <div class="paragraph"><p>Most projects with multiple contributors (such as the Linux kernel,
1080 or git itself) have frequent merges, and <em>gitk</em> does a better job of
1081 visualizing their history. For example,</p></div>
1082 <div class="listingblock">
1083 <div class="content">
1084 <pre><tt>$ gitk --since="2 weeks ago" drivers/</tt></pre>
1085 </div></div>
1086 <div class="paragraph"><p>allows you to browse any commits from the last 2 weeks of commits
1087 that modified files under the "drivers" directory. (Note: you can
1088 adjust gitk&#8217;s fonts by holding down the control key while pressing
1089 "-" or "+".)</p></div>
1090 <div class="paragraph"><p>Finally, most commands that take filenames will optionally allow you
1091 to precede any filename by a commit, to specify a particular version
1092 of the file:</p></div>
1093 <div class="listingblock">
1094 <div class="content">
1095 <pre><tt>$ git diff v2.5:Makefile HEAD:Makefile.in</tt></pre>
1096 </div></div>
1097 <div class="paragraph"><p>You can also use <em>git show</em> to see any such file:</p></div>
1098 <div class="listingblock">
1099 <div class="content">
1100 <pre><tt>$ git show v2.5:Makefile</tt></pre>
1101 </div></div>
1102 </div>
1103 <h2 id="_next_steps">Next Steps</h2>
1104 <div class="sectionbody">
1105 <div class="paragraph"><p>This tutorial should be enough to perform basic distributed revision
1106 control for your projects. However, to fully understand the depth
1107 and power of git you need to understand two simple ideas on which it
1108 is based:</p></div>
1109 <div class="ulist"><ul>
1110 <li>
1112 The object database is the rather elegant system used to
1113 store the history of your project&#8212;files, directories, and
1114 commits.
1115 </p>
1116 </li>
1117 <li>
1119 The index file is a cache of the state of a directory tree,
1120 used to create commits, check out working directories, and
1121 hold the various trees involved in a merge.
1122 </p>
1123 </li>
1124 </ul></div>
1125 <div class="paragraph"><p>Part two of this tutorial explains the object
1126 database, the index file, and a few other odds and ends that you&#8217;ll
1127 need to make the most of git. You can find it at <a href="gittutorial-2.html">gittutorial-2(7)</a>.</p></div>
1128 <div class="paragraph"><p>If you don&#8217;t want to continue with that right away, a few other
1129 digressions that may be interesting at this point are:</p></div>
1130 <div class="ulist"><ul>
1131 <li>
1133 <a href="git-format-patch.html">git-format-patch(1)</a>, <a href="git-am.html">git-am(1)</a>: These convert
1134 series of git commits into emailed patches, and vice versa,
1135 useful for projects such as the Linux kernel which rely heavily
1136 on emailed patches.
1137 </p>
1138 </li>
1139 <li>
1141 <a href="git-bisect.html">git-bisect(1)</a>: When there is a regression in your
1142 project, one way to track down the bug is by searching through
1143 the history to find the exact commit that&#8217;s to blame. Git bisect
1144 can help you perform a binary search for that commit. It is
1145 smart enough to perform a close-to-optimal search even in the
1146 case of complex non-linear history with lots of merged branches.
1147 </p>
1148 </li>
1149 <li>
1151 <a href="gitworkflows.html">gitworkflows(7)</a>: Gives an overview of recommended
1152 workflows.
1153 </p>
1154 </li>
1155 <li>
1157 <a href="everyday.html">Everyday GIT with 20 Commands Or So</a>
1158 </p>
1159 </li>
1160 <li>
1162 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>: Git for CVS users.
1163 </p>
1164 </li>
1165 </ul></div>
1166 </div>
1167 <h2 id="_see_also">SEE ALSO</h2>
1168 <div class="sectionbody">
1169 <div class="paragraph"><p><a href="gittutorial-2.html">gittutorial-2(7)</a>,
1170 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>,
1171 <a href="gitcore-tutorial.html">gitcore-tutorial(7)</a>,
1172 <a href="gitglossary.html">gitglossary(7)</a>,
1173 <a href="git-help.html">git-help(1)</a>,
1174 <a href="gitworkflows.html">gitworkflows(7)</a>,
1175 <a href="everyday.html">Everyday git</a>,
1176 <a href="user-manual.html">The Git User&#8217;s Manual</a></p></div>
1177 </div>
1178 <h2 id="_git">GIT</h2>
1179 <div class="sectionbody">
1180 <div class="paragraph"><p>Part of the <a href="git.html">git(1)</a> suite.</p></div>
1181 </div>
1182 </div>
1183 <div id="footnotes"><hr /></div>
1184 <div id="footer">
1185 <div id="footer-text">
1186 Last updated 2011-09-21 23:01:14 PDT
1187 </div>
1188 </div>
1189 </body>
1190 </html>