Autogenerated HTML docs for v1.7.5.1-339-gb602ed
[git/jnareb-git.git] / gittutorial.html
blob2ea2db98d121cf80de1de9fdca13ee93dde0d4fc
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="paragraph"><p>git *</p></div>
418 </div>
419 <h2 id="_description">DESCRIPTION</h2>
420 <div class="sectionbody">
421 <div class="paragraph"><p>This tutorial explains how to import a new project into git, make
422 changes to it, and share changes with other developers.</p></div>
423 <div class="paragraph"><p>If you are instead primarily interested in using git to fetch a project,
424 for example, to test the latest version, you may prefer to start with
425 the first two chapters of <a href="user-manual.html">The Git User&#8217;s Manual</a>.</p></div>
426 <div class="paragraph"><p>First, note that you can get documentation for a command such as
427 <tt>git log --graph</tt> with:</p></div>
428 <div class="listingblock">
429 <div class="content">
430 <pre><tt>$ man git-log</tt></pre>
431 </div></div>
432 <div class="paragraph"><p>or:</p></div>
433 <div class="listingblock">
434 <div class="content">
435 <pre><tt>$ git help log</tt></pre>
436 </div></div>
437 <div class="paragraph"><p>With the latter, you can use the manual viewer of your choice; see
438 <a href="git-help.html">git-help(1)</a> for more information.</p></div>
439 <div class="paragraph"><p>It is a good idea to introduce yourself to git with your name and
440 public email address before doing any operation. The easiest
441 way to do so is:</p></div>
442 <div class="listingblock">
443 <div class="content">
444 <pre><tt>$ git config --global user.name "Your Name Comes Here"
445 $ git config --global user.email you@yourdomain.example.com</tt></pre>
446 </div></div>
447 </div>
448 <h2 id="_importing_a_new_project">Importing a new project</h2>
449 <div class="sectionbody">
450 <div class="paragraph"><p>Assume you have a tarball project.tar.gz with your initial work. You
451 can place it under git revision control as follows.</p></div>
452 <div class="listingblock">
453 <div class="content">
454 <pre><tt>$ tar xzf project.tar.gz
455 $ cd project
456 $ git init</tt></pre>
457 </div></div>
458 <div class="paragraph"><p>Git will reply</p></div>
459 <div class="listingblock">
460 <div class="content">
461 <pre><tt>Initialized empty Git repository in .git/</tt></pre>
462 </div></div>
463 <div class="paragraph"><p>You&#8217;ve now initialized the working directory&#8212;you may notice a new
464 directory created, named ".git".</p></div>
465 <div class="paragraph"><p>Next, tell git to take a snapshot of the contents of all files under the
466 current directory (note the <em>.</em>), with <em>git add</em>:</p></div>
467 <div class="listingblock">
468 <div class="content">
469 <pre><tt>$ git add .</tt></pre>
470 </div></div>
471 <div class="paragraph"><p>This snapshot is now stored in a temporary staging area which git calls
472 the "index". You can permanently store the contents of the index in the
473 repository with <em>git commit</em>:</p></div>
474 <div class="listingblock">
475 <div class="content">
476 <pre><tt>$ git commit</tt></pre>
477 </div></div>
478 <div class="paragraph"><p>This will prompt you for a commit message. You&#8217;ve now stored the first
479 version of your project in git.</p></div>
480 </div>
481 <h2 id="_making_changes">Making changes</h2>
482 <div class="sectionbody">
483 <div class="paragraph"><p>Modify some files, then add their updated contents to the index:</p></div>
484 <div class="listingblock">
485 <div class="content">
486 <pre><tt>$ git add file1 file2 file3</tt></pre>
487 </div></div>
488 <div class="paragraph"><p>You are now ready to commit. You can see what is about to be committed
489 using <em>git diff</em> with the --cached option:</p></div>
490 <div class="listingblock">
491 <div class="content">
492 <pre><tt>$ git diff --cached</tt></pre>
493 </div></div>
494 <div class="paragraph"><p>(Without --cached, <em>git diff</em> will show you any changes that
495 you&#8217;ve made but not yet added to the index.) You can also get a brief
496 summary of the situation with <em>git status</em>:</p></div>
497 <div class="listingblock">
498 <div class="content">
499 <pre><tt>$ git status
500 # On branch master
501 # Changes to be committed:
502 # (use "git reset HEAD &lt;file&gt;..." to unstage)
504 # modified: file1
505 # modified: file2
506 # modified: file3
507 #</tt></pre>
508 </div></div>
509 <div class="paragraph"><p>If you need to make any further adjustments, do so now, and then add any
510 newly modified content to the index. Finally, commit your changes with:</p></div>
511 <div class="listingblock">
512 <div class="content">
513 <pre><tt>$ git commit</tt></pre>
514 </div></div>
515 <div class="paragraph"><p>This will again prompt you for a message describing the change, and then
516 record a new version of the project.</p></div>
517 <div class="paragraph"><p>Alternatively, instead of running <em>git add</em> beforehand, you can use</p></div>
518 <div class="listingblock">
519 <div class="content">
520 <pre><tt>$ git commit -a</tt></pre>
521 </div></div>
522 <div class="paragraph"><p>which will automatically notice any modified (but not new) files, add
523 them to the index, and commit, all in one step.</p></div>
524 <div class="paragraph"><p>A note on commit messages: Though not required, it&#8217;s a good idea to
525 begin the commit message with a single short (less than 50 character)
526 line summarizing the change, followed by a blank line and then a more
527 thorough description. Tools that turn commits into email, for
528 example, use the first line on the Subject: line and the rest of the
529 commit in the body.</p></div>
530 </div>
531 <h2 id="_git_tracks_content_not_files">Git tracks content not files</h2>
532 <div class="sectionbody">
533 <div class="paragraph"><p>Many revision control systems provide an <tt>add</tt> command that tells the
534 system to start tracking changes to a new file. Git&#8217;s <tt>add</tt> command
535 does something simpler and more powerful: <em>git add</em> is used both for new
536 and newly modified files, and in both cases it takes a snapshot of the
537 given files and stages that content in the index, ready for inclusion in
538 the next commit.</p></div>
539 </div>
540 <h2 id="_viewing_project_history">Viewing project history</h2>
541 <div class="sectionbody">
542 <div class="paragraph"><p>At any point you can view the history of your changes using</p></div>
543 <div class="listingblock">
544 <div class="content">
545 <pre><tt>$ git log</tt></pre>
546 </div></div>
547 <div class="paragraph"><p>If you also want to see complete diffs at each step, use</p></div>
548 <div class="listingblock">
549 <div class="content">
550 <pre><tt>$ git log -p</tt></pre>
551 </div></div>
552 <div class="paragraph"><p>Often the overview of the change is useful to get a feel of
553 each step</p></div>
554 <div class="listingblock">
555 <div class="content">
556 <pre><tt>$ git log --stat --summary</tt></pre>
557 </div></div>
558 </div>
559 <h2 id="_managing_branches">Managing branches</h2>
560 <div class="sectionbody">
561 <div class="paragraph"><p>A single git repository can maintain multiple branches of
562 development. To create a new branch named "experimental", use</p></div>
563 <div class="listingblock">
564 <div class="content">
565 <pre><tt>$ git branch experimental</tt></pre>
566 </div></div>
567 <div class="paragraph"><p>If you now run</p></div>
568 <div class="listingblock">
569 <div class="content">
570 <pre><tt>$ git branch</tt></pre>
571 </div></div>
572 <div class="paragraph"><p>you&#8217;ll get a list of all existing branches:</p></div>
573 <div class="listingblock">
574 <div class="content">
575 <pre><tt> experimental
576 * master</tt></pre>
577 </div></div>
578 <div class="paragraph"><p>The "experimental" branch is the one you just created, and the
579 "master" branch is a default branch that was created for you
580 automatically. The asterisk marks the branch you are currently on;
581 type</p></div>
582 <div class="listingblock">
583 <div class="content">
584 <pre><tt>$ git checkout experimental</tt></pre>
585 </div></div>
586 <div class="paragraph"><p>to switch to the experimental branch. Now edit a file, commit the
587 change, and switch back to the master branch:</p></div>
588 <div class="listingblock">
589 <div class="content">
590 <pre><tt>(edit file)
591 $ git commit -a
592 $ git checkout master</tt></pre>
593 </div></div>
594 <div class="paragraph"><p>Check that the change you made is no longer visible, since it was
595 made on the experimental branch and you&#8217;re back on the master branch.</p></div>
596 <div class="paragraph"><p>You can make a different change on the master branch:</p></div>
597 <div class="listingblock">
598 <div class="content">
599 <pre><tt>(edit file)
600 $ git commit -a</tt></pre>
601 </div></div>
602 <div class="paragraph"><p>at this point the two branches have diverged, with different changes
603 made in each. To merge the changes made in experimental into master, run</p></div>
604 <div class="listingblock">
605 <div class="content">
606 <pre><tt>$ git merge experimental</tt></pre>
607 </div></div>
608 <div class="paragraph"><p>If the changes don&#8217;t conflict, you&#8217;re done. If there are conflicts,
609 markers will be left in the problematic files showing the conflict;</p></div>
610 <div class="listingblock">
611 <div class="content">
612 <pre><tt>$ git diff</tt></pre>
613 </div></div>
614 <div class="paragraph"><p>will show this. Once you&#8217;ve edited the files to resolve the
615 conflicts,</p></div>
616 <div class="listingblock">
617 <div class="content">
618 <pre><tt>$ git commit -a</tt></pre>
619 </div></div>
620 <div class="paragraph"><p>will commit the result of the merge. Finally,</p></div>
621 <div class="listingblock">
622 <div class="content">
623 <pre><tt>$ gitk</tt></pre>
624 </div></div>
625 <div class="paragraph"><p>will show a nice graphical representation of the resulting history.</p></div>
626 <div class="paragraph"><p>At this point you could delete the experimental branch with</p></div>
627 <div class="listingblock">
628 <div class="content">
629 <pre><tt>$ git branch -d experimental</tt></pre>
630 </div></div>
631 <div class="paragraph"><p>This command ensures that the changes in the experimental branch are
632 already in the current branch.</p></div>
633 <div class="paragraph"><p>If you develop on a branch crazy-idea, then regret it, you can always
634 delete the branch with</p></div>
635 <div class="listingblock">
636 <div class="content">
637 <pre><tt>$ git branch -D crazy-idea</tt></pre>
638 </div></div>
639 <div class="paragraph"><p>Branches are cheap and easy, so this is a good way to try something
640 out.</p></div>
641 </div>
642 <h2 id="_using_git_for_collaboration">Using git for collaboration</h2>
643 <div class="sectionbody">
644 <div class="paragraph"><p>Suppose that Alice has started a new project with a git repository in
645 /home/alice/project, and that Bob, who has a home directory on the
646 same machine, wants to contribute.</p></div>
647 <div class="paragraph"><p>Bob begins with:</p></div>
648 <div class="listingblock">
649 <div class="content">
650 <pre><tt>bob$ git clone /home/alice/project myrepo</tt></pre>
651 </div></div>
652 <div class="paragraph"><p>This creates a new directory "myrepo" containing a clone of Alice&#8217;s
653 repository. The clone is on an equal footing with the original
654 project, possessing its own copy of the original project&#8217;s history.</p></div>
655 <div class="paragraph"><p>Bob then makes some changes and commits them:</p></div>
656 <div class="listingblock">
657 <div class="content">
658 <pre><tt>(edit files)
659 bob$ git commit -a
660 (repeat as necessary)</tt></pre>
661 </div></div>
662 <div class="paragraph"><p>When he&#8217;s ready, he tells Alice to pull changes from the repository
663 at /home/bob/myrepo. She does this with:</p></div>
664 <div class="listingblock">
665 <div class="content">
666 <pre><tt>alice$ cd /home/alice/project
667 alice$ git pull /home/bob/myrepo master</tt></pre>
668 </div></div>
669 <div class="paragraph"><p>This merges the changes from Bob&#8217;s "master" branch into Alice&#8217;s
670 current branch. If Alice has made her own changes in the meantime,
671 then she may need to manually fix any conflicts.</p></div>
672 <div class="paragraph"><p>The "pull" command thus performs two operations: it fetches changes
673 from a remote branch, then merges them into the current branch.</p></div>
674 <div class="paragraph"><p>Note that in general, Alice would want her local changes committed before
675 initiating this "pull". If Bob&#8217;s work conflicts with what Alice did since
676 their histories forked, Alice will use her working tree and the index to
677 resolve conflicts, and existing local changes will interfere with the
678 conflict resolution process (git will still perform the fetch but will
679 refuse to merge --- Alice will have to get rid of her local changes in
680 some way and pull again when this happens).</p></div>
681 <div class="paragraph"><p>Alice can peek at what Bob did without merging first, using the "fetch"
682 command; this allows Alice to inspect what Bob did, using a special
683 symbol "FETCH_HEAD", in order to determine if he has anything worth
684 pulling, like this:</p></div>
685 <div class="listingblock">
686 <div class="content">
687 <pre><tt>alice$ git fetch /home/bob/myrepo master
688 alice$ git log -p HEAD..FETCH_HEAD</tt></pre>
689 </div></div>
690 <div class="paragraph"><p>This operation is safe even if Alice has uncommitted local changes.
691 The range notation "HEAD..FETCH_HEAD" means "show everything that is reachable
692 from the FETCH_HEAD but exclude anything that is reachable from HEAD".
693 Alice already knows everything that leads to her current state (HEAD),
694 and reviews what Bob has in his state (FETCH_HEAD) that she has not
695 seen with this command.</p></div>
696 <div class="paragraph"><p>If Alice wants to visualize what Bob did since their histories forked
697 she can issue the following command:</p></div>
698 <div class="listingblock">
699 <div class="content">
700 <pre><tt>$ gitk HEAD..FETCH_HEAD</tt></pre>
701 </div></div>
702 <div class="paragraph"><p>This uses the same two-dot range notation we saw earlier with <em>git log</em>.</p></div>
703 <div class="paragraph"><p>Alice may want to view what both of them did since they forked.
704 She can use three-dot form instead of the two-dot form:</p></div>
705 <div class="listingblock">
706 <div class="content">
707 <pre><tt>$ gitk HEAD...FETCH_HEAD</tt></pre>
708 </div></div>
709 <div class="paragraph"><p>This means "show everything that is reachable from either one, but
710 exclude anything that is reachable from both of them".</p></div>
711 <div class="paragraph"><p>Please note that these range notation can be used with both gitk
712 and "git log".</p></div>
713 <div class="paragraph"><p>After inspecting what Bob did, if there is nothing urgent, Alice may
714 decide to continue working without pulling from Bob. If Bob&#8217;s history
715 does have something Alice would immediately need, Alice may choose to
716 stash her work-in-progress first, do a "pull", and then finally unstash
717 her work-in-progress on top of the resulting history.</p></div>
718 <div class="paragraph"><p>When you are working in a small closely knit group, it is not
719 unusual to interact with the same repository over and over
720 again. By defining <em>remote</em> repository shorthand, you can make
721 it easier:</p></div>
722 <div class="listingblock">
723 <div class="content">
724 <pre><tt>alice$ git remote add bob /home/bob/myrepo</tt></pre>
725 </div></div>
726 <div class="paragraph"><p>With this, Alice can perform the first part of the "pull" operation
727 alone using the <em>git fetch</em> command without merging them with her own
728 branch, using:</p></div>
729 <div class="listingblock">
730 <div class="content">
731 <pre><tt>alice$ git fetch bob</tt></pre>
732 </div></div>
733 <div class="paragraph"><p>Unlike the longhand form, when Alice fetches from Bob using a
734 remote repository shorthand set up with <em>git remote</em>, what was
735 fetched is stored in a remote-tracking branch, in this case
736 <tt>bob/master</tt>. So after this:</p></div>
737 <div class="listingblock">
738 <div class="content">
739 <pre><tt>alice$ git log -p master..bob/master</tt></pre>
740 </div></div>
741 <div class="paragraph"><p>shows a list of all the changes that Bob made since he branched from
742 Alice&#8217;s master branch.</p></div>
743 <div class="paragraph"><p>After examining those changes, Alice
744 could merge the changes into her master branch:</p></div>
745 <div class="listingblock">
746 <div class="content">
747 <pre><tt>alice$ git merge bob/master</tt></pre>
748 </div></div>
749 <div class="paragraph"><p>This <tt>merge</tt> can also be done by <em>pulling from her own remote-tracking
750 branch</em>, like this:</p></div>
751 <div class="listingblock">
752 <div class="content">
753 <pre><tt>alice$ git pull . remotes/bob/master</tt></pre>
754 </div></div>
755 <div class="paragraph"><p>Note that git pull always merges into the current branch,
756 regardless of what else is given on the command line.</p></div>
757 <div class="paragraph"><p>Later, Bob can update his repo with Alice&#8217;s latest changes using</p></div>
758 <div class="listingblock">
759 <div class="content">
760 <pre><tt>bob$ git pull</tt></pre>
761 </div></div>
762 <div class="paragraph"><p>Note that he doesn&#8217;t need to give the path to Alice&#8217;s repository;
763 when Bob cloned Alice&#8217;s repository, git stored the location of her
764 repository in the repository configuration, and that location is
765 used for pulls:</p></div>
766 <div class="listingblock">
767 <div class="content">
768 <pre><tt>bob$ git config --get remote.origin.url
769 /home/alice/project</tt></pre>
770 </div></div>
771 <div class="paragraph"><p>(The complete configuration created by <em>git clone</em> is visible using
772 <tt>git config -l</tt>, and the <a href="git-config.html">git-config(1)</a> man page
773 explains the meaning of each option.)</p></div>
774 <div class="paragraph"><p>Git also keeps a pristine copy of Alice&#8217;s master branch under the
775 name "origin/master":</p></div>
776 <div class="listingblock">
777 <div class="content">
778 <pre><tt>bob$ git branch -r
779 origin/master</tt></pre>
780 </div></div>
781 <div class="paragraph"><p>If Bob later decides to work from a different host, he can still
782 perform clones and pulls using the ssh protocol:</p></div>
783 <div class="listingblock">
784 <div class="content">
785 <pre><tt>bob$ git clone alice.org:/home/alice/project myrepo</tt></pre>
786 </div></div>
787 <div class="paragraph"><p>Alternatively, git has a native protocol, or can use rsync or http;
788 see <a href="git-pull.html">git-pull(1)</a> for details.</p></div>
789 <div class="paragraph"><p>Git can also be used in a CVS-like mode, with a central repository
790 that various users push changes to; see <a href="git-push.html">git-push(1)</a> and
791 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>.</p></div>
792 </div>
793 <h2 id="_exploring_history">Exploring history</h2>
794 <div class="sectionbody">
795 <div class="paragraph"><p>Git history is represented as a series of interrelated commits. We
796 have already seen that the <em>git log</em> command can list those commits.
797 Note that first line of each git log entry also gives a name for the
798 commit:</p></div>
799 <div class="listingblock">
800 <div class="content">
801 <pre><tt>$ git log
802 commit c82a22c39cbc32576f64f5c6b3f24b99ea8149c7
803 Author: Junio C Hamano &lt;junkio@cox.net&gt;
804 Date: Tue May 16 17:18:22 2006 -0700
806 merge-base: Clarify the comments on post processing.</tt></pre>
807 </div></div>
808 <div class="paragraph"><p>We can give this name to <em>git show</em> to see the details about this
809 commit.</p></div>
810 <div class="listingblock">
811 <div class="content">
812 <pre><tt>$ git show c82a22c39cbc32576f64f5c6b3f24b99ea8149c7</tt></pre>
813 </div></div>
814 <div class="paragraph"><p>But there are other ways to refer to commits. You can use any initial
815 part of the name that is long enough to uniquely identify the commit:</p></div>
816 <div class="listingblock">
817 <div class="content">
818 <pre><tt>$ git show c82a22c39c # the first few characters of the name are
819 # usually enough
820 $ git show HEAD # the tip of the current branch
821 $ git show experimental # the tip of the "experimental" branch</tt></pre>
822 </div></div>
823 <div class="paragraph"><p>Every commit usually has one "parent" commit
824 which points to the previous state of the project:</p></div>
825 <div class="listingblock">
826 <div class="content">
827 <pre><tt>$ git show HEAD^ # to see the parent of HEAD
828 $ git show HEAD^^ # to see the grandparent of HEAD
829 $ git show HEAD~4 # to see the great-great grandparent of HEAD</tt></pre>
830 </div></div>
831 <div class="paragraph"><p>Note that merge commits may have more than one parent:</p></div>
832 <div class="listingblock">
833 <div class="content">
834 <pre><tt>$ git show HEAD^1 # show the first parent of HEAD (same as HEAD^)
835 $ git show HEAD^2 # show the second parent of HEAD</tt></pre>
836 </div></div>
837 <div class="paragraph"><p>You can also give commits names of your own; after running</p></div>
838 <div class="listingblock">
839 <div class="content">
840 <pre><tt>$ git tag v2.5 1b2e1d63ff</tt></pre>
841 </div></div>
842 <div class="paragraph"><p>you can refer to 1b2e1d63ff by the name "v2.5". If you intend to
843 share this name with other people (for example, to identify a release
844 version), you should create a "tag" object, and perhaps sign it; see
845 <a href="git-tag.html">git-tag(1)</a> for details.</p></div>
846 <div class="paragraph"><p>Any git command that needs to know a commit can take any of these
847 names. For example:</p></div>
848 <div class="listingblock">
849 <div class="content">
850 <pre><tt>$ git diff v2.5 HEAD # compare the current HEAD to v2.5
851 $ git branch stable v2.5 # start a new branch named "stable" based
852 # at v2.5
853 $ git reset --hard HEAD^ # reset your current branch and working
854 # directory to its state at HEAD^</tt></pre>
855 </div></div>
856 <div class="paragraph"><p>Be careful with that last command: in addition to losing any changes
857 in the working directory, it will also remove all later commits from
858 this branch. If this branch is the only branch containing those
859 commits, they will be lost. Also, don&#8217;t use <em>git reset</em> on a
860 publicly-visible branch that other developers pull from, as it will
861 force needless merges on other developers to clean up the history.
862 If you need to undo changes that you have pushed, use <em>git revert</em>
863 instead.</p></div>
864 <div class="paragraph"><p>The <em>git grep</em> command can search for strings in any version of your
865 project, so</p></div>
866 <div class="listingblock">
867 <div class="content">
868 <pre><tt>$ git grep "hello" v2.5</tt></pre>
869 </div></div>
870 <div class="paragraph"><p>searches for all occurrences of "hello" in v2.5.</p></div>
871 <div class="paragraph"><p>If you leave out the commit name, <em>git grep</em> will search any of the
872 files it manages in your current directory. So</p></div>
873 <div class="listingblock">
874 <div class="content">
875 <pre><tt>$ git grep "hello"</tt></pre>
876 </div></div>
877 <div class="paragraph"><p>is a quick way to search just the files that are tracked by git.</p></div>
878 <div class="paragraph"><p>Many git commands also take sets of commits, which can be specified
879 in a number of ways. Here are some examples with <em>git log</em>:</p></div>
880 <div class="listingblock">
881 <div class="content">
882 <pre><tt>$ git log v2.5..v2.6 # commits between v2.5 and v2.6
883 $ git log v2.5.. # commits since v2.5
884 $ git log --since="2 weeks ago" # commits from the last 2 weeks
885 $ git log v2.5.. Makefile # commits since v2.5 which modify
886 # Makefile</tt></pre>
887 </div></div>
888 <div class="paragraph"><p>You can also give <em>git log</em> a "range" of commits where the first is not
889 necessarily an ancestor of the second; for example, if the tips of
890 the branches "stable" and "master" diverged from a common
891 commit some time ago, then</p></div>
892 <div class="listingblock">
893 <div class="content">
894 <pre><tt>$ git log stable..master</tt></pre>
895 </div></div>
896 <div class="paragraph"><p>will list commits made in the master branch but not in the
897 stable branch, while</p></div>
898 <div class="listingblock">
899 <div class="content">
900 <pre><tt>$ git log master..stable</tt></pre>
901 </div></div>
902 <div class="paragraph"><p>will show the list of commits made on the stable branch but not
903 the master branch.</p></div>
904 <div class="paragraph"><p>The <em>git log</em> command has a weakness: it must present commits in a
905 list. When the history has lines of development that diverged and
906 then merged back together, the order in which <em>git log</em> presents
907 those commits is meaningless.</p></div>
908 <div class="paragraph"><p>Most projects with multiple contributors (such as the Linux kernel,
909 or git itself) have frequent merges, and <em>gitk</em> does a better job of
910 visualizing their history. For example,</p></div>
911 <div class="listingblock">
912 <div class="content">
913 <pre><tt>$ gitk --since="2 weeks ago" drivers/</tt></pre>
914 </div></div>
915 <div class="paragraph"><p>allows you to browse any commits from the last 2 weeks of commits
916 that modified files under the "drivers" directory. (Note: you can
917 adjust gitk&#8217;s fonts by holding down the control key while pressing
918 "-" or "+".)</p></div>
919 <div class="paragraph"><p>Finally, most commands that take filenames will optionally allow you
920 to precede any filename by a commit, to specify a particular version
921 of the file:</p></div>
922 <div class="listingblock">
923 <div class="content">
924 <pre><tt>$ git diff v2.5:Makefile HEAD:Makefile.in</tt></pre>
925 </div></div>
926 <div class="paragraph"><p>You can also use <em>git show</em> to see any such file:</p></div>
927 <div class="listingblock">
928 <div class="content">
929 <pre><tt>$ git show v2.5:Makefile</tt></pre>
930 </div></div>
931 </div>
932 <h2 id="_next_steps">Next Steps</h2>
933 <div class="sectionbody">
934 <div class="paragraph"><p>This tutorial should be enough to perform basic distributed revision
935 control for your projects. However, to fully understand the depth
936 and power of git you need to understand two simple ideas on which it
937 is based:</p></div>
938 <div class="ulist"><ul>
939 <li>
941 The object database is the rather elegant system used to
942 store the history of your project&#8212;files, directories, and
943 commits.
944 </p>
945 </li>
946 <li>
948 The index file is a cache of the state of a directory tree,
949 used to create commits, check out working directories, and
950 hold the various trees involved in a merge.
951 </p>
952 </li>
953 </ul></div>
954 <div class="paragraph"><p>Part two of this tutorial explains the object
955 database, the index file, and a few other odds and ends that you&#8217;ll
956 need to make the most of git. You can find it at <a href="gittutorial-2.html">gittutorial-2(7)</a>.</p></div>
957 <div class="paragraph"><p>If you don&#8217;t want to continue with that right away, a few other
958 digressions that may be interesting at this point are:</p></div>
959 <div class="ulist"><ul>
960 <li>
962 <a href="git-format-patch.html">git-format-patch(1)</a>, <a href="git-am.html">git-am(1)</a>: These convert
963 series of git commits into emailed patches, and vice versa,
964 useful for projects such as the Linux kernel which rely heavily
965 on emailed patches.
966 </p>
967 </li>
968 <li>
970 <a href="git-bisect.html">git-bisect(1)</a>: When there is a regression in your
971 project, one way to track down the bug is by searching through
972 the history to find the exact commit that&#8217;s to blame. Git bisect
973 can help you perform a binary search for that commit. It is
974 smart enough to perform a close-to-optimal search even in the
975 case of complex non-linear history with lots of merged branches.
976 </p>
977 </li>
978 <li>
980 <a href="gitworkflows.html">gitworkflows(7)</a>: Gives an overview of recommended
981 workflows.
982 </p>
983 </li>
984 <li>
986 <a href="everyday.html">Everyday GIT with 20 Commands Or So</a>
987 </p>
988 </li>
989 <li>
991 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>: Git for CVS users.
992 </p>
993 </li>
994 </ul></div>
995 </div>
996 <h2 id="_see_also">SEE ALSO</h2>
997 <div class="sectionbody">
998 <div class="paragraph"><p><a href="gittutorial-2.html">gittutorial-2(7)</a>,
999 <a href="gitcvs-migration.html">gitcvs-migration(7)</a>,
1000 <a href="gitcore-tutorial.html">gitcore-tutorial(7)</a>,
1001 <a href="gitglossary.html">gitglossary(7)</a>,
1002 <a href="git-help.html">git-help(1)</a>,
1003 <a href="gitworkflows.html">gitworkflows(7)</a>,
1004 <a href="everyday.html">Everyday git</a>,
1005 <a href="user-manual.html">The Git User&#8217;s Manual</a></p></div>
1006 </div>
1007 <h2 id="_git">GIT</h2>
1008 <div class="sectionbody">
1009 <div class="paragraph"><p>Part of the <a href="git.html">git(1)</a> suite.</p></div>
1010 </div>
1011 <div id="footer">
1012 <div id="footer-text">
1013 Last updated 2010-11-25 03:13:43 UTC
1014 </div>
1015 </div>
1016 </body>
1017 </html>