Autogenerated HTML docs for v1.7.6-472-g4bfe7
[git/jnareb-git.git] / gittutorial.html
blob26e9d619ffc3a3db7137ccd0def316c2d875c2a2
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.4.5" />
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.5em;
122 margin-bottom: 1.5em;
124 div.admonitionblock {
125 margin-top: 2.5em;
126 margin-bottom: 2.5em;
129 div.content { /* Block element content. */
130 padding: 0;
133 /* Block element titles. */
134 div.title, caption.title {
135 color: #527bbd;
136 font-family: sans-serif;
137 font-weight: bold;
138 text-align: left;
139 margin-top: 1.0em;
140 margin-bottom: 0.5em;
142 div.title + * {
143 margin-top: 0;
146 td div.title:first-child {
147 margin-top: 0.0em;
149 div.content div.title:first-child {
150 margin-top: 0.0em;
152 div.content + div.title {
153 margin-top: 0.0em;
156 div.sidebarblock > div.content {
157 background: #ffffee;
158 border: 1px solid silver;
159 padding: 0.5em;
162 div.listingblock > div.content {
163 border: 1px solid silver;
164 background: #f4f4f4;
165 padding: 0.5em;
168 div.quoteblock {
169 padding-left: 2.0em;
170 margin-right: 10%;
172 div.quoteblock > div.attribution {
173 padding-top: 0.5em;
174 text-align: right;
177 div.verseblock {
178 padding-left: 2.0em;
179 margin-right: 10%;
181 div.verseblock > div.content {
182 white-space: pre;
184 div.verseblock > div.attribution {
185 padding-top: 0.75em;
186 text-align: left;
188 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
189 div.verseblock + div.attribution {
190 text-align: left;
193 div.admonitionblock .icon {
194 vertical-align: top;
195 font-size: 1.1em;
196 font-weight: bold;
197 text-decoration: underline;
198 color: #527bbd;
199 padding-right: 0.5em;
201 div.admonitionblock td.content {
202 padding-left: 0.5em;
203 border-left: 2px solid silver;
206 div.exampleblock > div.content {
207 border-left: 2px solid silver;
208 padding: 0.5em;
211 div.imageblock div.content { padding-left: 0; }
212 span.image img { border-style: none; }
213 a.image:visited { color: white; }
215 dl {
216 margin-top: 0.8em;
217 margin-bottom: 0.8em;
219 dt {
220 margin-top: 0.5em;
221 margin-bottom: 0;
222 font-style: normal;
223 color: navy;
225 dd > *:first-child {
226 margin-top: 0.1em;
229 ul, ol {
230 list-style-position: outside;
232 ol.arabic {
233 list-style-type: decimal;
235 ol.loweralpha {
236 list-style-type: lower-alpha;
238 ol.upperalpha {
239 list-style-type: upper-alpha;
241 ol.lowerroman {
242 list-style-type: lower-roman;
244 ol.upperroman {
245 list-style-type: upper-roman;
248 div.compact ul, div.compact ol,
249 div.compact p, div.compact p,
250 div.compact div, div.compact div {
251 margin-top: 0.1em;
252 margin-bottom: 0.1em;
255 div.tableblock > table {
256 border: 3px solid #527bbd;
258 thead {
259 font-family: sans-serif;
260 font-weight: bold;
262 tfoot {
263 font-weight: bold;
265 td > div.verse {
266 white-space: pre;
268 p.table {
269 margin-top: 0;
271 /* Because the table frame attribute is overriden by CSS in most browsers. */
272 div.tableblock > table[frame="void"] {
273 border-style: none;
275 div.tableblock > table[frame="hsides"] {
276 border-left-style: none;
277 border-right-style: none;
279 div.tableblock > table[frame="vsides"] {
280 border-top-style: none;
281 border-bottom-style: none;
285 div.hdlist {
286 margin-top: 0.8em;
287 margin-bottom: 0.8em;
289 div.hdlist tr {
290 padding-bottom: 15px;
292 dt.hdlist1.strong, td.hdlist1.strong {
293 font-weight: bold;
295 td.hdlist1 {
296 vertical-align: top;
297 font-style: normal;
298 padding-right: 0.8em;
299 color: navy;
301 td.hdlist2 {
302 vertical-align: top;
304 div.hdlist.compact tr {
305 margin: 0;
306 padding-bottom: 0;
309 .comment {
310 background: yellow;
313 @media print {
314 div#footer-badges { display: none; }
317 div#toctitle {
318 color: #527bbd;
319 font-family: sans-serif;
320 font-size: 1.1em;
321 font-weight: bold;
322 margin-top: 1.0em;
323 margin-bottom: 0.1em;
326 div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
327 margin-top: 0;
328 margin-bottom: 0;
330 div.toclevel2 {
331 margin-left: 2em;
332 font-size: 0.9em;
334 div.toclevel3 {
335 margin-left: 4em;
336 font-size: 0.9em;
338 div.toclevel4 {
339 margin-left: 6em;
340 font-size: 0.9em;
342 /* Overrides for manpage documents */
343 h1 {
344 padding-top: 0.5em;
345 padding-bottom: 0.5em;
346 border-top: 2px solid silver;
347 border-bottom: 2px solid silver;
349 h2 {
350 border-style: none;
352 div.sectionbody {
353 margin-left: 5%;
356 @media print {
357 div#toc { display: none; }
360 /* Workarounds for IE6's broken and incomplete CSS2. */
362 div.sidebar-content {
363 background: #ffffee;
364 border: 1px solid silver;
365 padding: 0.5em;
367 div.sidebar-title, div.image-title {
368 color: #527bbd;
369 font-family: sans-serif;
370 font-weight: bold;
371 margin-top: 0.0em;
372 margin-bottom: 0.5em;
375 div.listingblock div.content {
376 border: 1px solid silver;
377 background: #f4f4f4;
378 padding: 0.5em;
381 div.quoteblock-attribution {
382 padding-top: 0.5em;
383 text-align: right;
386 div.verseblock-content {
387 white-space: pre;
389 div.verseblock-attribution {
390 padding-top: 0.75em;
391 text-align: left;
394 div.exampleblock-content {
395 border-left: 2px solid silver;
396 padding-left: 0.5em;
399 /* IE6 sets dynamically generated links as visited. */
400 div#toc a:visited { color: blue; }
401 </style>
402 </head>
403 <body>
404 <div id="header">
405 <h1>
406 gittutorial(7) Manual Page
407 </h1>
408 <h2>NAME</h2>
409 <div class="sectionbody">
410 <p>gittutorial -
411 A tutorial introduction to git (for version 1.5.1 or newer)
412 </p>
413 </div>
414 </div>
415 <h2 id="_synopsis">SYNOPSIS</h2>
416 <div class="sectionbody">
417 <div class="verseblock">
418 <div class="verseblock-content">git *</div>
419 <div class="verseblock-attribution">
420 </div></div>
421 </div>
422 <h2 id="_description">DESCRIPTION</h2>
423 <div class="sectionbody">
424 <div class="paragraph"><p>This tutorial explains how to import a new project into git, make
425 changes to it, and share changes with other developers.</p></div>
426 <div class="paragraph"><p>If you are instead primarily interested in using git to fetch a project,
427 for example, to test the latest version, you may prefer to start with
428 the first two chapters of <a href="user-manual.html">The Git User&#8217;s Manual</a>.</p></div>
429 <div class="paragraph"><p>First, note that you can get documentation for a command such as
430 <tt>git log --graph</tt> with:</p></div>
431 <div class="listingblock">
432 <div class="content">
433 <pre><tt>$ man git-log</tt></pre>
434 </div></div>
435 <div class="paragraph"><p>or:</p></div>
436 <div class="listingblock">
437 <div class="content">
438 <pre><tt>$ git help log</tt></pre>
439 </div></div>
440 <div class="paragraph"><p>With the latter, you can use the manual viewer of your choice; see
441 <a href="git-help.html">git-help(1)</a> for more information.</p></div>
442 <div class="paragraph"><p>It is a good idea to introduce yourself to git with your name and
443 public email address before doing any operation. The easiest
444 way to do so is:</p></div>
445 <div class="listingblock">
446 <div class="content">
447 <pre><tt>$ git config --global user.name "Your Name Comes Here"
448 $ git config --global user.email you@yourdomain.example.com</tt></pre>
449 </div></div>
450 </div>
451 <h2 id="_importing_a_new_project">Importing a new project</h2>
452 <div class="sectionbody">
453 <div class="paragraph"><p>Assume you have a tarball project.tar.gz with your initial work. You
454 can place it under git revision control as follows.</p></div>
455 <div class="listingblock">
456 <div class="content">
457 <pre><tt>$ tar xzf project.tar.gz
458 $ cd project
459 $ git init</tt></pre>
460 </div></div>
461 <div class="paragraph"><p>Git will reply</p></div>
462 <div class="listingblock">
463 <div class="content">
464 <pre><tt>Initialized empty Git repository in .git/</tt></pre>
465 </div></div>
466 <div class="paragraph"><p>You&#8217;ve now initialized the working directory&#8212;you may notice a new
467 directory created, named ".git".</p></div>
468 <div class="paragraph"><p>Next, tell git to take a snapshot of the contents of all files under the
469 current directory (note the <em>.</em>), with <em>git add</em>:</p></div>
470 <div class="listingblock">
471 <div class="content">
472 <pre><tt>$ git add .</tt></pre>
473 </div></div>
474 <div class="paragraph"><p>This snapshot is now stored in a temporary staging area which git calls
475 the "index". You can permanently store the contents of the index in the
476 repository with <em>git commit</em>:</p></div>
477 <div class="listingblock">
478 <div class="content">
479 <pre><tt>$ git commit</tt></pre>
480 </div></div>
481 <div class="paragraph"><p>This will prompt you for a commit message. You&#8217;ve now stored the first
482 version of your project in git.</p></div>
483 </div>
484 <h2 id="_making_changes">Making changes</h2>
485 <div class="sectionbody">
486 <div class="paragraph"><p>Modify some files, then add their updated contents to the index:</p></div>
487 <div class="listingblock">
488 <div class="content">
489 <pre><tt>$ git add file1 file2 file3</tt></pre>
490 </div></div>
491 <div class="paragraph"><p>You are now ready to commit. You can see what is about to be committed
492 using <em>git diff</em> with the --cached option:</p></div>
493 <div class="listingblock">
494 <div class="content">
495 <pre><tt>$ git diff --cached</tt></pre>
496 </div></div>
497 <div class="paragraph"><p>(Without --cached, <em>git diff</em> will show you any changes that
498 you&#8217;ve made but not yet added to the index.) You can also get a brief
499 summary of the situation with <em>git status</em>:</p></div>
500 <div class="listingblock">
501 <div class="content">
502 <pre><tt>$ git status
503 # On branch master
504 # Changes to be committed:
505 # (use "git reset HEAD &lt;file&gt;..." to unstage)
507 # modified: file1
508 # modified: file2
509 # modified: file3
510 #</tt></pre>
511 </div></div>
512 <div class="paragraph"><p>If you need to make any further adjustments, do so now, and then add any
513 newly modified content to the index. Finally, commit your changes with:</p></div>
514 <div class="listingblock">
515 <div class="content">
516 <pre><tt>$ git commit</tt></pre>
517 </div></div>
518 <div class="paragraph"><p>This will again prompt you for a message describing the change, and then
519 record a new version of the project.</p></div>
520 <div class="paragraph"><p>Alternatively, instead of running <em>git add</em> beforehand, you can use</p></div>
521 <div class="listingblock">
522 <div class="content">
523 <pre><tt>$ git commit -a</tt></pre>
524 </div></div>
525 <div class="paragraph"><p>which will automatically notice any modified (but not new) files, add
526 them to the index, and commit, all in one step.</p></div>
527 <div class="paragraph"><p>A note on commit messages: Though not required, it&#8217;s a good idea to
528 begin the commit message with a single short (less than 50 character)
529 line summarizing the change, followed by a blank line and then a more
530 thorough description. Tools that turn commits into email, for
531 example, use the first line on the Subject: line and the rest of the
532 commit in the body.</p></div>
533 </div>
534 <h2 id="_git_tracks_content_not_files">Git tracks content not files</h2>
535 <div class="sectionbody">
536 <div class="paragraph"><p>Many revision control systems provide an <tt>add</tt> command that tells the
537 system to start tracking changes to a new file. Git&#8217;s <tt>add</tt> command
538 does something simpler and more powerful: <em>git add</em> is used both for new
539 and newly modified files, and in both cases it takes a snapshot of the
540 given files and stages that content in the index, ready for inclusion in
541 the next commit.</p></div>
542 </div>
543 <h2 id="_viewing_project_history">Viewing project history</h2>
544 <div class="sectionbody">
545 <div class="paragraph"><p>At any point you can view the history of your changes using</p></div>
546 <div class="listingblock">
547 <div class="content">
548 <pre><tt>$ git log</tt></pre>
549 </div></div>
550 <div class="paragraph"><p>If you also want to see complete diffs at each step, use</p></div>
551 <div class="listingblock">
552 <div class="content">
553 <pre><tt>$ git log -p</tt></pre>
554 </div></div>
555 <div class="paragraph"><p>Often the overview of the change is useful to get a feel of
556 each step</p></div>
557 <div class="listingblock">
558 <div class="content">
559 <pre><tt>$ git log --stat --summary</tt></pre>
560 </div></div>
561 </div>
562 <h2 id="_managing_branches">Managing branches</h2>
563 <div class="sectionbody">
564 <div class="paragraph"><p>A single git repository can maintain multiple branches of
565 development. To create a new branch named "experimental", use</p></div>
566 <div class="listingblock">
567 <div class="content">
568 <pre><tt>$ git branch experimental</tt></pre>
569 </div></div>
570 <div class="paragraph"><p>If you now run</p></div>
571 <div class="listingblock">
572 <div class="content">
573 <pre><tt>$ git branch</tt></pre>
574 </div></div>
575 <div class="paragraph"><p>you&#8217;ll get a list of all existing branches:</p></div>
576 <div class="listingblock">
577 <div class="content">
578 <pre><tt> experimental
579 * master</tt></pre>
580 </div></div>
581 <div class="paragraph"><p>The "experimental" branch is the one you just created, and the
582 "master" branch is a default branch that was created for you
583 automatically. The asterisk marks the branch you are currently on;
584 type</p></div>
585 <div class="listingblock">
586 <div class="content">
587 <pre><tt>$ git checkout experimental</tt></pre>
588 </div></div>
589 <div class="paragraph"><p>to switch to the experimental branch. Now edit a file, commit the
590 change, and switch back to the master branch:</p></div>
591 <div class="listingblock">
592 <div class="content">
593 <pre><tt>(edit file)
594 $ git commit -a
595 $ git checkout master</tt></pre>
596 </div></div>
597 <div class="paragraph"><p>Check that the change you made is no longer visible, since it was
598 made on the experimental branch and you&#8217;re back on the master branch.</p></div>
599 <div class="paragraph"><p>You can make a different change on the master branch:</p></div>
600 <div class="listingblock">
601 <div class="content">
602 <pre><tt>(edit file)
603 $ git commit -a</tt></pre>
604 </div></div>
605 <div class="paragraph"><p>at this point the two branches have diverged, with different changes
606 made in each. To merge the changes made in experimental into master, run</p></div>
607 <div class="listingblock">
608 <div class="content">
609 <pre><tt>$ git merge experimental</tt></pre>
610 </div></div>
611 <div class="paragraph"><p>If the changes don&#8217;t conflict, you&#8217;re done. If there are conflicts,
612 markers will be left in the problematic files showing the conflict;</p></div>
613 <div class="listingblock">
614 <div class="content">
615 <pre><tt>$ git diff</tt></pre>
616 </div></div>
617 <div class="paragraph"><p>will show this. Once you&#8217;ve edited the files to resolve the
618 conflicts,</p></div>
619 <div class="listingblock">
620 <div class="content">
621 <pre><tt>$ git commit -a</tt></pre>
622 </div></div>
623 <div class="paragraph"><p>will commit the result of the merge. Finally,</p></div>
624 <div class="listingblock">
625 <div class="content">
626 <pre><tt>$ gitk</tt></pre>
627 </div></div>
628 <div class="paragraph"><p>will show a nice graphical representation of the resulting history.</p></div>
629 <div class="paragraph"><p>At this point you could delete the experimental branch with</p></div>
630 <div class="listingblock">
631 <div class="content">
632 <pre><tt>$ git branch -d experimental</tt></pre>
633 </div></div>
634 <div class="paragraph"><p>This command ensures that the changes in the experimental branch are
635 already in the current branch.</p></div>
636 <div class="paragraph"><p>If you develop on a branch crazy-idea, then regret it, you can always
637 delete the branch with</p></div>
638 <div class="listingblock">
639 <div class="content">
640 <pre><tt>$ git branch -D crazy-idea</tt></pre>
641 </div></div>
642 <div class="paragraph"><p>Branches are cheap and easy, so this is a good way to try something
643 out.</p></div>
644 </div>
645 <h2 id="_using_git_for_collaboration">Using git for collaboration</h2>
646 <div class="sectionbody">
647 <div class="paragraph"><p>Suppose that Alice has started a new project with a git repository in
648 /home/alice/project, and that Bob, who has a home directory on the
649 same machine, wants to contribute.</p></div>
650 <div class="paragraph"><p>Bob begins with:</p></div>
651 <div class="listingblock">
652 <div class="content">
653 <pre><tt>bob$ git clone /home/alice/project myrepo</tt></pre>
654 </div></div>
655 <div class="paragraph"><p>This creates a new directory "myrepo" containing a clone of Alice&#8217;s
656 repository. The clone is on an equal footing with the original
657 project, possessing its own copy of the original project&#8217;s history.</p></div>
658 <div class="paragraph"><p>Bob then makes some changes and commits them:</p></div>
659 <div class="listingblock">
660 <div class="content">
661 <pre><tt>(edit files)
662 bob$ git commit -a
663 (repeat as necessary)</tt></pre>
664 </div></div>
665 <div class="paragraph"><p>When he&#8217;s ready, he tells Alice to pull changes from the repository
666 at /home/bob/myrepo. She does this with:</p></div>
667 <div class="listingblock">
668 <div class="content">
669 <pre><tt>alice$ cd /home/alice/project
670 alice$ git pull /home/bob/myrepo master</tt></pre>
671 </div></div>
672 <div class="paragraph"><p>This merges the changes from Bob&#8217;s "master" branch into Alice&#8217;s
673 current branch. If Alice has made her own changes in the meantime,
674 then she may need to manually fix any conflicts.</p></div>
675 <div class="paragraph"><p>The "pull" command thus performs two operations: it fetches changes
676 from a remote branch, then merges them into the current branch.</p></div>
677 <div class="paragraph"><p>Note that in general, Alice would want her local changes committed before
678 initiating this "pull". If Bob&#8217;s work conflicts with what Alice did since
679 their histories forked, Alice will use her working tree and the index to
680 resolve conflicts, and existing local changes will interfere with the
681 conflict resolution process (git will still perform the fetch but will
682 refuse to merge --- Alice will have to get rid of her local changes in
683 some way and pull again when this happens).</p></div>
684 <div class="paragraph"><p>Alice can peek at what Bob did without merging first, using the "fetch"
685 command; this allows Alice to inspect what Bob did, using a special
686 symbol "FETCH_HEAD", in order to determine if he has anything worth
687 pulling, like this:</p></div>
688 <div class="listingblock">
689 <div class="content">
690 <pre><tt>alice$ git fetch /home/bob/myrepo master
691 alice$ git log -p HEAD..FETCH_HEAD</tt></pre>
692 </div></div>
693 <div class="paragraph"><p>This operation is safe even if Alice has uncommitted local changes.
694 The range notation "HEAD..FETCH_HEAD" means "show everything that is reachable
695 from the FETCH_HEAD but exclude anything that is reachable from HEAD".
696 Alice already knows everything that leads to her current state (HEAD),
697 and reviews what Bob has in his state (FETCH_HEAD) that she has not
698 seen with this command.</p></div>
699 <div class="paragraph"><p>If Alice wants to visualize what Bob did since their histories forked
700 she can issue the following command:</p></div>
701 <div class="listingblock">
702 <div class="content">
703 <pre><tt>$ gitk HEAD..FETCH_HEAD</tt></pre>
704 </div></div>
705 <div class="paragraph"><p>This uses the same two-dot range notation we saw earlier with <em>git log</em>.</p></div>
706 <div class="paragraph"><p>Alice may want to view what both of them did since they forked.
707 She can use three-dot form instead of the two-dot form:</p></div>
708 <div class="listingblock">
709 <div class="content">
710 <pre><tt>$ gitk HEAD...FETCH_HEAD</tt></pre>
711 </div></div>
712 <div class="paragraph"><p>This means "show everything that is reachable from either one, but
713 exclude anything that is reachable from both of them".</p></div>
714 <div class="paragraph"><p>Please note that these range notation can be used with both gitk
715 and "git log".</p></div>
716 <div class="paragraph"><p>After inspecting what Bob did, if there is nothing urgent, Alice may
717 decide to continue working without pulling from Bob. If Bob&#8217;s history
718 does have something Alice would immediately need, Alice may choose to
719 stash her work-in-progress first, do a "pull", and then finally unstash
720 her work-in-progress on top of the resulting history.</p></div>
721 <div class="paragraph"><p>When you are working in a small closely knit group, it is not
722 unusual to interact with the same repository over and over
723 again. By defining <em>remote</em> repository shorthand, you can make
724 it easier:</p></div>
725 <div class="listingblock">
726 <div class="content">
727 <pre><tt>alice$ git remote add bob /home/bob/myrepo</tt></pre>
728 </div></div>
729 <div class="paragraph"><p>With this, Alice can perform the first part of the "pull" operation
730 alone using the <em>git fetch</em> command without merging them with her own
731 branch, using:</p></div>
732 <div class="listingblock">
733 <div class="content">
734 <pre><tt>alice$ git fetch bob</tt></pre>
735 </div></div>
736 <div class="paragraph"><p>Unlike the longhand form, when Alice fetches from Bob using a
737 remote repository shorthand set up with <em>git remote</em>, what was
738 fetched is stored in a remote-tracking branch, in this case
739 <tt>bob/master</tt>. So after this:</p></div>
740 <div class="listingblock">
741 <div class="content">
742 <pre><tt>alice$ git log -p master..bob/master</tt></pre>
743 </div></div>
744 <div class="paragraph"><p>shows a list of all the changes that Bob made since he branched from
745 Alice&#8217;s master branch.</p></div>
746 <div class="paragraph"><p>After examining those changes, Alice
747 could merge the changes into her master branch:</p></div>
748 <div class="listingblock">
749 <div class="content">
750 <pre><tt>alice$ git merge bob/master</tt></pre>
751 </div></div>
752 <div class="paragraph"><p>This <tt>merge</tt> can also be done by <em>pulling from her own remote-tracking
753 branch</em>, like this:</p></div>
754 <div class="listingblock">
755 <div class="content">
756 <pre><tt>alice$ git pull . remotes/bob/master</tt></pre>
757 </div></div>
758 <div class="paragraph"><p>Note that git pull always merges into the current branch,
759 regardless of what else is given on the command line.</p></div>
760 <div class="paragraph"><p>Later, Bob can update his repo with Alice&#8217;s latest changes using</p></div>
761 <div class="listingblock">
762 <div class="content">
763 <pre><tt>bob$ git pull</tt></pre>
764 </div></div>
765 <div class="paragraph"><p>Note that he doesn&#8217;t need to give the path to Alice&#8217;s repository;
766 when Bob cloned Alice&#8217;s repository, git stored the location of her
767 repository in the repository configuration, and that location is
768 used for pulls:</p></div>
769 <div class="listingblock">
770 <div class="content">
771 <pre><tt>bob$ git config --get remote.origin.url
772 /home/alice/project</tt></pre>
773 </div></div>
774 <div class="paragraph"><p>(The complete configuration created by <em>git clone</em> is visible using
775 <tt>git config -l</tt>, and the <a href="git-config.html">git-config(1)</a> man page
776 explains the meaning of each option.)</p></div>
777 <div class="paragraph"><p>Git also keeps a pristine copy of Alice&#8217;s master branch under the
778 name "origin/master":</p></div>
779 <div class="listingblock">
780 <div class="content">
781 <pre><tt>bob$ git branch -r
782 origin/master</tt></pre>
783 </div></div>
784 <div class="paragraph"><p>If Bob later decides to work from a different host, he can still
785 perform clones and pulls using the ssh protocol:</p></div>
786 <div class="listingblock">
787 <div class="content">
788 <pre><tt>bob$ git clone alice.org:/home/alice/project myrepo</tt></pre>
789 </div></div>
790 <div class="paragraph"><p>Alternatively, git has a native protocol, or can use rsync or http;
791 see <a href="git-pull.html">git-pull(1)</a> for details.</p></div>
792 <div class="paragraph"><p>Git can also be used in a CVS-like mode, with a central repository
793 that various users push changes to; see <a href="git-push.html">git-push(1)</a> and
794 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>.</p></div>
795 </div>
796 <h2 id="_exploring_history">Exploring history</h2>
797 <div class="sectionbody">
798 <div class="paragraph"><p>Git history is represented as a series of interrelated commits. We
799 have already seen that the <em>git log</em> command can list those commits.
800 Note that first line of each git log entry also gives a name for the
801 commit:</p></div>
802 <div class="listingblock">
803 <div class="content">
804 <pre><tt>$ git log
805 commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
806 Author: Junio C Hamano &lt;junkio@cox.net&gt;
807 Date: Tue May 16 17:18:22 2006 -0700
809 merge-base: Clarify the comments on post processing.</tt></pre>
810 </div></div>
811 <div class="paragraph"><p>We can give this name to <em>git show</em> to see the details about this
812 commit.</p></div>
813 <div class="listingblock">
814 <div class="content">
815 <pre><tt>$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7</tt></pre>
816 </div></div>
817 <div class="paragraph"><p>But there are other ways to refer to commits. You can use any initial
818 part of the name that is long enough to uniquely identify the commit:</p></div>
819 <div class="listingblock">
820 <div class="content">
821 <pre><tt>$ git show c82a22c39c # the first few characters of the name are
822 # usually enough
823 $ git show HEAD # the tip of the current branch
824 $ git show experimental # the tip of the "experimental" branch</tt></pre>
825 </div></div>
826 <div class="paragraph"><p>Every commit usually has one "parent" commit
827 which points to the previous state of the project:</p></div>
828 <div class="listingblock">
829 <div class="content">
830 <pre><tt>$ git show HEAD^ # to see the parent of HEAD
831 $ git show HEAD^^ # to see the grandparent of HEAD
832 $ git show HEAD~4 # to see the great-great grandparent of HEAD</tt></pre>
833 </div></div>
834 <div class="paragraph"><p>Note that merge commits may have more than one parent:</p></div>
835 <div class="listingblock">
836 <div class="content">
837 <pre><tt>$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
838 $ git show HEAD^2 # show the second parent of HEAD</tt></pre>
839 </div></div>
840 <div class="paragraph"><p>You can also give commits names of your own; after running</p></div>
841 <div class="listingblock">
842 <div class="content">
843 <pre><tt>$ git tag v2.5 1b2e1d63ff</tt></pre>
844 </div></div>
845 <div class="paragraph"><p>you can refer to 1b2e1d63ff by the name "v2.5". If you intend to
846 share this name with other people (for example, to identify a release
847 version), you should create a "tag" object, and perhaps sign it; see
848 <a href="git-tag.html">git-tag(1)</a> for details.</p></div>
849 <div class="paragraph"><p>Any git command that needs to know a commit can take any of these
850 names. For example:</p></div>
851 <div class="listingblock">
852 <div class="content">
853 <pre><tt>$ git diff v2.5 HEAD # compare the current HEAD to v2.5
854 $ git branch stable v2.5 # start a new branch named "stable" based
855 # at v2.5
856 $ git reset --hard HEAD^ # reset your current branch and working
857 # directory to its state at HEAD^</tt></pre>
858 </div></div>
859 <div class="paragraph"><p>Be careful with that last command: in addition to losing any changes
860 in the working directory, it will also remove all later commits from
861 this branch. If this branch is the only branch containing those
862 commits, they will be lost. Also, don&#8217;t use <em>git reset</em> on a
863 publicly-visible branch that other developers pull from, as it will
864 force needless merges on other developers to clean up the history.
865 If you need to undo changes that you have pushed, use <em>git revert</em>
866 instead.</p></div>
867 <div class="paragraph"><p>The <em>git grep</em> command can search for strings in any version of your
868 project, so</p></div>
869 <div class="listingblock">
870 <div class="content">
871 <pre><tt>$ git grep "hello" v2.5</tt></pre>
872 </div></div>
873 <div class="paragraph"><p>searches for all occurrences of "hello" in v2.5.</p></div>
874 <div class="paragraph"><p>If you leave out the commit name, <em>git grep</em> will search any of the
875 files it manages in your current directory. So</p></div>
876 <div class="listingblock">
877 <div class="content">
878 <pre><tt>$ git grep "hello"</tt></pre>
879 </div></div>
880 <div class="paragraph"><p>is a quick way to search just the files that are tracked by git.</p></div>
881 <div class="paragraph"><p>Many git commands also take sets of commits, which can be specified
882 in a number of ways. Here are some examples with <em>git log</em>:</p></div>
883 <div class="listingblock">
884 <div class="content">
885 <pre><tt>$ git log v2.5..v2.6 # commits between v2.5 and v2.6
886 $ git log v2.5.. # commits since v2.5
887 $ git log --since="2 weeks ago" # commits from the last 2 weeks
888 $ git log v2.5.. Makefile # commits since v2.5 which modify
889 # Makefile</tt></pre>
890 </div></div>
891 <div class="paragraph"><p>You can also give <em>git log</em> a "range" of commits where the first is not
892 necessarily an ancestor of the second; for example, if the tips of
893 the branches "stable" and "master" diverged from a common
894 commit some time ago, then</p></div>
895 <div class="listingblock">
896 <div class="content">
897 <pre><tt>$ git log stable..master</tt></pre>
898 </div></div>
899 <div class="paragraph"><p>will list commits made in the master branch but not in the
900 stable branch, while</p></div>
901 <div class="listingblock">
902 <div class="content">
903 <pre><tt>$ git log master..stable</tt></pre>
904 </div></div>
905 <div class="paragraph"><p>will show the list of commits made on the stable branch but not
906 the master branch.</p></div>
907 <div class="paragraph"><p>The <em>git log</em> command has a weakness: it must present commits in a
908 list. When the history has lines of development that diverged and
909 then merged back together, the order in which <em>git log</em> presents
910 those commits is meaningless.</p></div>
911 <div class="paragraph"><p>Most projects with multiple contributors (such as the Linux kernel,
912 or git itself) have frequent merges, and <em>gitk</em> does a better job of
913 visualizing their history. For example,</p></div>
914 <div class="listingblock">
915 <div class="content">
916 <pre><tt>$ gitk --since="2 weeks ago" drivers/</tt></pre>
917 </div></div>
918 <div class="paragraph"><p>allows you to browse any commits from the last 2 weeks of commits
919 that modified files under the "drivers" directory. (Note: you can
920 adjust gitk&#8217;s fonts by holding down the control key while pressing
921 "-" or "+".)</p></div>
922 <div class="paragraph"><p>Finally, most commands that take filenames will optionally allow you
923 to precede any filename by a commit, to specify a particular version
924 of the file:</p></div>
925 <div class="listingblock">
926 <div class="content">
927 <pre><tt>$ git diff v2.5:Makefile HEAD:Makefile.in</tt></pre>
928 </div></div>
929 <div class="paragraph"><p>You can also use <em>git show</em> to see any such file:</p></div>
930 <div class="listingblock">
931 <div class="content">
932 <pre><tt>$ git show v2.5:Makefile</tt></pre>
933 </div></div>
934 </div>
935 <h2 id="_next_steps">Next Steps</h2>
936 <div class="sectionbody">
937 <div class="paragraph"><p>This tutorial should be enough to perform basic distributed revision
938 control for your projects. However, to fully understand the depth
939 and power of git you need to understand two simple ideas on which it
940 is based:</p></div>
941 <div class="ulist"><ul>
942 <li>
944 The object database is the rather elegant system used to
945 store the history of your project&#8212;files, directories, and
946 commits.
947 </p>
948 </li>
949 <li>
951 The index file is a cache of the state of a directory tree,
952 used to create commits, check out working directories, and
953 hold the various trees involved in a merge.
954 </p>
955 </li>
956 </ul></div>
957 <div class="paragraph"><p>Part two of this tutorial explains the object
958 database, the index file, and a few other odds and ends that you&#8217;ll
959 need to make the most of git. You can find it at <a href="gittutorial-2.html">gittutorial-2(7)</a>.</p></div>
960 <div class="paragraph"><p>If you don&#8217;t want to continue with that right away, a few other
961 digressions that may be interesting at this point are:</p></div>
962 <div class="ulist"><ul>
963 <li>
965 <a href="git-format-patch.html">git-format-patch(1)</a>, <a href="git-am.html">git-am(1)</a>: These convert
966 series of git commits into emailed patches, and vice versa,
967 useful for projects such as the Linux kernel which rely heavily
968 on emailed patches.
969 </p>
970 </li>
971 <li>
973 <a href="git-bisect.html">git-bisect(1)</a>: When there is a regression in your
974 project, one way to track down the bug is by searching through
975 the history to find the exact commit that&#8217;s to blame. Git bisect
976 can help you perform a binary search for that commit. It is
977 smart enough to perform a close-to-optimal search even in the
978 case of complex non-linear history with lots of merged branches.
979 </p>
980 </li>
981 <li>
983 <a href="gitworkflows.html">gitworkflows(7)</a>: Gives an overview of recommended
984 workflows.
985 </p>
986 </li>
987 <li>
989 <a href="everyday.html">Everyday GIT with 20 Commands Or So</a>
990 </p>
991 </li>
992 <li>
994 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>: Git for CVS users.
995 </p>
996 </li>
997 </ul></div>
998 </div>
999 <h2 id="_see_also">SEE ALSO</h2>
1000 <div class="sectionbody">
1001 <div class="paragraph"><p><a href="gittutorial-2.html">gittutorial-2(7)</a>,
1002 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>,
1003 <a href="gitcore-tutorial.html">gitcore-tutorial(7)</a>,
1004 <a href="gitglossary.html">gitglossary(7)</a>,
1005 <a href="git-help.html">git-help(1)</a>,
1006 <a href="gitworkflows.html">gitworkflows(7)</a>,
1007 <a href="everyday.html">Everyday git</a>,
1008 <a href="user-manual.html">The Git User&#8217;s Manual</a></p></div>
1009 </div>
1010 <h2 id="_git">GIT</h2>
1011 <div class="sectionbody">
1012 <div class="paragraph"><p>Part of the <a href="git.html">git(1)</a> suite.</p></div>
1013 </div>
1014 <div id="footer">
1015 <div id="footer-text">
1016 Last updated 2011-07-23 00:49:30 UTC
1017 </div>
1018 </div>
1019 </body>
1020 </html>