Autogenerated HTML docs for v1.1.4-gdcc6
[git.git] / tutorial.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"\r
2     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\r
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\r
4 <head>\r
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />\r
6 <meta name="generator" content="AsciiDoc 7.0.1" />\r
7 <style type="text/css">\r
8 /* Debug borders */\r
9 p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {\r
10 /*\r
11   border: 1px solid red;\r
12 */\r
13 }\r
14 \r
15 body {\r
16   margin: 1em 5% 1em 5%;\r
17 }\r
18 \r
19 a { color: blue; }\r
20 a:visited { color: fuchsia; }\r
21 \r
22 em {\r
23   font-style: italic;\r
24 }\r
25 \r
26 strong {\r
27   font-weight: bold;\r
28 }\r
29 \r
30 tt {\r
31   color: navy;\r
32 }\r
33 \r
34 h1, h2, h3, h4, h5, h6 {\r
35   color: #527bbd;\r
36   font-family: sans-serif;\r
37   margin-top: 1.2em;\r
38   margin-bottom: 0.5em;\r
39   line-height: 1.3;\r
40 }\r
41 \r
42 h1 {\r
43   border-bottom: 2px solid silver;\r
44 }\r
45 h2 {\r
46   border-bottom: 2px solid silver;\r
47   padding-top: 0.5em;\r
48 }\r
49 \r
50 div.sectionbody {\r
51   font-family: serif;\r
52   margin-left: 0;\r
53 }\r
54 \r
55 hr {\r
56   border: 1px solid silver;\r
57 }\r
58 \r
59 p {\r
60   margin-top: 0.5em;\r
61   margin-bottom: 0.5em;\r
62 }\r
63 \r
64 pre {\r
65   padding: 0;\r
66   margin: 0;\r
67 }\r
68 \r
69 span#author {\r
70   color: #527bbd;\r
71   font-family: sans-serif;\r
72   font-weight: bold;\r
73   font-size: 1.2em;\r
74 }\r
75 span#email {\r
76 }\r
77 span#revision {\r
78   font-family: sans-serif;\r
79 }\r
80 \r
81 div#footer {\r
82   font-family: sans-serif;\r
83   font-size: small;\r
84   border-top: 2px solid silver;\r
85   padding-top: 0.5em;\r
86   margin-top: 4.0em;\r
87 }\r
88 div#footer-text {\r
89   float: left;\r
90   padding-bottom: 0.5em;\r
91 }\r
92 div#footer-badges {\r
93   float: right;\r
94   padding-bottom: 0.5em;\r
95 }\r
96 \r
97 div#preamble,\r
98 div.tableblock, div.imageblock, div.exampleblock, div.verseblock,\r
99 div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,\r
100 div.admonitionblock {\r
101   margin-right: 10%;\r
102   margin-top: 1.5em;\r
103   margin-bottom: 1.5em;\r
104 }\r
105 div.admonitionblock {\r
106   margin-top: 2.5em;\r
107   margin-bottom: 2.5em;\r
108 }\r
109 \r
110 div.content { /* Block element content. */\r
111   padding: 0;\r
112 }\r
113 \r
114 /* Block element titles. */\r
115 div.title, caption.title {\r
116   font-family: sans-serif;\r
117   font-weight: bold;\r
118   text-align: left;\r
119   margin-top: 1.0em;\r
120   margin-bottom: 0.5em;\r
121 }\r
122 div.title + * {\r
123   margin-top: 0;\r
124 }\r
125 \r
126 td div.title:first-child {\r
127   margin-top: 0.0em;\r
128 }\r
129 div.content div.title:first-child {\r
130   margin-top: 0.0em;\r
131 }\r
132 div.content + div.title {\r
133   margin-top: 0.0em;\r
134 }\r
135 \r
136 div.sidebarblock > div.content {\r
137   background: #ffffee;\r
138   border: 1px solid silver;\r
139   padding: 0.5em;\r
140 }\r
141 \r
142 div.listingblock > div.content {\r
143   border: 1px solid silver;\r
144   background: #f4f4f4;\r
145   padding: 0.5em;\r
146 }\r
147 \r
148 div.quoteblock > div.content {\r
149   padding-left: 2.0em;\r
150 }\r
151 div.quoteblock .attribution {\r
152   text-align: right;\r
153 }\r
154 \r
155 div.admonitionblock .icon {\r
156   vertical-align: top;\r
157   font-size: 1.1em;\r
158   font-weight: bold;\r
159   text-decoration: underline;\r
160   color: #527bbd;\r
161   padding-right: 0.5em;\r
162 }\r
163 div.admonitionblock td.content {\r
164   padding-left: 0.5em;\r
165   border-left: 2px solid silver;\r
166 }\r
167 \r
168 div.exampleblock > div.content {\r
169   border-left: 2px solid silver;\r
170   padding: 0.5em;\r
171 }\r
172 \r
173 div.verseblock div.content {\r
174   white-space: pre;\r
175 }\r
176 \r
177 div.imageblock div.content { padding-left: 0; }\r
178 div.imageblock img { border: 1px solid silver; }\r
179 span.image img { border-style: none; }\r
180 \r
181 dl {\r
182   margin-top: 0.8em;\r
183   margin-bottom: 0.8em;\r
184 }\r
185 dt {\r
186   margin-top: 0.5em;\r
187   margin-bottom: 0;\r
188   font-style: italic;\r
189 }\r
190 dd > *:first-child {\r
191   margin-top: 0;\r
192 }\r
193 \r
194 ul, ol {\r
195     list-style-position: outside;\r
196 }\r
197 ol.olist2 {\r
198   list-style-type: lower-alpha;\r
199 }\r
200 \r
201 div.tableblock > table {\r
202   border-color: #527bbd;\r
203   border-width: 3px;\r
204 }\r
205 thead {\r
206   font-family: sans-serif;\r
207   font-weight: bold;\r
208 }\r
209 tfoot {\r
210   font-weight: bold;\r
211 }\r
212 \r
213 div.hlist {\r
214   margin-top: 0.8em;\r
215   margin-bottom: 0.8em;\r
216 }\r
217 td.hlist1 {\r
218   vertical-align: top;\r
219   font-style: italic;\r
220   padding-right: 0.8em;\r
221 }\r
222 td.hlist2 {\r
223   vertical-align: top;\r
224 }\r
225 \r
226 @media print {\r
227   div#footer-badges { display: none; }\r
228 }\r
229 /* Workarounds for IE6's broken and incomplete CSS2. */\r
230 \r
231 div.sidebar-content {\r
232   background: #ffffee;\r
233   border: 1px solid silver;\r
234   padding: 0.5em;\r
235 }\r
236 div.sidebar-title, div.image-title {\r
237   font-family: sans-serif;\r
238   font-weight: bold;\r
239   margin-top: 0.0em;\r
240   margin-bottom: 0.5em;\r
241 }\r
242 \r
243 div.listingblock div.content {\r
244   border: 1px solid silver;\r
245   background: #f4f4f4;\r
246   padding: 0.5em;\r
247 }\r
248 \r
249 div.quoteblock-content {\r
250   padding-left: 2.0em;\r
251 }\r
252 \r
253 div.exampleblock-content {\r
254   border-left: 2px solid silver;\r
255   padding-left: 0.5em;\r
256 }\r
257 </style>\r
258 <title>A tutorial introduction to git</title>\r
259 </head>\r
260 <body>\r
261 <div id="header">\r
262 <h1>A tutorial introduction to git</h1>\r
263 </div>\r
264 <div id="preamble">\r
265 <div class="sectionbody">\r
266 <p>This tutorial explains how to import a new project into git, make\r
267 changes to it, and share changes with other developers.</p>\r
268 <p>First, note that you can get documentation for a command such as "git\r
269 diff" with:</p>\r
270 <div class="listingblock">\r
271 <div class="content">\r
272 <pre><tt>$ man git-diff</tt></pre>\r
273 </div></div>\r
274 </div>\r
275 </div>\r
276 <h2>Importing a new project</h2>\r
277 <div class="sectionbody">\r
278 <p>Assume you have a tarball project.tar.gz with your initial work.  You\r
279 can place it under git revision control as follows.</p>\r
280 <div class="listingblock">\r
281 <div class="content">\r
282 <pre><tt>$ tar xzf project.tar.gz\r
283 $ cd project\r
284 $ git init-db</tt></pre>\r
285 </div></div>\r
286 <p>Git will reply</p>\r
287 <div class="listingblock">\r
288 <div class="content">\r
289 <pre><tt>defaulting to local storage area</tt></pre>\r
290 </div></div>\r
291 <p>You've now initialized the working directory&#8212;you may notice a new\r
292 directory created, named ".git".  Tell git that you want it to track\r
293 every file under the current directory with</p>\r
294 <div class="listingblock">\r
295 <div class="content">\r
296 <pre><tt>$ git add .</tt></pre>\r
297 </div></div>\r
298 <p>Finally,</p>\r
299 <div class="listingblock">\r
300 <div class="content">\r
301 <pre><tt>$ git commit -a</tt></pre>\r
302 </div></div>\r
303 <p>will prompt you for a commit message, then record the current state\r
304 of all the files to the repository.</p>\r
305 <p>Try modifying some files, then run</p>\r
306 <div class="listingblock">\r
307 <div class="content">\r
308 <pre><tt>$ git diff</tt></pre>\r
309 </div></div>\r
310 <p>to review your changes.  When you're done,</p>\r
311 <div class="listingblock">\r
312 <div class="content">\r
313 <pre><tt>$ git commit -a</tt></pre>\r
314 </div></div>\r
315 <p>will again prompt your for a message describing the change, and then\r
316 record the new versions of the modified files.</p>\r
317 <p>A note on commit messages: Though not required, it's a good idea to\r
318 begin the commit message with a single short (less than 50 character)\r
319 line summarizing the change, followed by a blank line and then a more\r
320 thorough description.  Tools that turn commits into email, for\r
321 example, use the first line on the Subject line and the rest of the\r
322 commit in the body.</p>\r
323 <p>To add a new file, first create the file, then</p>\r
324 <div class="listingblock">\r
325 <div class="content">\r
326 <pre><tt>$ git add path/to/new/file</tt></pre>\r
327 </div></div>\r
328 <p>then commit as usual.  No special command is required when removing a\r
329 file; just remove it, then commit.</p>\r
330 <p>At any point you can view the history of your changes using</p>\r
331 <div class="listingblock">\r
332 <div class="content">\r
333 <pre><tt>$ git whatchanged</tt></pre>\r
334 </div></div>\r
335 <p>If you also want to see complete diffs at each step, use</p>\r
336 <div class="listingblock">\r
337 <div class="content">\r
338 <pre><tt>$ git whatchanged -p</tt></pre>\r
339 </div></div>\r
340 </div>\r
341 <h2>Managing branches</h2>\r
342 <div class="sectionbody">\r
343 <p>A single git repository can maintain multiple branches of\r
344 development.  To create a new branch named "experimental", use</p>\r
345 <div class="listingblock">\r
346 <div class="content">\r
347 <pre><tt>$ git branch experimental</tt></pre>\r
348 </div></div>\r
349 <p>If you now run</p>\r
350 <div class="listingblock">\r
351 <div class="content">\r
352 <pre><tt>$ git branch</tt></pre>\r
353 </div></div>\r
354 <p>you'll get a list of all existing branches:</p>\r
355 <div class="listingblock">\r
356 <div class="content">\r
357 <pre><tt>  experimental\r
358 * master</tt></pre>\r
359 </div></div>\r
360 <p>The "experimental" branch is the one you just created, and the\r
361 "master" branch is a default branch that was created for you\r
362 automatically.  The asterisk marks the branch you are currently on;\r
363 type</p>\r
364 <div class="listingblock">\r
365 <div class="content">\r
366 <pre><tt>$ git checkout experimental</tt></pre>\r
367 </div></div>\r
368 <p>to switch to the experimental branch.  Now edit a file, commit the\r
369 change, and switch back to the master branch:</p>\r
370 <div class="listingblock">\r
371 <div class="content">\r
372 <pre><tt>(edit file)\r
373 $ git commit -a\r
374 $ git checkout master</tt></pre>\r
375 </div></div>\r
376 <p>Check that the change you made is no longer visible, since it was\r
377 made on the experimental branch and you're back on the master branch.</p>\r
378 <p>You can make a different change on the master branch:</p>\r
379 <div class="listingblock">\r
380 <div class="content">\r
381 <pre><tt>(edit file)\r
382 $ git commit -a</tt></pre>\r
383 </div></div>\r
384 <p>at this point the two branches have diverged, with different changes\r
385 made in each.  To merge the changes made in the two branches, run</p>\r
386 <div class="listingblock">\r
387 <div class="content">\r
388 <pre><tt>$ git pull . experimental</tt></pre>\r
389 </div></div>\r
390 <p>If the changes don't conflict, you're done.  If there are conflicts,\r
391 markers will be left in the problematic files showing the conflict;</p>\r
392 <div class="listingblock">\r
393 <div class="content">\r
394 <pre><tt>$ git diff</tt></pre>\r
395 </div></div>\r
396 <p>will show this.  Once you've edited the files to resolve the\r
397 conflicts,</p>\r
398 <div class="listingblock">\r
399 <div class="content">\r
400 <pre><tt>$ git commit -a</tt></pre>\r
401 </div></div>\r
402 <p>will commit the result of the merge. Finally,</p>\r
403 <div class="listingblock">\r
404 <div class="content">\r
405 <pre><tt>$ gitk</tt></pre>\r
406 </div></div>\r
407 <p>will show a nice graphical representation of the resulting history.</p>\r
408 <p>If you develop on a branch crazy-idea, then regret it, you can always\r
409 delete the branch with</p>\r
410 <div class="listingblock">\r
411 <div class="content">\r
412 <pre><tt>$ git branch -D crazy-idea</tt></pre>\r
413 </div></div>\r
414 <p>Branches are cheap and easy, so this is a good way to try something\r
415 out.</p>\r
416 </div>\r
417 <h2>Using git for collaboration</h2>\r
418 <div class="sectionbody">\r
419 <p>Suppose that Alice has started a new project with a git repository in\r
420 /home/alice/project, and that Bob, who has a home directory on the\r
421 same machine, wants to contribute.</p>\r
422 <p>Bob begins with:</p>\r
423 <div class="listingblock">\r
424 <div class="content">\r
425 <pre><tt>$ git clone /home/alice/project myrepo</tt></pre>\r
426 </div></div>\r
427 <p>This creates a new directory "myrepo" containing a clone of Alice's\r
428 repository.  The clone is on an equal footing with the original\r
429 project, posessing its own copy of the original project's history.</p>\r
430 <p>Bob then makes some changes and commits them:</p>\r
431 <div class="listingblock">\r
432 <div class="content">\r
433 <pre><tt>(edit files)\r
434 $ git commit -a\r
435 (repeat as necessary)</tt></pre>\r
436 </div></div>\r
437 <p>When he's ready, he tells Alice to pull changes from the repository\r
438 at /home/bob/myrepo.  She does this with:</p>\r
439 <div class="listingblock">\r
440 <div class="content">\r
441 <pre><tt>$ cd /home/alice/project\r
442 $ git pull /home/bob/myrepo</tt></pre>\r
443 </div></div>\r
444 <p>This actually pulls changes from the branch in Bob's repository named\r
445 "master".  Alice could request a different branch by adding the name\r
446 of the branch to the end of the git pull command line.</p>\r
447 <p>This merges Bob's changes into her repository; "git whatchanged" will\r
448 now show the new commits.  If Alice has made her own changes in the\r
449 meantime, then Bob's changes will be merged in, and she will need to\r
450 manually fix any conflicts.</p>\r
451 <p>A more cautious Alice might wish to examine Bob's changes before\r
452 pulling them.  She can do this by creating a temporary branch just\r
453 for the purpose of studying Bob's changes:</p>\r
454 <div class="listingblock">\r
455 <div class="content">\r
456 <pre><tt>$ git fetch /home/bob/myrepo master:bob-incoming</tt></pre>\r
457 </div></div>\r
458 <p>which fetches the changes from Bob's master branch into a new branch\r
459 named bob-incoming.  (Unlike git pull, git fetch just fetches a copy\r
460 of Bob's line of development without doing any merging).  Then</p>\r
461 <div class="listingblock">\r
462 <div class="content">\r
463 <pre><tt>$ git whatchanged -p master..bob-incoming</tt></pre>\r
464 </div></div>\r
465 <p>shows a list of all the changes that Bob made since he branched from\r
466 Alice's master branch.</p>\r
467 <p>After examing those changes, and possibly fixing things, Alice can\r
468 pull the changes into her master branch:</p>\r
469 <div class="listingblock">\r
470 <div class="content">\r
471 <pre><tt>$ git checkout master\r
472 $ git pull . bob-incoming</tt></pre>\r
473 </div></div>\r
474 <p>The last command is a pull from the "bob-incoming" branch in Alice's\r
475 own repository.</p>\r
476 <p>Later, Bob can update his repo with Alice's latest changes using</p>\r
477 <div class="listingblock">\r
478 <div class="content">\r
479 <pre><tt>$ git pull</tt></pre>\r
480 </div></div>\r
481 <p>Note that he doesn't need to give the path to Alice's repository;\r
482 when Bob cloned Alice's repository, git stored the location of her\r
483 repository in the file .git/remotes/origin, and that location is used\r
484 as the default for pulls.</p>\r
485 <p>Bob may also notice a branch in his repository that he didn't create:</p>\r
486 <div class="listingblock">\r
487 <div class="content">\r
488 <pre><tt>$ git branch\r
489 * master\r
490   origin</tt></pre>\r
491 </div></div>\r
492 <p>The "origin" branch, which was created automatically by "git clone",\r
493 is a pristine copy of Alice's master branch; Bob should never commit\r
494 to it.</p>\r
495 <p>If Bob later decides to work from a different host, he can still\r
496 perform clones and pulls using the ssh protocol:</p>\r
497 <div class="listingblock">\r
498 <div class="content">\r
499 <pre><tt>$ git clone alice.org:/home/alice/project myrepo</tt></pre>\r
500 </div></div>\r
501 <p>Alternatively, git has a native protocol, or can use rsync or http;\r
502 see <a href="git-pull.html">git-pull(1)</a> for details.</p>\r
503 <p>Git can also be used in a CVS-like mode, with a central repository\r
504 that various users push changes to; see <a href="git-push.html">git-push(1)</a> and\r
505 <a href="cvs-migration.html">git for CVS users</a>.</p>\r
506 </div>\r
507 <h2>Keeping track of history</h2>\r
508 <div class="sectionbody">\r
509 <p>Git history is represented as a series of interrelated commits.  The\r
510 most recent commit in the currently checked-out branch can always be\r
511 referred to as HEAD, and the "parent" of any commit can always be\r
512 referred to by appending a caret, "^", to the end of the name of the\r
513 commit.  So, for example,</p>\r
514 <div class="listingblock">\r
515 <div class="content">\r
516 <pre><tt>git diff HEAD^ HEAD</tt></pre>\r
517 </div></div>\r
518 <p>shows the difference between the most-recently checked-in state of\r
519 the tree and the previous state, and</p>\r
520 <div class="listingblock">\r
521 <div class="content">\r
522 <pre><tt>git diff HEAD^^ HEAD^</tt></pre>\r
523 </div></div>\r
524 <p>shows the difference between that previous state and the state two\r
525 commits ago.  Also, HEAD~5 can be used as a shorthand for HEAD<sup>^</sup>^^,\r
526 and more generally HEAD~n can refer to the nth previous commit.\r
527 Commits representing merges have more than one parent, and you can\r
528 specify which parent to follow in that case; see\r
529 <a href="git-rev-parse.html">git-rev-parse(1)</a>.</p>\r
530 <p>The name of a branch can also be used to refer to the most recent\r
531 commit on that branch; so you can also say things like</p>\r
532 <div class="listingblock">\r
533 <div class="content">\r
534 <pre><tt>git diff HEAD experimental</tt></pre>\r
535 </div></div>\r
536 <p>to see the difference between the most-recently committed tree in\r
537 the current branch and the most-recently committed tree in the\r
538 experimental branch.</p>\r
539 <p>But you may find it more useful to see the list of commits made in\r
540 the experimental branch but not in the current branch, and</p>\r
541 <div class="listingblock">\r
542 <div class="content">\r
543 <pre><tt>git whatchanged HEAD..experimental</tt></pre>\r
544 </div></div>\r
545 <p>will do that, just as</p>\r
546 <div class="listingblock">\r
547 <div class="content">\r
548 <pre><tt>git whatchanged experimental..HEAD</tt></pre>\r
549 </div></div>\r
550 <p>will show the list of commits made on the HEAD but not included in\r
551 experimental.</p>\r
552 <p>You can also give commits convenient names of your own: after running</p>\r
553 <div class="listingblock">\r
554 <div class="content">\r
555 <pre><tt>$ git-tag v2.5 HEAD^^</tt></pre>\r
556 </div></div>\r
557 <p>you can refer to HEAD^^ by the name "v2.5".  If you intend to share\r
558 this name with other people (for example, to identify a release\r
559 version), you should create a "tag" object, and perhaps sign it; see\r
560 <a href="git-tag.html">git-tag(1)</a> for details.</p>\r
561 <p>You can revisit the old state of a tree, and make further\r
562 modifications if you wish, using git branch: the command</p>\r
563 <div class="listingblock">\r
564 <div class="content">\r
565 <pre><tt>$ git branch stable-release v2.5</tt></pre>\r
566 </div></div>\r
567 <p>will create a new branch named "stable-release" starting from the\r
568 commit which you tagged with the name v2.5.</p>\r
569 <p>You can reset the state of any branch to an earlier commit at any\r
570 time with</p>\r
571 <div class="listingblock">\r
572 <div class="content">\r
573 <pre><tt>$ git reset --hard v2.5</tt></pre>\r
574 </div></div>\r
575 <p>This will remove all later commits from this branch and reset the\r
576 working tree to the state it had when the given commit was made.  If\r
577 this branch is the only branch containing the later commits, those\r
578 later changes will be lost.  Don't use "git reset" on a\r
579 publicly-visible branch that other developers pull from, as git will\r
580 be confused by history that disappears in this way.</p>\r
581 </div>\r
582 <h2>Next Steps</h2>\r
583 <div class="sectionbody">\r
584 <p>Some good commands to explore next:</p>\r
585 <ul>\r
586 <li>\r
587 <p>\r
588 <a href="git-diff.html">git-diff(1)</a>: This flexible command does much more than\r
589     we've seen in the few examples above.\r
590 </p>\r
591 </li>\r
592 <li>\r
593 <p>\r
594 <a href="git-format-patch.html">git-format-patch(1)</a>, <a href="git-am.html">git-am(1)</a>: These convert\r
595     series of git commits into emailed patches, and vice versa,\r
596     useful for projects such as the linux kernel which rely heavily\r
597     on emailed patches.\r
598 </p>\r
599 </li>\r
600 <li>\r
601 <p>\r
602 <a href="git-bisect.html">git-bisect(1)</a>: When there is a regression in your\r
603     project, one way to track down the bug is by searching through\r
604     the history to find the exact commit that's to blame.  Git bisect\r
605     can help you perform a binary search for that commit.  It is\r
606     smart enough to perform a close-to-optimal search even in the\r
607     case of complex non-linear history with lots of merged branches.\r
608 </p>\r
609 </li>\r
610 </ul>\r
611 <p>Other good starting points include <a href="everyday.html">Everday GIT\r
612 with 20 Commands Or So</a> and <a href="cvs-migration.html">git for CVS\r
613 users</a>.  Also, <a href="core-tutorial.html">A short git tutorial</a> gives an\r
614 introduction to lower-level git commands for advanced users and\r
615 developers.</p>\r
616 </div>\r
617 <div id="footer">\r
618 <div id="footer-text">\r
619 Last updated 22-Jan-2006 23:54:22 PDT\r
620 </div>\r
621 </div>\r
622 </body>\r
623 </html>\r