Merge Git book to Help document
[TortoiseGit.git] / doc / source / en / TortoiseGit / git_book / gitbook.xml
blobb18aac81588fa519085e0026fc26c7d4e5e77b22
1 <?xml version='1.0'?>
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4 <chapter id="git-book">
5 <title>Git Basic</title>
7 <sect1 id="Git_Community_Boo"><title>Git Community Book</title>
8 <sect2 id="Git_Book_Preface"><title>Preface</title>
9 <para>The open Git resource pulled together by the whole
10 community</para>
12 </sect2><sect2 id="Author"><title>Authors</title>
13 Thank these guys: 
14 <para>Alecs King (alecsk@gmail.com), Amos Waterland
15 (apw@rossby.metr.ou.edu), Andrew Ruder (andy@aeruder.net), Andy
16 Parkins (andyparkins@gmail.com), Arjen Laarhoven (arjen@yaph.org),
17 Brian Hetro (whee@smaertness.net), Carl Worth (cworth@cworth.org),
18 Christian Meder (chris@absolutegiganten.org), Dan McGee
19 (dpmcgee@gmail.com), David Kastrup (dak@gnu.org), Dmitry V. Levin
20 (ldv@altlinux.org), Francis Daly (francis@daoine.org), Gerrit Pape
21 (pape@smarden.org), Greg Louis (glouis@dynamicro.ca), Gustaf
22 Hendeby (hendeby@isy.liu.se), Horst H. von Brand
23 (vonbrand@inf.utfsm.cl), J. Bruce Fields (bfields@fieldses.org),
24 Jakub Narebski (jnareb@gmail.com), Jim Meyering (jim@meyering.net),
25 Johan Herland (johan@herland.net), Johannes Schindelin
26 (Johannes.Schindelin@gmx.de), Jon Loeliger (jdl@freescale.org),
27 Josh Triplett (josh@freedesktop.org), Junio C Hamano
28 (gitster@pobox.com), Linus Torvalds (torvalds@osdl.org), Lukas
29 Sandström (lukass@etek.chalmers.se), Marcus Fritzsch
30 (m@fritschy.de), Michael Coleman (tutufan@gmail.com), Michael Smith
31 (msmith@cbnco.com), Mike Coleman (tutufan@gmail.com), Miklos Vajna
32 (vmiklos@frugalware.org), Nicolas Pitre (nico@cam.org), Oliver
33 Steele (steele@osteele.com), Paolo Ciarrocchi
34 (paolo.ciarrocchi@gmail.com), Pavel Roskin (proski@gnu.org), Ralf
35 Wildenhues (Ralf.Wildenhues@gmx.de), Robin Rosenberg
36 (robin.rosenberg.lists@dewire.com), Santi Béjar
37 (sbejar@gmail.com), Scott Chacon (schacon@gmail.com), Sergei
38 Organov (osv@javad.com), Shawn Bohrer (shawn.bohrer@gmail.com),
39 Shawn O. Pearce (spearce@spearce.org), Steffen Prohaska
40 (prohaska@zib.de), Tom Prince (tom.prince@ualberta.net), William
41 Pursell (bill.pursell@gmail.com), Yasushi SHOJI
42 (yashi@atmark-techno.com)</para>
43 </sect2><sect2 id="Maintainer___Edito"><title>Maintainer /
44 Editor</title>
45 Bug this guy: 
46 <para>Scott Chacon (schacon@gmail.com)</para>
48 </sect2></sect1><sect1 id="Introductio"><title>Introduction</title>
49 <sect2 id="Welcome_to_Gi"><title>Welcome to Git</title>
50 <para>Welcome to Git - the fast, distributed version control
51 system.</para>
52 <para>This book is meant to be a starting point for people new to
53 Git to learn it as quickly and easily as possible.</para>
54 <para>This book will start out by introducing you to the way Git
55 stores data, to give you the context for why it is different than
56 other VCS tools. This is meant to take you about 20 minutes.</para>
57 <para>Next we will cover <emphasis>Basic Git Usage</emphasis> - the
58 commands you will be using 90% of the time. These should give you a
59 good basis to use Git comfortably for most of what you're going to
60 use it for. This section should take you about 30 minutes to read
61 through.</para>
62 <para>Next we will go over <emphasis>Intermediate Git
63 Usage</emphasis> - things that are slightly more complex, but may
64 replace some of the basic commands you learned in the first
65 section. This will mostly be tricks and commands that will feel
66 more comfortable after you know the basic commands.</para>
67 <para>After you have all of that mastered, we will cover
68 <emphasis>Advanced Git</emphasis> - commands that most people
69 probably don't use very often, but can be very helpful in certain
70 situations. Learning these commands should round out your
71 day-to-day git knowledge; you will be a master of the Git!</para>
72 <para>Now that you know Git, we will then cover <emphasis>Working
73 with Git</emphasis>. Here we will go over how to use Git in
74 scripts, with deployment tools, with editors and more. These
75 sections are meant to help you integrate Git into your
76 environment.</para>
77 <para>Lastly, we will have a series of articles on
78 <emphasis>low-level documentation</emphasis> that may help the Git
79 hackers who want to learn how the actual internals and protocols
80 work in Git.</para>
81 <sect3 id="Feedback_and_Contributin"><title>Feedback and
82 Contributing</title>
83 <para>At any point, if you see a mistake or want to contribute to
84 the book, you can send me an email at
85 <ulink url="mailto:schacon@gmail.com">schacon@gmail.com</ulink>, or
86 you can clone the source of this book at
87 <ulink url="http://github.com/schacon/gitbook">http://github.com/schacon/gitbook</ulink>
88 and send me a patch or a pull-request.</para>
89 </sect3><sect3 id="Reference"><title>References</title>
90 <para>Much of this book is pulled together from different sources
91 and then added to.If you would like to read some of the original
92 articles or resources, please visit them and thank the
93 authors:</para>
94 <itemizedlist>
95 <listitem><ulink url="http://www.kernel.org/pub/software/scm/git/docs/user-manual.html">Git
96 User Manual</ulink></listitem>
97 <listitem><ulink url="http://www.kernel.org/pub/software/scm/git/docs/gittutorial.html">The
98 Git Tutorial</ulink></listitem>
99 <listitem><ulink url="http://www.kernel.org/pub/software/scm/git/docs/gittutorial-2.html">The
100 Git Tutorial pt 2</ulink></listitem>
101 <listitem><ulink url="http://osteele.com/archives/2008/05/my-git-workflow">"My
102 Git Workflow" blog post</ulink></listitem>
103 </itemizedlist>
104 </sect3></sect2><sect2 id="The_Git_Object_Mode"><title>The Git
105 Object Model</title>
106 <sect3 id="The_SH"><title>The SHA</title>
107 <para>All the information needed to represent the history of a
108 project is stored in files referenced by a 40-digit "object name"
109 that looks something like this:</para>
110 <programlisting>
111 <literal>6ff87c4664981e4397625791c8ea3bbb5f2279a3
112 </literal>
113 </programlisting>
115 <para>You will see these 40-character strings all over the place in
116 Git. In each case the name is calculated by taking the SHA1 hash of
117 the contents of the object. The SHA1 hash is a cryptographic hash
118 function. What that means to us is that it is virtually impossible
119 to find two different objects with the same name. This has a number
120 of advantages; among others:</para>
121 <itemizedlist>
122 <listitem>Git can quickly determine whether two objects are
123 identical or not, just by comparing names.</listitem>
124 <listitem>Since object names are computed the same way in every
125 repository, the same content stored in two repositories will always
126 be stored under the same name.</listitem>
127 <listitem>Git can detect errors when it reads an object, by
128 checking that the object's name is still the SHA1 hash of its
129 contents.</listitem>
130 </itemizedlist>
131 </sect3><sect3 id="The_Object"><title>The Objects</title>
132 <para>Every object consists of three things - a
133 <emphasis>type</emphasis>, a <emphasis>size</emphasis> and
134 <emphasis>content</emphasis>. The <emphasis>size</emphasis> is
135 simply the size of the contents, the contents depend on what type
136 of object it is, and there are four different types of objects:
137 "blob", "tree", "commit", and "tag".</para>
138 <itemizedlist>
139 <listitem>A <emphasis>"blob"</emphasis> is used to
140 store file data - it is generally a file.</listitem>
141 <listitem>A <emphasis>"tree"</emphasis> is basically
142 like a directory - it references a bunch of other trees and/or
143 blobs (i.e. files and sub-directories)</listitem>
144 <listitem>A <emphasis>"commit"</emphasis> points to a
145 single tree, marking it as what the project looked like at a
146 certain point in time. It contains meta-information about that
147 point in time, such as a timestamp, the author of the changes since
148 the last commit, a pointer to the previous commit(s),
149 etc.</listitem>
150 <listitem>A <emphasis>"tag"</emphasis>is a way to
151 mark a specific commit as special in some way. It is normally used
152 to tag certain commits as specific releases or something along
153 those lines.</listitem>
154 </itemizedlist>
155 <para>Almost all of Git is built around manipulating this simple
156 structure of four different object types. It is sort of its own
157 little filesystem that sits on top of your machine's
158 filesystem.</para>
159 </sect3><sect3 id="Different_from_SV"><title>Different from
160 SVN</title>
161 <para>It is important to note that this is very different from most
162 SCM systems that you may be familiar with. Subversion, CVS,
163 Perforce, Mercurial and the like all use <emphasis>Delta
164 Storage</emphasis> systems - they store the differences between one
165 commit and the next. Git does not do this - it stores a snapshot of
166 what all the files in your project look like in this tree structure
167 each time you commit. This is a very important concept to
168 understand when using Git.</para>
169 </sect3><sect3 id="Blob_Objec"><title>Blob Object</title>
170 <para>A blob generally stores the contents of a file.</para>
171 <inlinemediaobject><imageobject>
172 <imagedata fileref="../images/object-blob.png"/>
173 </imageobject></inlinemediaobject>
176 <para>You can use
177 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-show.html">git
178 show</ulink> to examine the contents of any blob. Assuming we have
179 the SHA for a blob, we can examine its contents like this:</para>
180 <programlisting>
181 <literal>$ git show 6ff87c4664
183  Note that the only valid version of the GPL as far as this project
184  is concerned is _this_ particular version of the license (ie v2, not
185  v2.2 or v3.x or whatever), unless explicitly otherwise stated.
187 </literal>
188 </programlisting>
190 <para>A "blob" object is nothing but a chunk of binary data. It
191 doesn't refer to anything else or have attributes of any kind, not
192 even a file name.</para>
193 <para>Since the blob is entirely defined by its data, if two files
194 in a directory tree (or in multiple different versions of the
195 repository) have the same contents, they will share the same blob
196 object. The object is totally independent of its location in the
197 directory tree, and renaming a file does not change the object that
198 file is associated with.</para>
199 </sect3><sect3 id="Tree_Objec"><title>Tree Object</title>
200 <para>A tree is a simple object that has a bunch of pointers to
201 blobs and other trees - it generally represents the contents of a
202 directory or subdirectory.</para>
203 <inlinemediaobject><imageobject>
204 <imagedata fileref="../images/object-tree.png"/>
205 </imageobject></inlinemediaobject>
208 <para>The ever-versatile
209 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-show.html">git
210 show</ulink> command can also be used to examine tree objects, but
211 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-ls-tree.html">git
212 ls-tree</ulink> will give you more details. Assuming we have the
213 SHA for a tree, we can examine it like this:</para>
214 <programlisting>
215 <literal>$ git ls-tree fb3a8bdd0ce
216 100644 blob 63c918c667fa005ff12ad89437f2fdc80926e21c    .gitignore
217 100644 blob 5529b198e8d14decbe4ad99db3f7fb632de0439d    .mailmap
218 100644 blob 6ff87c4664981e4397625791c8ea3bbb5f2279a3    COPYING
219 040000 tree 2fb783e477100ce076f6bf57e4a6f026013dc745    Documentation
220 100755 blob 3c0032cec592a765692234f1cba47dfdcc3a9200    GIT-VERSION-GEN
221 100644 blob 289b046a443c0647624607d471289b2c7dcd470b    INSTALL
222 100644 blob 4eb463797adc693dc168b926b6932ff53f17d0b1    Makefile
223 100644 blob 548142c327a6790ff8821d67c2ee1eff7a656b52    README
225 </literal>
226 </programlisting>
228 <para>As you can see, a tree object contains a list of entries,
229 each with a mode, object type, SHA1 name, and name, sorted by name.
230 It represents the contents of a single directory tree.</para>
231 <para>An object referenced by a tree may be blob, representing the
232 contents of a file, or another tree, representing the contents of a
233 subdirectory. Since trees and blobs, like all other objects, are
234 named by the SHA1 hash of their contents, two trees have the same
235 SHA1 name if and only if their contents (including, recursively,
236 the contents of all subdirectories) are identical. This allows git
237 to quickly determine the differences between two related tree
238 objects, since it can ignore any entries with identical object
239 names.</para>
240 <para>(Note: in the presence of submodules, trees may also have
241 commits as entries. See the <emphasis>Submodules</emphasis>
242 section.)</para>
243 <para>Note that the files all have mode 644 or 755: git actually
244 only pays attention to the executable bit.</para>
245 </sect3><sect3 id="Commit_Objec"><title>Commit Object</title>
246 <para>The "commit" object links a physical state of a tree with a
247 description of how we got there and why.</para>
248 <inlinemediaobject><imageobject>
249 <imagedata fileref="../images/object-commit.png"/>
250 </imageobject></inlinemediaobject>
253 <para>You can use the --pretty=raw option to
254 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-show.html">git
255 show</ulink> or
256 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-log.html">git
257 log</ulink> to examine your favorite commit:</para>
258 <programlisting>
259 <literal>$ git show -s --pretty=raw 2be7fcb476
260 commit 2be7fcb4764f2dbcee52635b91fedb1b3dcf7ab4
261 tree fb3a8bdd0ceddd019615af4d57a53f43d8cee2bf
262 parent 257a84d9d02e90447b149af58b271c19405edb6a
263 author Dave Watson &lt;dwatson@mimvista.com&gt; 1187576872 -0400
264 committer Junio C Hamano &lt;gitster@pobox.com&gt; 1187591163 -0700
266     Fix misspelling of 'suppress' in docs
268     Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
269 </literal>
270 </programlisting>
272 <para>As you can see, a commit is defined by:</para>
273 <itemizedlist>
274 <listitem>a <emphasis>tree</emphasis>: The SHA1 name
275 of a tree object (as defined below), representing the contents of a
276 directory at a certain point in time.</listitem>
277 <listitem><emphasis>parent(s)</emphasis>: The SHA1
278 name of some number of commits which represent the immediately
279 previous step(s) in the history of the project. The example above
280 has one parent; merge commits may have more than one. A commit with
281 no parents is called a "root" commit, and represents the initial
282 revision of a project. Each project must have at least one root. A
283 project can also have multiple roots, though that isn't common (or
284 necessarily a good idea).</listitem>
285 <listitem>an <emphasis>author</emphasis>: The name of
286 the person responsible for this change, together with its
287 date.</listitem>
288 <listitem>a <emphasis>committer</emphasis>: The name
289 of the person who actually created the commit, with the date it was
290 done. This may be different from the author; for example, if the
291 author wrote a patch and emailed it to another person who used the
292 patch to create the commit.</listitem>
293 <listitem>a <emphasis>comment</emphasis> describing
294 this commit.</listitem>
295 </itemizedlist>
296 <para>Note that a commit does not itself contain any information
297 about what actually changed; all changes are calculated by
298 comparing the contents of the tree referred to by this commit with
299 the trees associated with its parents. In particular, git does not
300 attempt to record file renames explicitly, though it can identify
301 cases where the existence of the same file data at changing paths
302 suggests a rename. (See, for example, the -M option to
303 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-diff.html">git
304 diff</ulink>).</para>
305 <para>A commit is usually created by
306 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-commit.html">git
307 commit</ulink>, which creates a commit whose parent is normally the
308 current HEAD, and whose tree is taken from the content currently
309 stored in the index.</para>
310 </sect3><sect3 id="The_Object_Mode"><title>The Object Model</title>
311 <para>So, now that we've looked at the 3 main object types (blob,
312 tree and commit), let's take a quick look at how they all fit
313 together.</para>
314 <para>If we had a simple project with the following directory
315 structure:</para>
316 <programlisting>
317 <literal>$&gt;tree
319 |-- README
320 `-- lib
321     |-- inc
322     |   `-- tricks.rb
323     `-- mylib.rb
325 2 directories, 3 files
326 </literal>
327 </programlisting>
329 <para>And we committed this to a Git repository, it would be
330 represented like this:</para>
331 <inlinemediaobject><imageobject>
332 <imagedata fileref="../images/objects-example.png"/>
333 </imageobject></inlinemediaobject>
336 <para>You can see that we have created a <emphasis>tree</emphasis>
337 object for each directory (including the root) and a
338 <emphasis>blob</emphasis> object for each file. Then we have a
339 <emphasis>commit</emphasis> object to point to the root, so we can
340 track what our project looked like when it was committed.</para>
341 </sect3><sect3 id="Tag_Objec"><title>Tag Object</title>
342 <inlinemediaobject><imageobject>
343 <imagedata fileref="../images/object-tag.png"/>
344 </imageobject></inlinemediaobject>
347 <para>A tag object contains an object name (called simply
348 'object'), object type, tag name, the name of the person ("tagger")
349 who created the tag, and a message, which may contain a signature,
350 as can be seen using
351 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-cat-file.html">git
352 cat-file</ulink>:</para>
353 <programlisting>
354 <literal>$ git cat-file tag v1.5.0
355 object 437b1b20df4b356c9342dac8d38849f24ef44f27
356 type commit
357 tag v1.5.0
358 tagger Junio C Hamano &lt;junkio@cox.net&gt; 1171411200 +0000
360 GIT 1.5.0
361 -----BEGIN PGP SIGNATURE-----
362 Version: GnuPG v1.4.6 (GNU/Linux)
364 iD8DBQBF0lGqwMbZpPMRm5oRAuRiAJ9ohBLd7s2kqjkKlq1qqC57SbnmzQCdG4ui
365 nLE/L9aUXdWeTFPron96DLA=
366 =2E+0
367 -----END PGP SIGNATURE-----
368 </literal>
369 </programlisting>
371 <para>See the
372 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html">git
373 tag</ulink> command to learn how to create and verify tag objects.
374 (Note that
375 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html">git
376 tag</ulink> can also be used to create "lightweight tags", which
377 are not tag objects at all, but just simple references whose names
378 begin with "refs/tags/").</para>
379 </sect3></sect2><sect2 id="Git_Directory_and_Working_Director"><title>Git
380 Directory and Working Directory</title>
381 <sect3 id="The_Git_Director"><title>The Git Directory</title>
382 <para>The 'git directory' is the directory that stores all Git's
383 history and meta information for your project - including all of
384 the objects (commits, trees, blobs, tags), all of the pointers to
385 where different branches are and more.</para>
386 <para>There is only one Git Directory per project (as opposed to
387 one per subdirectory like with SVN or CVS), and that directory is
388 (by default, though not necessarily) '.git' in the root of your
389 project. If you look at the contents of that directory, you can see
390 all of your important files:</para>
391 <programlisting>
392 <literal>$&gt;tree -L 1
394 |-- HEAD         # pointer to your current branch
395 |-- config       # your configuration preferences
396 |-- description  # description of your project 
397 |-- hooks/       # pre/post action hooks
398 |-- index        # index file (see next section)
399 |-- logs/        # a history of where your branches have been
400 |-- objects/     # your objects (commits, trees, blobs, tags)
401 `-- refs/        # pointers to your branches
402 </literal>
403 </programlisting>
405 <para>(there may be some other files/directories in there as well,
406 but they are not important for now)</para>
407 </sect3><sect3 id="The_Working_Director"><title>The Working
408 Directory</title>
409 <para>The Git 'working directory' is the directory that holds the
410 current checkout of the files you are working on. Files in this
411 directory are often removed or replaced by Git as you switch
412 branches - this is normal. All your history is stored in the Git
413 Directory; the working directory is simply a temporary checkout
414 place where you can modify the files until your next commit.</para>
415 </sect3></sect2><sect2 id="The_Git_Inde"><title>The Git
416 Index</title>
417 <para>The Git index is used as a staging area between your working
418 directory and your repository. You can use the index to build up a
419 set of changes that you want to commit together. When you create a
420 commit, what is committed is what is currently in the index, not
421 what is in your working directory.</para>
422 <sect3 id="Looking_at_the_Inde"><title>Looking at the Index</title>
423 <para>The easiest way to see what is in the index is with the
424 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-status.html">git
425 status</ulink> command. When you run git status, you can see which
426 files are staged (currently in your index), which are modified but
427 not yet staged, and which are completely untracked.</para>
428 <programlisting>
429 <literal>$&gt;git status
430 # On branch master
431 # Your branch is behind 'origin/master' by 11 commits, and can be fast-forwarded.
433 # Changes to be committed:
434 #   (use "git reset HEAD &lt;file&gt;..." to unstage)
436 #   modified:   daemon.c
438 # Changed but not updated:
439 #   (use "git add &lt;file&gt;..." to update what will be committed)
441 #   modified:   grep.c
442 #   modified:   grep.h
444 # Untracked files:
445 #   (use "git add &lt;file&gt;..." to include in what will be committed)
447 #   blametree
448 #   blametree-init
449 #   git-gui/git-citool
450 </literal>
451 </programlisting>
453 <para>If you blow the index away entirely, you generally haven't
454 lost any information as long as you have the name of the tree that
455 it described.</para>
456 <para>And with that, you should have a pretty good understanding of
457 the basics of what Git is doing behind the scenes, and why it is a
458 bit different than most other SCM systems. Don't worry if you don't
459 totally understand it all right now; we'll revisit all of these
460 topics in the next sections. Now we're ready to move on to
461 installing, configuring and using Git.</para>
462 </sect3></sect2></sect1><sect1 id="First_Tim"><title>First
463 Time</title>
464 <sect2 id="Installing_Gi"><title>Installing Git</title>
465 <sect3 id="Installing_from_Sourc"><title>Installing from
466 Source</title>
467 <para>In short, on a Unix-based system, you can download the Git
468 source code from the <ulink url="http://git-scm.com/download">Git
469 Download Page</ulink>, and then run something along the lines of
470 :</para>
471 <programlisting>
472 <literal>$ make prefix=/usr all ;# as yourself
473 $ make prefix=/usr install ;# as root
474 </literal>
475 </programlisting>
477 <para>You will need the
478 <ulink url="http://expat.sourceforge.net/">expat</ulink>,
479 <ulink url="http://curl.linux-mirror.org">curl</ulink>,
480 <ulink url="http://www.zlib.net">zlib</ulink>, and
481 <ulink url="http://www.openssl.org">openssl</ulink> libraries
482 installed - though with the possible exception of
483 <emphasis>expat</emphasis>, these will normally already be
484 there.</para>
485 </sect3><sect3 id="Linu"><title>Linux</title>
486 <para>If you are running Linux, you can likely install Git easily
487 via your native package management system:</para>
488 <programlisting>
489 <literal>$ yum install git-core
491 $ apt-get install git-core
492 </literal>
493 </programlisting>
495 <para>If that doesn't work, you can download the .deb or .rpm
496 packages from here:</para>
497 <para><ulink url="http://kernel.org/pub/software/scm/git/RPMS/">RPM
498 Packages</ulink></para>
499 <para><ulink url="http://www.backports.org/debian/pool/main/g/git-core/">Stable
500 Debs</ulink></para>
501 <para>If you prefer to install from source on a Linux system, this
502 article may be helpful:</para>
503 <para><ulink url="http://chrisolsen.org/2008/03/10/installing-git-on-ubuntu/">Article:
504 Installing Git on Ubuntu</ulink></para>
505 </sect3><sect3 id="Mac_10."><title>Mac 10.4</title>
506 <para>In both Mac 10.4 and 10.5, you can install Git via
507 <ulink url="http://www.macports.org/">MacPorts</ulink>, if you have
508 that installed. If not, you can install it from
509 <ulink url="http://www.macports.org/install.php">here</ulink>.</para>
510 <para>Once MacPorts is installed, all you should have to do
511 is:</para>
512 <programlisting>
513 <literal>$ sudo port install git-core
514 </literal>
515 </programlisting>
517 <para>If you prefer to install from source, these articles may be
518 helpful:</para>
519 <para><ulink url="http://rails.wincent.com/wiki/Installing_Git_1.5.2.3_on_Mac_OS_X_Tiger">Article:
520 Installing Git on Tiger</ulink></para>
521 <para><ulink url="http://larrytheliquid.com/2007/12/29/compiling-git-and-git-svn-on-osx-tiger/">Article:
522 Installing Git and git-svn on Tiger from source</ulink></para>
523 </sect3><sect3 id="Mac_10."><title>Mac 10.5</title>
524 <para>With Leopard, you can also install via MacPorts, but here you
525 have the additional option of using a nice installer, which you can
526 download from here:
527 <ulink url="http://code.google.com/p/git-osx-installer/downloads/list?can=3">Git
528 OSX Installer</ulink></para>
529 <para>If you prefer to install it from source, these guides may be
530 particularly helpful to you :</para>
531 <para><ulink url="http://solutions.treypiepmeier.com/2008/02/25/installing-git-on-os-x-leopard/">Article:
532 Installing Git on OSX Leopard</ulink></para>
533 <para><ulink url="http://dysinger.net/2007/12/30/installing-git-on-mac-os-x-105-leopard/">Article:
534 Installing Git on OS 10.5</ulink></para>
535 </sect3><sect3 id="Window"><title>Windows</title>
536 <para>On Windows, installing Git is pretty easy. Simply download
537 and install the
538 <ulink url="http://code.google.com/p/msysgit/downloads/list">msysGit</ulink>
539 package.</para>
540 <para>See the <emphasis>Git on Windows</emphasis> chapter for a
541 screencast demonstrating installing and using Git on
542 Windows.</para>
543 </sect3></sect2><sect2 id="Setup_and_Initializatio"><title>Setup
544 and Initialization</title>
545 <sect3 id="Git_Confi"><title>Git Config</title>
546 <para>The first thing you're going to want to do is set up your
547 name and email address for Git to use to sign your commits.</para>
548 <programlisting>
549 <literal>$ git config --global user.name "Scott Chacon"
550 $ git config --global user.email "schacon@gmail.com"
551 </literal>
552 </programlisting>
554 <para>That will set up a file in your home directory which may be
555 used by any of your projects. By default that file is
556 <emphasis>~/.gitconfig</emphasis> and the contents will look like
557 this:</para>
558 <programlisting>
559 <literal>[user]
560         name = Scott Chacon
561         email = schacon@gmail.com
562 </literal>
563 </programlisting>
565 <para>If you want to override those values for a specific project
566 (to use a work email address, for example), you can run the
567 <emphasis>git config</emphasis> command without the
568 <emphasis>--global</emphasis> option while in that project. This
569 will add a [user] section like the one shown above to the
570 <emphasis>.git/config</emphasis> file in your project's root
571 directory.</para>
572 </sect3></sect2></sect1><sect1 id="Basic_Usag"><title>Basic
573 Usage</title>
574 <sect2 id="Getting_a_Git_Repositor"><title>Getting a Git
575 Repository</title>
576 <para>So now that we're all set up, we need a Git repository. We
577 can do this one of two ways - we can <emphasis>clone</emphasis> one
578 that already exists, or we can <emphasis>initialize</emphasis> one
579 either from existing files that aren't in source control yet, or
580 from an empty directory.</para>
581 <sect3 id="Cloning_a_Repositor"><title>Cloning a Repository</title>
582 <para>In order to get a copy of a project, you will need to know
583 the project's Git URL - the location of the repository. Git can
584 operate over many different protocols, so it may begin with ssh://,
585 http(s)://, git://, or just a username (in which case git will
586 assume ssh). Some repositories may be accessed over more than one
587 protocol. For example, the source code to Git itself can be cloned
588 either over the git:// protocol:</para>
589 <programlisting>
590 <literal>git clone git://git.kernel.org/pub/scm/git/git.git
591 </literal>
592 </programlisting>
594 <para>or over http:</para>
595 <programlisting>
596 <literal>git clone http://www.kernel.org/pub/scm/git/git.git
597 </literal>
598 </programlisting>
600 <para>The git:// protocol is faster and more efficient, but
601 sometimes it is necessary to use http when behind corporate
602 firewalls or what have you. In either case you should then have a
603 new directory named 'git' that contains all the Git source code and
604 history - it is basically a complete copy of what was on the
605 server.</para>
606 <para>By default, Git will name the new directory it has checked
607 out your cloned code into after whatever comes directly before the
608 '.git' in the path of the cloned project. (ie. <emphasis>git clone
609 http://git.kernel.org/linux/kernel/git/torvalds/linux-2.6.git</emphasis>
610 will result in a new directory named 'linux-2.6')</para>
611 </sect3><sect3 id="Initializing_a_New_Repositor"><title>Initializing
612 a New Repository</title>
613 <para>Assume you have a tarball named project.tar.gz with your
614 initial work. You can place it under git revision control as
615 follows.</para>
616 <programlisting>
617 <literal>$ tar xzf project.tar.gz
618 $ cd project
619 $ git init
620 </literal>
621 </programlisting>
623 <para>Git will reply</para>
624 <programlisting>
625 <literal>Initialized empty Git repository in .git/
626 </literal>
627 </programlisting>
629 <para>You've now initialized the working directory--you may notice
630 a new directory created, named ".git".</para>
631 </sect3></sect2><sect2 id="Normal_Workflo"><title>Normal
632 Workflow</title>
633 <para>Modify some files, then add their updated contents to the
634 index:</para>
635 <programlisting>
636 <literal>$ git add file1 file2 file3
637 </literal>
638 </programlisting>
640 <para>You are now ready to commit. You can see what is about to be
641 committed using
642 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-diff.html">git
643 diff</ulink> with the --cached option:</para>
644 <programlisting>
645 <literal>$ git diff --cached
646 </literal>
647 </programlisting>
649 <para>(Without --cached,
650 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-diff.html">git
651 diff</ulink> will show you any changes that you've made but not yet
652 added to the index.) You can also get a brief summary of the
653 situation with
654 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-status.html">git
655 status</ulink>:</para>
656 <programlisting>
657 <literal>$ git status
658 # On branch master
659 # Changes to be committed:
660 #   (use "git reset HEAD &lt;file&gt;..." to unstage)
662 #   modified:   file1
663 #   modified:   file2
664 #   modified:   file3
666 </literal>
667 </programlisting>
669 <para>If you need to make any further adjustments, do so now, and
670 then add any newly modified content to the index. Finally, commit
671 your changes with:</para>
672 <programlisting>
673 <literal>$ git commit
674 </literal>
675 </programlisting>
677 <para>This will again prompt you for a message describing the
678 change, and then record a new version of the project.</para>
679 <para>Alternatively, instead of running <literal>git add</literal>
680 beforehand, you can use</para>
681 <programlisting>
682 <literal>$ git commit -a
683 </literal>
684 </programlisting>
686 <para>which will automatically notice any modified (but not new)
687 files, add them to the index, and commit, all in one step.</para>
688 <para>A note on commit messages: Though not required, it's a good
689 idea to begin the commit message with a single short (less than 50
690 character) line summarizing the change, followed by a blank line
691 and then a more thorough description. Tools that turn commits into
692 email, for example, use the first line on the Subject: line and the
693 rest of the commit message in the body.</para>
694 <sect4 id="Git_tracks_content_not_file"><title>Git tracks content
695 not files</title>
696 <para>Many revision control systems provide an "add" command that
697 tells the system to start tracking changes to a new file. Git's
698 "add" command does something simpler and more powerful:
699 <literal>git add</literal> is used both for new and newly modified
700 files, and in both cases it takes a snapshot of the given files and
701 stages that content in the index, ready for inclusion in the next
702 commit.</para>
703 </sect4></sect2><sect2 id="Basic_Branching_and_Mergin"><title>Basic
704 Branching and Merging</title>
705 <para>A single git repository can maintain multiple branches of
706 development. To create a new branch named "experimental",
707 use</para>
708 <programlisting>
709 <literal>$ git branch experimental
710 </literal>
711 </programlisting>
713 <para>If you now run</para>
714 <programlisting>
715 <literal>$ git branch
716 </literal>
717 </programlisting>
719 <para>you'll get a list of all existing branches:</para>
720 <programlisting>
721 <literal> experimental
722 * master
723 </literal>
724 </programlisting>
726 <para>The "experimental" branch is the one you just created, and
727 the "master" branch is a default branch that was created for you
728 automatically. The asterisk marks the branch you are currently on;
729 type</para>
730 <programlisting>
731 <literal>$ git checkout experimental
732 </literal>
733 </programlisting>
735 <para>to switch to the experimental branch. Now edit a file, commit
736 the change, and switch back to the master branch:</para>
737 <programlisting>
738 <literal>(edit file)
739 $ git commit -a
740 $ git checkout master
741 </literal>
742 </programlisting>
744 <para>Check that the change you made is no longer visible, since it
745 was made on the experimental branch and you're back on the master
746 branch.</para>
747 <para>You can make a different change on the master branch:</para>
748 <programlisting>
749 <literal>(edit file)
750 $ git commit -a
751 </literal>
752 </programlisting>
754 <para>at this point the two branches have diverged, with different
755 changes made in each. To merge the changes made in experimental
756 into master, run</para>
757 <programlisting>
758 <literal>$ git merge experimental
759 </literal>
760 </programlisting>
762 <para>If the changes don't conflict, you're done. If there are
763 conflicts, markers will be left in the problematic files showing
764 the conflict;</para>
765 <programlisting>
766 <literal>$ git diff
767 </literal>
768 </programlisting>
770 <para>will show this. Once you've edited the files to resolve the
771 conflicts,</para>
772 <programlisting>
773 <literal>$ git commit -a
774 </literal>
775 </programlisting>
777 <para>will commit the result of the merge. Finally,</para>
778 <programlisting>
779 <literal>$ gitk
780 </literal>
781 </programlisting>
783 <para>will show a nice graphical representation of the resulting
784 history.</para>
785 <para>At this point you could delete the experimental branch
786 with</para>
787 <programlisting>
788 <literal>$ git branch -d experimental
789 </literal>
790 </programlisting>
792 <para>This command ensures that the changes in the experimental
793 branch are already in the current branch.</para>
794 <para>If you develop on a branch crazy-idea, then regret it, you
795 can always delete the branch with</para>
796 <programlisting>
797 <literal>$ git branch -D crazy-idea
798 </literal>
799 </programlisting>
801 <para>Branches are cheap and easy, so this is a good way to try
802 something out.</para>
803 <sect3 id="How_to_merg"><title>How to merge</title>
804 <para>You can rejoin two diverging branches of development using
805 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-merge.html">git
806 merge</ulink>:</para>
807 <programlisting>
808 <literal>$ git merge branchname
809 </literal>
810 </programlisting>
812 <para>merges the changes made in the branch "branchname" into the
813 current branch. If there are conflicts--for example, if the same
814 file is modified in two different ways in the remote branch and the
815 local branch--then you are warned; the output may look something
816 like this:</para>
817 <programlisting>
818 <literal>$ git merge next
819  100% (4/4) done
820 Auto-merged file.txt
821 CONFLICT (content): Merge conflict in file.txt
822 Automatic merge failed; fix conflicts and then commit the result.
823 </literal>
824 </programlisting>
826 <para>Conflict markers are left in the problematic files, and after
827 you resolve the conflicts manually, you can update the index with
828 the contents and run git commit, as you normally would when
829 modifying a file.</para>
830 <para>If you examine the resulting commit using gitk, you will see
831 that it has two parents: one pointing to the top of the current
832 branch, and one to the top of the other branch.</para>
833 </sect3><sect3 id="Resolving_a_merg"><title>Resolving a
834 merge</title>
835 <para>When a merge isn't resolved automatically, git leaves the
836 index and the working tree in a special state that gives you all
837 the information you need to help resolve the merge.</para>
838 <para>Files with conflicts are marked specially in the index, so
839 until you resolve the problem and update the index,
840 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-commit.html">git
841 commit</ulink> will fail:</para>
842 <programlisting>
843 <literal>$ git commit
844 file.txt: needs merge
845 </literal>
846 </programlisting>
848 <para>Also,
849 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-status.html">git
850 status</ulink> will list those files as "unmerged", and the files
851 with conflicts will have conflict markers added, like this:</para>
852 <programlisting>
853 <literal>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD:file.txt
854 Hello world
855 =======
856 Goodbye
857 &gt;&gt;&gt;&gt;&gt;&gt;&gt; 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
858 </literal>
859 </programlisting>
861 <para>All you need to do is edit the files to resolve the
862 conflicts, and then</para>
863 <programlisting>
864 <literal>$ git add file.txt
865 $ git commit
866 </literal>
867 </programlisting>
869 <para>Note that the commit message will already be filled in for
870 you with some information about the merge. Normally you can just
871 use this default message unchanged, but you may add additional
872 commentary of your own if desired.</para>
873 <para>The above is all you need to know to resolve a simple merge.
874 But git also provides more information to help resolve
875 conflicts:</para>
876 </sect3><sect3 id="Undoing_a_merg"><title>Undoing a merge</title>
877 <para>If you get stuck and decide to just give up and throw the
878 whole mess away, you can always return to the pre-merge state
879 with</para>
880 <programlisting>
881 <literal>$ git reset --hard HEAD
882 </literal>
883 </programlisting>
885 <para>Or, if you've already committed the merge that you want to
886 throw away,</para>
887 <programlisting>
888 <literal>$ git reset --hard ORIG_HEAD
889 </literal>
890 </programlisting>
892 <para>However, this last command can be dangerous in some
893 cases--never throw away a commit if that commit may itself have
894 been merged into another branch, as doing so may confuse further
895 merges.</para>
896 </sect3><sect3 id="Fast-forward_merge"><title>Fast-forward
897 merges</title>
898 <para>There is one special case not mentioned above, which is
899 treated differently. Normally, a merge results in a merge commit
900 with two parents, one for each of the two lines of development that
901 were merged.</para>
902 <para>However, if the current branch has not diverged from the
903 other--so every commit present in the current branch is already
904 contained in the other--then git just performs a "fast forward";
905 the head of the current branch is moved forward to point at the
906 head of the merged-in branch, without any new commits being
907 created.</para>
908 </sect3></sect2><sect2 id="Reviewing_History_-_Git_Lo"><title>Reviewing
909 History - Git Log</title>
910 <para>The
911 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-log.html">git
912 log</ulink> command can show lists of commits. On its own, it shows
913 all commits reachable from the parent commit; but you can also make
914 more specific requests:</para>
915 <programlisting>
916 <literal>$ git log v2.5..        # commits since (not reachable from) v2.5
917 $ git log test..master  # commits reachable from master but not test
918 $ git log master..test  # commits reachable from test but not master
919 $ git log master...test # commits reachable from either test or
920                         #    master, but not both
921 $ git log --since="2 weeks ago" # commits from the last 2 weeks
922 $ git log Makefile      # commits that modify Makefile
923 $ git log fs/           # commits that modify any file under fs/
924 $ git log -S'foo()'     # commits that add or remove any file data
925                         # matching the string 'foo()'
926 $ git log --no-merges   # dont show merge commits
927 </literal>
928 </programlisting>
930 <para>And of course you can combine all of these; the following
931 finds commits since v2.5 which touch the Makefile or any file under
932 fs:</para>
933 <programlisting>
934 <literal>$ git log v2.5.. Makefile fs/
935 </literal>
936 </programlisting>
938 <para>Git log will show a listing of each commit, with the most
939 recent commits first, that match the arguments given to the log
940 command.</para>
941 <programlisting>
942 <literal>commit f491239170cb1463c7c3cd970862d6de636ba787
943 Author: Matt McCutchen &lt;matt@mattmccutchen.net&gt;
944 Date:   Thu Aug 14 13:37:41 2008 -0400
946     git format-patch documentation: clarify what --cover-letter does
948 commit 7950659dc9ef7f2b50b18010622299c508bfdfc3
949 Author: Eric Raible &lt;raible@gmail.com&gt;
950 Date:   Thu Aug 14 10:12:54 2008 -0700
952     bash completion: 'git apply' should use 'fix' not 'strip'
953     Bring completion up to date with the man page.
954 </literal>
955 </programlisting>
957 <para>You can also ask git log to show patches:</para>
958 <programlisting>
959 <literal>$ git log -p
961 commit da9973c6f9600d90e64aac647f3ed22dfd692f70
962 Author: Robert Schiele &lt;rschiele@gmail.com&gt;
963 Date:   Mon Aug 18 16:17:04 2008 +0200
965     adapt git-cvsserver manpage to dash-free syntax
967 diff --git a/Documentation/git-cvsserver.txt b/Documentation/git-cvsserver.txt
968 index c2d3c90..785779e 100644
969 --- a/Documentation/git-cvsserver.txt
970 +++ b/Documentation/git-cvsserver.txt
971 @@ -11,7 +11,7 @@ SYNOPSIS
972  SSH:
974  [verse]
975 -export CVS_SERVER=git-cvsserver
976 +export CVS_SERVER="git cvsserver"
977  'cvs' -d :ext:user@server/path/repo.git co &lt;HEAD_name&gt;
979  pserver (/etc/inetd.conf):
980 </literal>
981 </programlisting>
983 <sect3 id="Log_Stat"><title>Log Stats</title>
984 <para>If you pass the <literal>--stat</literal> option to 'git
985 log', it will show you which files have changed in that commit and
986 how many lines were added and removed from each.</para>
987 <programlisting>
988 <literal>$ git log --stat
990 commit dba9194a49452b5f093b96872e19c91b50e526aa
991 Author: Junio C Hamano &lt;gitster@pobox.com&gt;
992 Date:   Sun Aug 17 15:44:11 2008 -0700
994     Start 1.6.0.X maintenance series
996  Documentation/RelNotes-1.6.0.1.txt |   15 +++++++++++++++
997  RelNotes                           |    2 +-
998  2 files changed, 16 insertions(+), 1 deletions(-)
999 </literal>
1000 </programlisting>
1002 </sect3><sect3 id="Formatting_the_Lo"><title>Formatting the
1003 Log</title>
1004 <para>You can also format the log output almost however you want.
1005 The '--pretty' option can take a number of preset formats, such as
1006 'oneline':</para>
1007 <programlisting>
1008 <literal>$ git log --pretty=oneline
1009 a6b444f570558a5f31ab508dc2a24dc34773825f dammit, this is the second time this has reverted
1010 49d77f72783e4e9f12d1bbcacc45e7a15c800240 modified index to create refs/heads if it is not 
1011 9764edd90cf9a423c9698a2f1e814f16f0111238 Add diff-lcs dependency
1012 e1ba1e3ca83d53a2f16b39c453fad33380f8d1cc Add dependency for Open4
1013 0f87b4d9020fff756c18323106b3fd4e2f422135 merged recent changes: * accepts relative alt pat
1014 f0ce7d5979dfb0f415799d086e14a8d2f9653300 updated the Manifest file
1015 </literal>
1016 </programlisting>
1018 <para>or you can do 'short' format:</para>
1019 <programlisting>
1020 <literal>$ git log --pretty=short
1021 commit a6b444f570558a5f31ab508dc2a24dc34773825f
1022 Author: Scott Chacon &lt;schacon@gmail.com&gt;
1024     dammit, this is the second time this has reverted
1026 commit 49d77f72783e4e9f12d1bbcacc45e7a15c800240
1027 Author: Scott Chacon &lt;schacon@gmail.com&gt;
1029     modified index to create refs/heads if it is not there
1031 commit 9764edd90cf9a423c9698a2f1e814f16f0111238
1032 Author: Hans Engel &lt;engel@engel.uk.to&gt;
1034     Add diff-lcs dependency
1035 </literal>
1036 </programlisting>
1038 <para>You can also use 'medium', 'full', 'fuller', 'email' or
1039 'raw'. If those formats aren't exactly what you need, you can also
1040 create your own format with the '--pretty=format' option (see the
1041 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-log.html">git
1042 log</ulink> docs for all the formatting options).</para>
1043 <programlisting>
1044 <literal>$ git log --pretty=format:'%h was %an, %ar, message: %s'
1045 a6b444f was Scott Chacon, 5 days ago, message: dammit, this is the second time this has re
1046 49d77f7 was Scott Chacon, 8 days ago, message: modified index to create refs/heads if it i
1047 9764edd was Hans Engel, 11 days ago, message: Add diff-lcs dependency
1048 e1ba1e3 was Hans Engel, 11 days ago, message: Add dependency for Open4
1049 0f87b4d was Scott Chacon, 12 days ago, message: merged recent changes:
1050 </literal>
1051 </programlisting>
1053 <para>Another interesting thing you can do is visualize the commit
1054 graph with the '--graph' option, like so:</para>
1055 <programlisting>
1056 <literal>$ git log --pretty=format:'%h : %s' --graph
1057 * 2d3acf9 : ignore errors from SIGCHLD on trap
1058 *   5e3ee11 : Merge branch 'master' of git://github.com/dustin/grit
1059 |\  
1060 | * 420eac9 : Added a method for getting the current branch.
1061 * | 30e367c : timeout code and tests
1062 * | 5a09431 : add timeout protection to grit
1063 * | e1193f8 : support for heads with slashes in them
1064 |/  
1065 * d6016bc : require time for xmlschema
1066 </literal>
1067 </programlisting>
1069 <para>It will give a pretty nice ASCII representation of the commit
1070 history lines.</para>
1071 </sect3><sect3 id="Ordering_the_Lo"><title>Ordering the Log</title>
1072 <para>You can also view the log entries in a few different orders.
1073 Note that git log starts with the most recent commit and works
1074 backwards through the parents; however, since git history can
1075 contain multiple independent lines of development, the particular
1076 order that commits are listed in may be somewhat arbitrary.</para>
1077 <para>If you want to specify a certain order, you can add an
1078 ordering option to the git log command.</para>
1079 <para>By default, the commits are shown in reverse chronological
1080 order.</para>
1081 <para>However, you can also specify '--topo-order', which makes the
1082 commits appear in topological order (i.e. descendant commits are
1083 shown before their parents). If we view the git log for the Grit
1084 repo in topo-order, you can see that the development lines are all
1085 grouped together.</para>
1086 <programlisting>
1087 <literal>$ git log --pretty=format:'%h : %s' --topo-order --graph
1088 *   4a904d7 : Merge branch 'idx2'
1089 |\  
1090 | *   dfeffce : merged in bryces changes and fixed some testing issues
1091 | |\  
1092 | | * 23f4ecf : Clarify how to get a full count out of Repo#commits
1093 | | *   9d6d250 : Appropriate time-zone test fix from halorgium
1094 | | |\  
1095 | | | * cec36f7 : Fix the to_hash test to run in US/Pacific time
1096 | | * | decfe7b : fixed manifest and grit.rb to make correct gemspec
1097 | | * | cd27d57 : added lib/grit/commit_stats.rb to the big list o' files
1098 | | * | 823a9d9 : cleared out errors by adding in Grit::Git#run method
1099 | | * |   4eb3bf0 : resolved merge conflicts, hopefully amicably
1100 | | |\ \  
1101 | | | * | d065e76 : empty commit to push project to runcoderun
1102 | | | * | 3fa3284 : whitespace
1103 | | | * | d01cffd : whitespace
1104 | | | * | 7c74272 : oops, update version here too
1105 | | | * | 13f8cc3 : push 0.8.3
1106 | | | * | 06bae5a : capture stderr and log it if debug is true when running commands
1107 | | | * | 0b5bedf : update history
1108 | | | * | d40e1f0 : some docs
1109 | | | * | ef8a23c : update gemspec to include the newly added files to manifest
1110 | | | * | 15dd347 : add missing files to manifest; add grit test
1111 | | | * | 3dabb6a : allow sending debug messages to a user defined logger if provided; tes
1112 | | | * | eac1c37 : pull out the date in this assertion and compare as xmlschemaw, to avoi
1113 | | | * | 0a7d387 : Removed debug print.
1114 | | | * | 4d6b69c : Fixed to close opened file description.
1115 </literal>
1116 </programlisting>
1118 <para>You can also use '--date-order', which orders the commits
1119 primarily by commit date. This option is similar to --topo-order in
1120 the sense that no parent comes before all of its children, but
1121 otherwise things are still ordered in the commit timestamp order.
1122 You can see that development lines are not grouped together here,
1123 that they jump around as parallel development occurred:</para>
1124 <programlisting>
1125 <literal>$ git log --pretty=format:'%h : %s' --date-order --graph
1126 *   4a904d7 : Merge branch 'idx2'
1127 |\  
1128 * | 81a3e0d : updated packfile code to recognize index v2
1129 | *   dfeffce : merged in bryces changes and fixed some testing issues
1130 | |\  
1131 | * | c615d80 : fixed a log issue
1132 |/ /  
1133 | * 23f4ecf : Clarify how to get a full count out of Repo#commits
1134 | *   9d6d250 : Appropriate time-zone test fix from halorgium
1135 | |\  
1136 | * | decfe7b : fixed manifest and grit.rb to make correct gemspec
1137 | * | cd27d57 : added lib/grit/commit_stats.rb to the big list o' file
1138 | * | 823a9d9 : cleared out errors by adding in Grit::Git#run method
1139 | * |   4eb3bf0 : resolved merge conflicts, hopefully amicably
1140 | |\ \  
1141 | * | | ba23640 : Fix CommitDb errors in test (was this the right fix?
1142 | * | | 4d8873e : test_commit no longer fails if you're not in PDT
1143 | * | | b3285ad : Use the appropriate method to find a first occurrenc
1144 | * | | 44dda6c : more cleanly accept separate options for initializin
1145 | * | | 839ba9f : needed to be able to ask Repo.new to work with a bar
1146 | | * | d065e76 : empty commit to push project to runcoderun
1147 * | | | 791ec6b : updated grit gemspec
1148 * | | | 756a947 : including code from github updates
1149 | | * | 3fa3284 : whitespace
1150 | | * | d01cffd : whitespace
1151 | * | | a0e4a3d : updated grit gemspec
1152 | * | | 7569d0d : including code from github updates
1153 </literal>
1154 </programlisting>
1156 <para>Lastly, you can reverse the order of the log with the
1157 '--reverse' option.</para>
1158 </sect3></sect2><sect2 id="Comparing_Commits_-_Git_Dif"><title>Comparing
1159 Commits - Git Diff</title>
1160 <para>You can generate diffs between any two versions of your
1161 project using
1162 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-diff.html">git
1163 diff</ulink>:</para>
1164 <programlisting>
1165 <literal>$ git diff master..test
1166 </literal>
1167 </programlisting>
1169 <para>That will produce the diff between the tips of the two
1170 branches. If you'd prefer to find the diff from their common
1171 ancestor to test, you can use three dots instead of two:</para>
1172 <programlisting>
1173 <literal>$ git diff master...test
1174 </literal>
1175 </programlisting>
1177 <para><ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-diff.html">git
1178 diff</ulink> is an incredibly useful tool for figuring out what has
1179 changed between any two points in your project's history, or to see
1180 what people are trying to introduce in new branches, etc.</para>
1181 <sect3 id="What_you_will_commi"><title>What you will commit</title>
1182 <para>You will commonly use
1183 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-diff.html">git
1184 diff</ulink> for figuring out differences between your last commit,
1185 your index, and your current working directory. A common use is to
1186 simply run</para>
1187 <programlisting>
1188 <literal>$ git diff
1189 </literal>
1190 </programlisting>
1192 <para>which will show you changes in the working directory that are
1193 not yet staged for the next commit. If you want to see what
1194 <emphasis>is</emphasis> staged for the next commit, you can
1195 run</para>
1196 <programlisting>
1197 <literal>$ git diff --cached
1198 </literal>
1199 </programlisting>
1201 <para>which will show you the difference between the index and your
1202 last commit; what you would be committing if you run "git commit"
1203 without the "-a" option. Lastly, you can run</para>
1204 <programlisting>
1205 <literal>$ git diff HEAD
1206 </literal>
1207 </programlisting>
1209 <para>which shows changes in the working directory since your last
1210 commit; what you would be committing if you run "git commit
1211 -a".</para>
1212 </sect3><sect3 id="More_Diff_Option"><title>More Diff
1213 Options</title>
1214 <para>If you want to see how your current working directory differs
1215 from the state of the project in another branch, you can run
1216 something like</para>
1217 <programlisting>
1218 <literal>$ git diff test
1219 </literal>
1220 </programlisting>
1222 <para>This will show you what is different between your current
1223 working directory and the snapshot on the 'test' branch. You can
1224 also limit the comparison to a specific file or subdirectory by
1225 adding a <emphasis>path limiter</emphasis>:</para>
1226 <programlisting>
1227 <literal>$ git diff HEAD -- ./lib 
1228 </literal>
1229 </programlisting>
1231 <para>That command will show the changes between your current
1232 working directory and the last commit (or, more accurately, the tip
1233 of the current branch), limiting the comparison to files in the
1234 'lib' subdirectory.</para>
1235 <para>If you don't want to see the whole patch, you can add the
1236 '--stat' option, which will limit the output to the files that have
1237 changed along with a little text graph depicting how many lines
1238 changed in each file.</para>
1239 <programlisting>
1240 <literal>$&gt;git diff --stat
1241  layout/book_index_template.html                    |    8 ++-
1242  text/05_Installing_Git/0_Source.markdown           |   14 ++++++
1243  text/05_Installing_Git/1_Linux.markdown            |   17 +++++++
1244  text/05_Installing_Git/2_Mac_104.markdown          |   11 +++++
1245  text/05_Installing_Git/3_Mac_105.markdown          |    8 ++++
1246  text/05_Installing_Git/4_Windows.markdown          |    7 +++
1247  .../1_Getting_a_Git_Repo.markdown                  |    7 +++-
1248  .../0_ Comparing_Commits_Git_Diff.markdown         |   45 +++++++++++++++++++-
1249  .../0_ Hosting_Git_gitweb_repoorcz_github.markdown |    4 +-
1250  9 files changed, 115 insertions(+), 6 deletions(-)
1251 </literal>
1252 </programlisting>
1254 <para>Sometimes that makes it easier to see overall what has
1255 changed, to jog your memory.</para>
1256 </sect3></sect2><sect2 id="Distributed_Workflow"><title>Distributed
1257 Workflows</title>
1258 <para>Suppose that Alice has started a new project with a git
1259 repository in /home/alice/project, and that Bob, who has a home
1260 directory on the same machine, wants to contribute.</para>
1261 <para>Bob begins with:</para>
1262 <programlisting>
1263 <literal>$ git clone /home/alice/project myrepo
1264 </literal>
1265 </programlisting>
1267 <para>This creates a new directory "myrepo" containing a clone of
1268 Alice's repository. The clone is on an equal footing with the
1269 original project, possessing its own copy of the original project's
1270 history.</para>
1271 <para>Bob then makes some changes and commits them:</para>
1272 <programlisting>
1273 <literal>(edit files)
1274 $ git commit -a
1275 (repeat as necessary)
1276 </literal>
1277 </programlisting>
1279 <para>When he's ready, he tells Alice to pull changes from the
1280 repository at /home/bob/myrepo. She does this with:</para>
1281 <programlisting>
1282 <literal>$ cd /home/alice/project
1283 $ git pull /home/bob/myrepo master
1284 </literal>
1285 </programlisting>
1287 <para>This merges the changes from Bob's "master" branch into
1288 Alice's current branch. If Alice has made her own changes in the
1289 meantime, then she may need to manually fix any conflicts. (Note
1290 that the "master" argument in the above command is actually
1291 unnecessary, as it is the default.)</para>
1292 <para>The "pull" command thus performs two operations: it fetches
1293 changes from a remote branch, then merges them into the current
1294 branch.</para>
1295 <para>When you are working in a small closely knit group, it is not
1296 unusual to interact with the same repository over and over again.
1297 By defining 'remote' repository shorthand, you can make it
1298 easier:</para>
1299 <programlisting>
1300 <literal>$ git remote add bob /home/bob/myrepo
1301 </literal>
1302 </programlisting>
1304 <para>With this, Alice can perform the first operation alone using
1305 the "git fetch" command without merging them with her own branch,
1306 using:</para>
1307 <programlisting>
1308 <literal>$ git fetch bob
1309 </literal>
1310 </programlisting>
1312 <para>Unlike the longhand form, when Alice fetches from Bob using a
1313 remote repository shorthand set up with <literal>git
1314 remote</literal>, what was fetched is stored in a remote tracking
1315 branch, in this case <literal>bob/master</literal>. So after
1316 this:</para>
1317 <programlisting>
1318 <literal>$ git log -p master..bob/master
1319 </literal>
1320 </programlisting>
1322 <para>shows a list of all the changes that Bob made since he
1323 branched from Alice's master branch.</para>
1324 <para>After examining those changes, Alice could merge the changes
1325 into her master branch:</para>
1326 <programlisting>
1327 <literal>$ git merge bob/master
1328 </literal>
1329 </programlisting>
1331 <para>This <literal>merge</literal> can also be done by 'pulling
1332 from her own remote tracking branch', like this:</para>
1333 <programlisting>
1334 <literal>$ git pull . remotes/bob/master
1335 </literal>
1336 </programlisting>
1338 <para>Note that git pull always merges into the current branch,
1339 regardless of what else is given on the command line.</para>
1340 <para>Later, Bob can update his repo with Alice's latest changes
1341 using</para>
1342 <programlisting>
1343 <literal>$ git pull
1344 </literal>
1345 </programlisting>
1347 <para>Note that he doesn't need to give the path to Alice's
1348 repository; when Bob cloned Alice's repository, git stored the
1349 location of her repository in the repository configuration, and
1350 that location is used for pulls:</para>
1351 <programlisting>
1352 <literal>$ git config --get remote.origin.url
1353 /home/alice/project
1354 </literal>
1355 </programlisting>
1357 <para>(The complete configuration created by git-clone is visible
1358 using "git config -l", and the
1359 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-config.html">git
1360 config</ulink> man page explains the meaning of each
1361 option.)</para>
1362 <para>Git also keeps a pristine copy of Alice's master branch under
1363 the name "origin/master":</para>
1364 <programlisting>
1365 <literal>$ git branch -r
1366   origin/master
1367 </literal>
1368 </programlisting>
1370 <para>If Bob later decides to work from a different host, he can
1371 still perform clones and pulls using the ssh protocol:</para>
1372 <programlisting>
1373 <literal>$ git clone alice.org:/home/alice/project myrepo
1374 </literal>
1375 </programlisting>
1377 <para>Alternatively, git has a native protocol, or can use rsync or
1378 http; see
1379 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-pull.html">git
1380 pull</ulink> for details.</para>
1381 <para>Git can also be used in a CVS-like mode, with a central
1382 repository that various users push changes to; see
1383 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-push.html">git
1384 push</ulink> and
1385 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/gitcvs-migration.html">gitcvs-migration</ulink>.</para>
1386 <sect3 id="Public_git_repositorie"><title>Public git
1387 repositories</title>
1388 <para>Another way to submit changes to a project is to tell the
1389 maintainer of that project to pull the changes from your repository
1390 using
1391 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-pull.html">git
1392 pull</ulink>. This is a way to get updates from the "main"
1393 repository, but it works just as well in the other
1394 direction.</para>
1395 <para>If you and the maintainer both have accounts on the same
1396 machine, then you can just pull changes from each other's
1397 repositories directly; commands that accept repository URLs as
1398 arguments will also accept a local directory name:</para>
1399 <programlisting>
1400 <literal>$ git clone /path/to/repository
1401 $ git pull /path/to/other/repository
1402 </literal>
1403 </programlisting>
1405 <para>or an ssh URL:</para>
1406 <programlisting>
1407 <literal>$ git clone ssh://yourhost/~you/repository
1408 </literal>
1409 </programlisting>
1411 <para>For projects with few developers, or for synchronizing a few
1412 private repositories, this may be all you need.</para>
1413 <para>However, the more common way to do this is to maintain a
1414 separate public repository (usually on a different host) for others
1415 to pull changes from. This is usually more convenient, and allows
1416 you to cleanly separate private work in progress from publicly
1417 visible work.</para>
1418 <para>You will continue to do your day-to-day work in your personal
1419 repository, but periodically "push" changes from your personal
1420 repository into your public repository, allowing other developers
1421 to pull from that repository. So the flow of changes, in a
1422 situation where there is one other developer with a public
1423 repository, looks like this:</para>
1424 <programlisting>
1425 <literal>                       you push
1426   your personal repo ------------------&gt; your public repo
1427     ^                                     |
1428     |                                     |
1429     | you pull                            | they pull
1430     |                                     |
1431     |                                     |
1432         |               they push             V
1433   their public repo &lt;------------------- their repo
1434 </literal>
1435 </programlisting>
1437 </sect3><sect3 id="Pushing_changes_to_a_public_repositor"><title>Pushing
1438 changes to a public repository</title>
1439 <para>Note that exporting via http or git allow other maintainers
1440 to fetch your latest changes, but they do not allow write access.
1441 For this, you will need to update the public repository with the
1442 latest changes created in your private repository.</para>
1443 <para>The simplest way to do this is using
1444 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-push.html">git
1445 push</ulink> and ssh; to update the remote branch named "master"
1446 with the latest state of your branch named "master", run</para>
1447 <programlisting>
1448 <literal>$ git push ssh://yourserver.com/~you/proj.git master:master
1449 </literal>
1450 </programlisting>
1452 <para>or just</para>
1453 <programlisting>
1454 <literal>$ git push ssh://yourserver.com/~you/proj.git master
1455 </literal>
1456 </programlisting>
1458 <para>As with git-fetch, git-push will complain if this does not
1459 result in a fast forward; see the following section for details on
1460 handling this case.</para>
1461 <para>Note that the target of a "push" is normally a bare
1462 repository. You can also push to a repository that has a
1463 checked-out working tree, but the working tree will not be updated
1464 by the push. This may lead to unexpected results if the branch you
1465 push to is the currently checked-out branch!</para>
1466 <para>As with git-fetch, you may also set up configuration options
1467 to save typing; so, for example, after</para>
1468 <programlisting>
1469 <literal>$ cat &gt;&gt;.git/config &lt;&lt;EOF
1470 [remote "public-repo"]
1471     url = ssh://yourserver.com/~you/proj.git
1473 </literal>
1474 </programlisting>
1476 <para>you should be able to perform the above push with just</para>
1477 <programlisting>
1478 <literal>$ git push public-repo master
1479 </literal>
1480 </programlisting>
1482 <para>See the explanations of the remote..url, branch..remote, and
1483 remote..push options in
1484 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-config.html">git
1485 config</ulink> for details.</para>
1486 </sect3><sect3 id="What_to_do_when_a_push_fail"><title>What to do
1487 when a push fails</title>
1488 <para>If a push would not result in a fast forward of the remote
1489 branch, then it will fail with an error like:</para>
1490 <programlisting>
1491 <literal>error: remote 'refs/heads/master' is not an ancestor of
1492 local  'refs/heads/master'.
1493 Maybe you are not up-to-date and need to pull first?
1494 error: failed to push to 'ssh://yourserver.com/~you/proj.git'
1495 </literal>
1496 </programlisting>
1498 <para>This can happen, for example, if you:</para>
1499 <programlisting>
1500 <literal>- use `git-reset --hard` to remove already-published commits, or
1501 - use `git-commit --amend` to replace already-published commits, or
1502 - use `git-rebase` to rebase any already-published commits.
1503 </literal>
1504 </programlisting>
1506 <para>You may force git-push to perform the update anyway by
1507 preceding the branch name with a plus sign:</para>
1508 <programlisting>
1509 <literal>$ git push ssh://yourserver.com/~you/proj.git +master
1510 </literal>
1511 </programlisting>
1513 <para>Normally whenever a branch head in a public repository is
1514 modified, it is modified to point to a descendant of the commit
1515 that it pointed to before. By forcing a push in this situation, you
1516 break that convention.</para>
1517 <para>Nevertheless, this is a common practice for people that need
1518 a simple way to publish a work-in-progress patch series, and it is
1519 an acceptable compromise as long as you warn other developers that
1520 this is how you intend to manage the branch.</para>
1521 <para>It's also possible for a push to fail in this way when other
1522 people have the right to push to the same repository. In that case,
1523 the correct solution is to retry the push after first updating your
1524 work: either by a pull, or by a fetch followed by a rebase; see the
1525 next section and
1526 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/gitcvs-migration.html">gitcvs-migration</ulink>
1527 for more.</para>
1528 </sect3></sect2><sect2 id="Git_Ta"><title>Git Tag</title>
1529 <sect3 id="Lightweight_Tag"><title>Lightweight Tags</title>
1530 <para>We can create a tag to refer to a particular commit by
1531 running
1532 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html">git
1533 tag</ulink> with no arguments.</para>
1534 <programlisting>
1535 <literal>$ git tag stable-1 1b2e1d63ff
1536 </literal>
1537 </programlisting>
1539 <para>After that, we can use stable-1 to refer to the commit
1540 1b2e1d63ff.</para>
1541 <para>This creates a "lightweight" tag, basically a branch that
1542 never moves. If you would also like to include a comment with the
1543 tag, and possibly sign it cryptographically, then we can create a
1544 <emphasis>tag object</emphasis> instead.</para>
1545 </sect3><sect3 id="Tag_Object"><title>Tag Objects</title>
1546 <para>If one of <emphasis>-a</emphasis>, <emphasis>-s</emphasis>,
1547 or <emphasis>-u &lt;key-id&gt;</emphasis> is passed, the command
1548 creates a tag object, and requires the tag message. Unless -m  or
1549 -F  is given, an editor is started for the user to type in the tag
1550 message.</para>
1551 <para>When this happens, a new object is added to the Git object
1552 database and the tag ref points to that <emphasis>tag
1553 object</emphasis>, rather than the commit itself. The strength of
1554 this is that you can sign the tag, so you can verify that it is the
1555 correct commit later. You can create a tag object like this:</para>
1556 <programlisting>
1557 <literal>$ git tag -a stable-1 1b2e1d63ff
1558 </literal>
1559 </programlisting>
1561 <para>It is actually possible to tag any object, but tagging commit
1562 objects is the most common. (In the Linux kernel source, the first
1563 tag object references a tree, rather than a commit)</para>
1564 </sect3><sect3 id="Signed_Tag"><title>Signed Tags</title>
1565 <para>If you have a GPG key setup, you can create signed tags
1566 fairly easily. First, you will probably want to setup your key id
1567 in your <emphasis>.git/config</emphasis> or
1568 <emphasis>~.gitconfig</emphasis> file.</para>
1569 <programlisting>
1570 <literal>[user]
1571     signingkey = &lt;gpg-key-id&gt;
1572 </literal>
1573 </programlisting>
1575 <para>You can also set that with</para>
1576 <programlisting>
1577 <literal>$ git config (--global) user.signingkey &lt;gpg-key-id&gt;
1578 </literal>
1579 </programlisting>
1581 <para>Now you can create a signed tag simply by replacing the
1582 <emphasis>-a</emphasis> with a <emphasis>-s</emphasis>.</para>
1583 <programlisting>
1584 <literal>$ git tag -s stable-1 1b2e1d63ff
1585 </literal>
1586 </programlisting>
1588 <para>If you don't have your GPG key in your config file, you can
1589 accomplish the same thing this way:</para>
1590 <programlisting>
1591 <literal>$ git tag -u &lt;gpg-key-id&gt; stable-1 1b2e1d63ff
1592 </literal>
1593 </programlisting>
1595 </sect3></sect2></sect1><sect1 id="Intermediate_Usag"><title>Intermediate
1596 Usage</title>
1597 <sect2 id="Ignoring_file"><title>Ignoring files</title>
1598 <para>A project will often generate files that you do 'not' want to
1599 track with git. This typically includes files generated by a build
1600 process or temporary backup files made by your editor. Of course,
1601 'not' tracking files with git is just a matter of 'not' calling
1602 "<literal>git-add</literal>" on them. But it quickly becomes
1603 annoying to have these untracked files lying around; e.g. they make
1604 "<literal>git add .</literal>" and "<literal>git commit
1605 -a</literal>" practically useless, and they keep showing up in the
1606 output of "<literal>git status</literal>".</para>
1607 <para>You can tell git to ignore certain files by creating a file
1608 called .gitignore in the top level of your working directory, with
1609 contents such as:</para>
1610 <programlisting>
1611 <literal># Lines starting with '#' are considered comments.
1612 # Ignore any file named foo.txt.
1613 foo.txt
1614 # Ignore (generated) html files,
1615 *.html
1616 # except foo.html which is maintained by hand.
1617 !foo.html
1618 # Ignore objects and archives.
1619 *.[oa]
1620 </literal>
1621 </programlisting>
1623 <para>See
1624 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/gitignore.html">gitignore</ulink>
1625 for a detailed explanation of the syntax. You can also place
1626 .gitignore files in other directories in your working tree, and
1627 they will apply to those directories and their subdirectories. The
1628 <literal>.gitignore</literal> files can be added to your repository
1629 like any other files (just run <literal>git add
1630 .gitignore</literal> and <literal>git commit</literal>, as usual),
1631 which is convenient when the exclude patterns (such as patterns
1632 matching build output files) would also make sense for other users
1633 who clone your repository.</para>
1634 <para>If you wish the exclude patterns to affect only certain
1635 repositories (instead of every repository for a given project), you
1636 may instead put them in a file in your repository named
1637 .git/info/exclude, or in any file specified by the
1638 <literal>core.excludesfile</literal> configuration variable. Some
1639 git commands can also take exclude patterns directly on the command
1640 line. See
1641 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/gitignore.html">gitignore</ulink>
1642 for the details.</para>
1643 </sect2><sect2 id="Rebasin"><title>Rebasing</title>
1644 <para>Suppose that you create a branch "mywork" on a
1645 remote-tracking branch "origin".</para>
1646 <programlisting>
1647 <literal>$ git checkout -b mywork origin
1648 </literal>
1649 </programlisting>
1651 <inlinemediaobject><imageobject>
1652 <imagedata fileref="../images/rebase0.png"/>
1653 </imageobject></inlinemediaobject>
1656 <para>Now you do some work, creating two new commits.</para>
1657 <programlisting>
1658 <literal>$ vi file.txt
1659 $ git commit
1660 $ vi otherfile.txt
1661 $ git commit
1663 </literal>
1664 </programlisting>
1666 <para>Meanwhile, someone else does some work creating two new
1667 commits on the origin branch too. This means both 'origin' and
1668 'mywork' has advanced, which means the work has diverged.</para>
1669 <inlinemediaobject><imageobject>
1670 <imagedata fileref="../images/rebase1.png"/>
1671 </imageobject></inlinemediaobject>
1674 <para>At this point, you could use "pull" to merge your changes
1675 back in; the result would create a new merge commit, like
1676 this:</para>
1677 <inlinemediaobject><imageobject>
1678 <imagedata fileref="../images/rebase2.png"/>
1679 </imageobject></inlinemediaobject>
1682 <para>However, if you prefer to keep the history in mywork a simple
1683 series of commits without any merges, you may instead choose to use
1684 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html">git
1685 rebase</ulink>:</para>
1686 <programlisting>
1687 <literal>$ git checkout mywork
1688 $ git rebase origin
1689 </literal>
1690 </programlisting>
1692 <para>This will remove each of your commits from mywork,
1693 temporarily saving them as patches (in a directory named
1694 ".git/rebase"), update mywork to point at the latest version of
1695 origin, then apply each of the saved patches to the new
1696 mywork.</para>
1697 <inlinemediaobject><imageobject>
1698 <imagedata fileref="../images/rebase3.png"/>
1699 </imageobject></inlinemediaobject>
1702 <para>Once the ref ('mywork') is updated to point to the newly
1703 created commit objects, your older commits will be abandoned. They
1704 will likely be removed if you run a pruning garbage collection.
1705 (see
1706 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-gc.html">git
1707 gc</ulink>)</para>
1708 <inlinemediaobject><imageobject>
1709 <imagedata fileref="../images/rebase4.png"/>
1710 </imageobject></inlinemediaobject>
1713 <para>So now we can look at the difference in our history between
1714 running a merge and running a rebase:</para>
1715 <inlinemediaobject><imageobject>
1716 <imagedata fileref="../images/rebase5.png"/>
1717 </imageobject></inlinemediaobject>
1720 <para>In the process of the rebase, it may discover conflicts. In
1721 that case it will stop and allow you to fix the conflicts; after
1722 fixing conflicts, use "git-add" to update the index with those
1723 contents, and then, instead of running git-commit, just run</para>
1724 <programlisting>
1725 <literal>$ git rebase --continue
1726 </literal>
1727 </programlisting>
1729 <para>and git will continue applying the rest of the
1730 patches.</para>
1731 <para>At any point you may use the <literal>--abort</literal>
1732 option to abort this process and return mywork to the state it had
1733 before you started the rebase:</para>
1734 <programlisting>
1735 <literal>$ git rebase --abort
1736 </literal>
1737 </programlisting>
1739 </sect2><sect2 id="Interactive_Rebasin"><title>Interactive
1740 Rebasing</title>
1741 <para>You can also rebase interactively. This is often used to
1742 re-write your own commit objects before pusing them somewhere. It
1743 is an easy way to split, merge or re-order commits before sharing
1744 them with others. You can also use it to clean up commits you've
1745 pulled from someone when applying them locally.</para>
1746 <para>If you have a number of commits that you would like to
1747 somehow modify during the rebase, you can invoke interactive mode
1748 by passing a '-i' or '--interactive' to the 'git rebase'
1749 command.</para>
1750 <programlisting>
1751 <literal>$ git rebase -i origin/master
1752 </literal>
1753 </programlisting>
1755 <para>This will invoke interactive rebase mode on all the commits
1756 you have made since the last time you have pushed (or merged from
1757 the origin repository).</para>
1758 <para>To see what commits those are beforehand, you can run log
1759 this way:</para>
1760 <programlisting>
1761 <literal>$ git log github/master..
1762 </literal>
1763 </programlisting>
1765 <para>Once you run the 'rebase -i' command, you will be thrown into
1766 your editor of choice with something that looks like this:</para>
1767 <programlisting>
1768 <literal>pick fc62e55 added file_size
1769 pick 9824bf4 fixed little thing
1770 pick 21d80a5 added number to log
1771 pick 76b9da6 added the apply command
1772 pick c264051 Revert "added file_size" - not implemented correctly
1774 # Rebase f408319..b04dc3d onto f408319
1776 # Commands:
1777 #  p, pick = use commit
1778 #  e, edit = use commit, but stop for amending
1779 #  s, squash = use commit, but meld into previous commit
1781 # If you remove a line here THAT COMMIT WILL BE LOST.
1782 # However, if you remove everything, the rebase will be aborted.
1784 </literal>
1785 </programlisting>
1787 <para>This means that there are 5 commits since you last pushed and
1788 it gives you one line per commit with the following format:</para>
1789 <programlisting>
1790 <literal>(action) (partial-sha) (short commit message)
1791 </literal>
1792 </programlisting>
1794 <para>Now, you can change the action (which is by default 'pick')
1795 to either 'edit' or 'squash', or just leave it as 'pick'. You can
1796 also reorder the commits just by moving the lines around however
1797 you want. Then, when you exit the editor, git will try to apply the
1798 commits however they are now arranged and do the action
1799 specified.</para>
1800 <para>If 'pick' is specified, it will simply try to apply the patch
1801 and save the commit with the same message as before.</para>
1802 <para>If 'squash' is specified, it will combine that commit with
1803 the previous one to create a new commit. This will drop you into
1804 your editor again to merge the commit messages of the two commits
1805 it is now squashing together. So, if you exit the editor with
1806 this:</para>
1807 <programlisting>
1808 <literal>pick   fc62e55 added file_size
1809 squash 9824bf4 fixed little thing
1810 squash 21d80a5 added number to log
1811 squash 76b9da6 added the apply command
1812 squash c264051 Revert "added file_size" - not implemented correctly
1813 </literal>
1814 </programlisting>
1816 <para>Then you will have to create a single commit message from
1817 this:</para>
1818 <programlisting>
1819 <literal># This is a combination of 5 commits.
1820 # The first commit's message is:
1821 added file_size
1823 # This is the 2nd commit message:
1825 fixed little thing
1827 # This is the 3rd commit message:
1829 added number to log
1831 # This is the 4th commit message:
1833 added the apply command
1835 # This is the 5th commit message:
1837 Revert "added file_size" - not implemented correctly
1839 This reverts commit fc62e5543b195f18391886b9f663d5a7eca38e84.
1840 </literal>
1841 </programlisting>
1843 <para>Once you have edited that down into once commit message and
1844 exit the editor, the commit will be saved with your new
1845 message.</para>
1846 <para>If 'edit' is specified, it will do the same thing, but then
1847 pause before moving on to the next one and drop you into the
1848 command line so you can amend the commit, or change the commit
1849 contents somehow.</para>
1850 <para>If you wanted to split a commit, for instance, you would
1851 specify 'edit' for that commit:</para>
1852 <programlisting>
1853 <literal>pick   fc62e55 added file_size
1854 pick   9824bf4 fixed little thing
1855 edit   21d80a5 added number to log
1856 pick   76b9da6 added the apply command
1857 pick   c264051 Revert "added file_size" - not implemented correctly
1858 </literal>
1859 </programlisting>
1861 <para>And then when you get to the command line, you revert that
1862 commit and create two (or more) new ones. Lets say 21d80a5 modified
1863 two files, file1 and file2, and you wanted to split them into
1864 seperate commits. You could do this after the rebase dropped you to
1865 the command line :</para>
1866 <programlisting>
1867 <literal>$ git reset HEAD^
1868 $ git add file1
1869 $ git commit 'first part of split commit'
1870 $ git add file2
1871 $ git commit 'second part of split commit'
1872 $ git rebase --continue
1873 </literal>
1874 </programlisting>
1876 <para>And now instead of 5 commits, you would have 6.</para>
1877 <para>The last useful thing that interactive rebase can do is drop
1878 commits for you. If instead of choosing 'pick', 'squash' or 'edit'
1879 for the commit line, you simply remove the line, it will remove the
1880 commit from the history.</para>
1881 </sect2><sect2 id="Interactive_Addin"><title>Interactive
1882 Adding</title>
1883 <para>Interactive Adding is a really nice way of working with and
1884 visualizing the Git index. To start it up, simply type 'git add
1885 -i'. Git will show you all the modified files you have and their
1886 status.</para>
1887 <programlisting>
1888 <literal>$&gt;git add -i
1889            staged     unstaged path
1890   1:    unchanged        +4/-0 assets/stylesheets/style.css
1891   2:    unchanged      +23/-11 layout/book_index_template.html
1892   3:    unchanged        +7/-7 layout/chapter_template.html
1893   4:    unchanged        +3/-3 script/pdf.rb
1894   5:    unchanged      +121/-0 text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
1896 *** Commands ***
1897   1: status   2: update   3: revert   4: add untracked
1898   5: patch    6: diff     7: quit     8: help
1899 What now&gt; 
1900 </literal>
1901 </programlisting>
1903 <para>In this case, we can see that there are 5 modified files that
1904 have not been added to our index yet (unstaged), and even how many
1905 lines have been added to or removed from each. It then shows us an
1906 interactive menu of what we can do in this mode.</para>
1907 <para>If we want to stage the files, we can type '2' or 'u' for the
1908 update mode. Then I can specify which files I want to stage (add to
1909 the index) by typing in the numbers of the files (in this case,
1910 1-4)</para>
1911 <programlisting>
1912 <literal>What now&gt; 2
1913            staged     unstaged path
1914   1:    unchanged        +4/-0 assets/stylesheets/style.css
1915   2:    unchanged      +23/-11 layout/book_index_template.html
1916   3:    unchanged        +7/-7 layout/chapter_template.html
1917   4:    unchanged        +3/-3 script/pdf.rb
1918   5:    unchanged      +121/-0 text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
1919 Update&gt;&gt; 1-4
1920            staged     unstaged path
1921 * 1:    unchanged        +4/-0 assets/stylesheets/style.css
1922 * 2:    unchanged      +23/-11 layout/book_index_template.html
1923 * 3:    unchanged        +7/-7 layout/chapter_template.html
1924 * 4:    unchanged        +3/-3 script/pdf.rb
1925   5:    unchanged      +121/-0 text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
1926 Update&gt;&gt; 
1927 </literal>
1928 </programlisting>
1930 <para>If I hit enter, I will be taken back to the main menu where I
1931 can see that the file status has changed:</para>
1932 <programlisting>
1933 <literal>What now&gt; status
1934            staged     unstaged path
1935   1:        +4/-0      nothing assets/stylesheets/style.css
1936   2:      +23/-11      nothing layout/book_index_template.html
1937   3:        +7/-7      nothing layout/chapter_template.html
1938   4:        +3/-3      nothing script/pdf.rb
1939   5:    unchanged      +121/-0 text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
1940 </literal>
1941 </programlisting>
1943 <para>Now we can see the first four files are staged and the last
1944 one is still not. This is basically a compressed way to see the
1945 same information we see when we run 'git status' from the command
1946 line:</para>
1947 <programlisting>
1948 <literal>$ git status
1949 # On branch master
1950 # Changes to be committed:
1951 #   (use "git reset HEAD &lt;file&gt;..." to unstage)
1953 #   modified:   assets/stylesheets/style.css
1954 #   modified:   layout/book_index_template.html
1955 #   modified:   layout/chapter_template.html
1956 #   modified:   script/pdf.rb
1958 # Changed but not updated:
1959 #   (use "git add &lt;file&gt;..." to update what will be committed)
1961 #   modified:   text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
1963 </literal>
1964 </programlisting>
1966 <para>There are a number of useful things we can do, including
1967 unstaging files (3: revert), adding untracked files (4: add
1968 untracked), and viewing diffs (6: diff). Those are all pretty
1969 straightforward. However, there is one command that is pretty cool
1970 here, which is staging patches (5: patch).</para>
1971 <para>If you type '5' or 'p' in the menu, git will show you your
1972 diff patch by patch (or hunk by hunk) and ask if you want to stage
1973 each one. That way you can actually stage for a commit a part of a
1974 file edit. If you've edited a file and want to only commit part of
1975 it and not an unfinished part, or commit documentation or
1976 whitespace changes seperate from substantive changes, you can use
1977 'git add -i' to do so relatively easily.</para>
1978 <para>Here I've staged some changes to the book_index_template.html
1979 file, but not all of them:</para>
1980 <programlisting>
1981 <literal>        staged     unstaged path
1982 1:        +4/-0      nothing assets/stylesheets/style.css
1983 2:       +20/-7        +3/-4 layout/book_index_template.html
1984 3:        +7/-7      nothing layout/chapter_template.html
1985 4:        +3/-3      nothing script/pdf.rb
1986 5:    unchanged      +121/-0 text/14_Interactive_Rebasing/0_ Interactive_Rebasing.markdown
1987 6:    unchanged       +85/-0 text/15_Interactive_Adding/0_ Interactive_Adding.markdown
1988 </literal>
1989 </programlisting>
1991 <para>When you are done making changes to your index through 'git
1992 add -i', you simply quit (7: quit) and then run 'git commit' to
1993 commit the staged changes. Remember <emphasis>not</emphasis> to run
1994 'git commit -a', which will blow away all the careful changes
1995 you've just made and simply commit everything.</para>
1996 </sect2><sect2 id="Stashin"><title>Stashing</title>
1997 <para>While you are in the middle of working on something
1998 complicated, you find an unrelated but obvious and trivial bug. You
1999 would like to fix it before continuing. You can use
2000 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-stash.html">git
2001 stash</ulink> to save the current state of your work, and after
2002 fixing the bug (or, optionally after doing so on a different branch
2003 and then coming back), unstash the work-in-progress changes.</para>
2004 <programlisting>
2005 <literal>$ git stash "work in progress for foo feature"
2006 </literal>
2007 </programlisting>
2009 <para>This command will save your changes away to the
2010 <literal>stash</literal>, and reset your working tree and the index
2011 to match the tip of your current branch. Then you can make your fix
2012 as usual.</para>
2013 <programlisting>
2014 <literal>... edit and test ...
2015 $ git commit -a -m "blorpl: typofix"
2016 </literal>
2017 </programlisting>
2019 <para>After that, you can go back to what you were working on with
2020 <literal>git stash apply</literal>:</para>
2021 <programlisting>
2022 <literal>$ git stash apply
2023 </literal>
2024 </programlisting>
2026 <sect3 id="Stash_Queu"><title>Stash Queue</title>
2027 <para>You can also use stashing to queue up stashed changes.If you
2028 run 'git stash list' you can see which stashes you have
2029 saved:</para>
2030 <programlisting>
2031 <literal>$&gt;git stash list
2032 stash@{0}: WIP on book: 51bea1d... fixed images
2033 stash@{1}: WIP on master: 9705ae6... changed the browse code to the official repo
2034 </literal>
2035 </programlisting>
2037 <para>Then you can apply them individually with 'git stash apply
2038 stash@{1}'. You can clear out the list with 'git stash
2039 clear'.</para>
2040 </sect3></sect2><sect2 id="Git_Treeishe"><title>Git
2041 Treeishes</title>
2042 <para>There are a number of ways to refer to a particular commit or
2043 tree other than spelling out the entire 40-character sha. In Git,
2044 these are referred to as a 'treeish'.</para>
2045 <sect3 id="Partial_Sh"><title>Partial Sha</title>
2046 <para>If your commit sha is
2047 '<literal>980e3ccdaac54a0d4de358f3fe5d718027d96aae</literal>', git
2048 will recognize any of the following identically:</para>
2049 <programlisting>
2050 <literal>980e3ccdaac54a0d4de358f3fe5d718027d96aae
2051 980e3ccdaac54a0d4
2052 980e3cc
2053 </literal>
2054 </programlisting>
2056 <para>As long as the partial sha is unique - it can't be confused
2057 with another (which is incredibly unlikely if you use at least 5
2058 characters), git will expand a partial sha for you.</para>
2059 </sect3><sect3 id="Branch,_Remote_or_Tag_Nam"><title>Branch, Remote
2060 or Tag Name</title>
2061 <para>You can always use a branch, remote or tag name instead of a
2062 sha, since they are simply pointers anyhow. If your master branch
2063 is on the 980e3 commit and you've pushed it to origin and have
2064 tagged it 'v1.0', then all of the following are equivalent:</para>
2065 <programlisting>
2066 <literal>980e3ccdaac54a0d4de358f3fe5d718027d96aae
2067 origin/master
2068 refs/remotes/origin/master
2069 master
2070 refs/heads/master
2071 v1.0
2072 refs/tags/v1.0
2073 </literal>
2074 </programlisting>
2076 <para>Which means the following will give you identical
2077 output:</para>
2078 <programlisting>
2079 <literal>$ git log master
2081 $ git log refs/tags/v1.0
2082 </literal>
2083 </programlisting>
2085 </sect3><sect3 id="Date_Spe"><title>Date Spec</title>
2086 <para>The Ref Log that git keeps will allow you to do some relative
2087 stuff locally, such as:</para>
2088 <programlisting>
2089 <literal>master@{yesterday}
2091 master@{1 month ago}
2092 </literal>
2093 </programlisting>
2095 <para>Which is shorthand for 'where the master branch head was
2096 yesterday', etc. Note that this format can result in different shas
2097 on different computers, even if the master branch is currently
2098 pointing to the same place.</para>
2099 </sect3><sect3 id="Ordinal_Spe"><title>Ordinal Spec</title>
2100 <para>This format will give you the Nth previous value of a
2101 particular reference. For example:</para>
2102 <programlisting>
2103 <literal>master@{5}
2104 </literal>
2105 </programlisting>
2107 <para>will give you the 5th prior value of the master head
2108 ref.</para>
2109 </sect3><sect3 id="Carrot_Paren"><title>Carrot Parent</title>
2110 <para>This will give you the Nth parent of a particular commit.
2111 This format is only useful on merge commits - commit objects that
2112 have more than one direct parent.</para>
2113 <programlisting>
2114 <literal>master^2
2115 </literal>
2116 </programlisting>
2118 </sect3><sect3 id="Tilde_Spe"><title>Tilde Spec</title>
2119 <para>The tilde spec will give you the Nth grandparent of a commit
2120 object. For example,</para>
2121 <programlisting>
2122 <literal>master~2
2123 </literal>
2124 </programlisting>
2126 <para>will give us the first parent of the first parent of the
2127 commit that master points to. It is equivalent to:</para>
2128 <programlisting>
2129 <literal>master^^
2130 </literal>
2131 </programlisting>
2133 <para>You can keep doing this, too. The following specs will point
2134 to the same commit:</para>
2135 <programlisting>
2136 <literal>master^^^^^^
2137 master~3^~2
2138 master~6
2139 </literal>
2140 </programlisting>
2142 </sect3><sect3 id="Tree_Pointe"><title>Tree Pointer</title>
2143 <para>This disambiguates a commit from the tree that it points to.
2144 If you want the sha that a commit points to, you can add the
2145 '^{tree}' spec to the end of it.</para>
2146 <programlisting>
2147 <literal>master^{tree}
2148 </literal>
2149 </programlisting>
2151 </sect3><sect3 id="Blob_Spe"><title>Blob Spec</title>
2152 <para>If you want the sha of a particular blob, you can add the
2153 blob path at the end of the treeish, like so:</para>
2154 <programlisting>
2155 <literal>master:/path/to/file
2156 </literal>
2157 </programlisting>
2159 </sect3><sect3 id="Rang"><title>Range</title>
2160 <para>Finally, you can specify a range of commits with the range
2161 spec. This will give you all the commits between 7b593b5 and 51bea1
2162 (where 51bea1 is most recent), excluding 7b593b5 but including
2163 51bea1:</para>
2164 <programlisting>
2165 <literal>7b593b5..51bea1
2166 </literal>
2167 </programlisting>
2169 <para>This will include every commit <emphasis>since</emphasis>
2170 7b593b:</para>
2171 <programlisting>
2172 <literal>7b593b.. 
2173 </literal>
2174 </programlisting>
2176 </sect3></sect2><sect2 id="Tracking_Branche"><title>Tracking
2177 Branches</title>
2178 <para>A 'tracking branch' in Git is a local branch that is
2179 connected to a remote branch. When you push and pull on that
2180 branch, it automatically pushes and pulls to the remote branch that
2181 it is connected with.</para>
2182 <para>Use this if you always pull from the same upstream branch
2183 into the new branch, and if you don't want to use "git pull  "
2184 explicitly.</para>
2185 <para>The 'git clone' command automatically sets up a 'master'
2186 branch that is a tracking branch for 'origin/master' - the master
2187 branch on the cloned repository.</para>
2188 <para>You can create a tracking branch manually by adding the
2189 '--track' option to the 'branch' command in Git.</para>
2190 <programlisting>
2191 <literal>git branch --track experimental origin/experimental
2192 </literal>
2193 </programlisting>
2195 <para>Then when you run:</para>
2196 <programlisting>
2197 <literal>$ git pull experimental
2198 </literal>
2199 </programlisting>
2201 <para>It will automatically fetch from 'origin' and merge
2202 'origin/experimental' into your local 'experimental' branch.</para>
2203 <para>Likewise, when you push to origin, it will push what your
2204 'experimental' points to to origins 'experimental', without having
2205 to specify it.</para>
2206 </sect2><sect2 id="Finding_with_Git_Gre"><title>Finding with Git
2207 Grep</title>
2208 <para>Finding files with words or phrases in Git is really easy
2209 with the
2210 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-grep.html">git
2211 grep</ulink> command. It is possible to do this with the normal
2212 unix 'grep' command, but with 'git grep' you can also search
2213 through previous versions of the project without having to check
2214 them out.</para>
2215 <para>For example, if I wanted to see every place that used the
2216 'xmmap' call in my git.git repository, I could run this:</para>
2217 <programlisting>
2218 <literal>$ git grep xmmap
2219 config.c:               contents = xmmap(NULL, contents_sz, PROT_READ,
2220 diff.c:         s-&gt;data = xmmap(NULL, s-&gt;size, PROT_READ, MAP_PRIVATE, fd, 0);
2221 git-compat-util.h:extern void *xmmap(void *start, size_t length, int prot, int fla
2222 read-cache.c:   mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
2223 refs.c: log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0);
2224 sha1_file.c:    map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0);
2225 sha1_file.c:    idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);
2226 sha1_file.c:                    win-&gt;base = xmmap(NULL, win-&gt;len,
2227 sha1_file.c:                    map = xmmap(NULL, *size, PROT_READ, MAP_PRIVATE, f
2228 sha1_file.c:            buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2229 wrapper.c:void *xmmap(void *start, size_t length,
2230 </literal>
2231 </programlisting>
2233 <para>If I wanted to see the line number of each match as well, I
2234 can add the '-n' option:</para>
2235 <programlisting>
2236 <literal>$&gt;git grep -n xmmap
2237 config.c:1016:          contents = xmmap(NULL, contents_sz, PROT_READ,
2238 diff.c:1833:            s-&gt;data = xmmap(NULL, s-&gt;size, PROT_READ, MAP_PRIVATE, fd,
2239 git-compat-util.h:291:extern void *xmmap(void *start, size_t length, int prot, int
2240 read-cache.c:1178:      mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_
2241 refs.c:1345:    log_mapped = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, logfd, 0);
2242 sha1_file.c:377:        map = xmmap(NULL, mapsz, PROT_READ, MAP_PRIVATE, fd, 0);
2243 sha1_file.c:479:        idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd
2244 sha1_file.c:780:                        win-&gt;base = xmmap(NULL, win-&gt;len,
2245 sha1_file.c:1076:                       map = xmmap(NULL, *size, PROT_READ, MAP_PR
2246 sha1_file.c:2393:               buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd
2247 wrapper.c:89:void *xmmap(void *start, size_t length,
2248 </literal>
2249 </programlisting>
2251 <para>If we're only interested in the filename, we can pass the
2252 '--name-only' option:</para>
2253 <programlisting>
2254 <literal>$&gt;git grep --name-only xmmap
2255 config.c
2256 diff.c
2257 git-compat-util.h
2258 read-cache.c
2259 refs.c
2260 sha1_file.c
2261 wrapper.c
2262 </literal>
2263 </programlisting>
2265 <para>We could also see how many line matches we have in each file
2266 with the '-c' option:</para>
2267 <programlisting>
2268 <literal>$&gt;git grep -c xmmap
2269 config.c:1
2270 diff.c:1
2271 git-compat-util.h:1
2272 read-cache.c:1
2273 refs.c:1
2274 sha1_file.c:5
2275 wrapper.c:1
2276 </literal>
2277 </programlisting>
2279 <para>Now, if I wanted to see where that was used in a specific
2280 version of git, I could add the tag reference to the end, like
2281 this:</para>
2282 <programlisting>
2283 <literal>$ git grep xmmap v1.5.0
2284 v1.5.0:config.c:                contents = xmmap(NULL, st.st_size, PROT_READ,
2285 v1.5.0:diff.c:          s-&gt;data = xmmap(NULL, s-&gt;size, PROT_READ, MAP_PRIVATE, fd,
2286 v1.5.0:git-compat-util.h:static inline void *xmmap(void *start, size_t length,
2287 v1.5.0:read-cache.c:                    cache_mmap = xmmap(NULL, cache_mmap_size, 
2288 v1.5.0:refs.c:  log_mapped = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, logfd
2289 v1.5.0:sha1_file.c:     map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 
2290 v1.5.0:sha1_file.c:     idx_map = xmmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd
2291 v1.5.0:sha1_file.c:                     win-&gt;base = xmmap(NULL, win-&gt;len,
2292 v1.5.0:sha1_file.c:     map = xmmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 
2293 v1.5.0:sha1_file.c:             buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd
2294 </literal>
2295 </programlisting>
2297 <para>We can see that there are some differences between the
2298 current lines and these lines in version 1.5.0, one of which is
2299 that xmmap is now used in wrapper.c where it was not back in
2300 v1.5.0.</para>
2301 <para>We can also combine search terms in grep. Say we wanted to
2302 search for where SORT_DIRENT is defined in our repository:</para>
2303 <programlisting>
2304 <literal>$ git grep -e '#define' --and -e SORT_DIRENT
2305 builtin-fsck.c:#define SORT_DIRENT 0
2306 builtin-fsck.c:#define SORT_DIRENT 1
2307 </literal>
2308 </programlisting>
2310 <para>We can also search for every file that has
2311 <emphasis>both</emphasis> search terms, but display each line that
2312 has <emphasis>either</emphasis> of the terms in those files:</para>
2313 <programlisting>
2314 <literal>$ git grep --all-match -e '#define' -e SORT_DIRENT
2315 builtin-fsck.c:#define REACHABLE 0x0001
2316 builtin-fsck.c:#define SEEN      0x0002
2317 builtin-fsck.c:#define ERROR_OBJECT 01
2318 builtin-fsck.c:#define ERROR_REACHABLE 02
2319 builtin-fsck.c:#define SORT_DIRENT 0
2320 builtin-fsck.c:#define DIRENT_SORT_HINT(de) 0
2321 builtin-fsck.c:#define SORT_DIRENT 1
2322 builtin-fsck.c:#define DIRENT_SORT_HINT(de) ((de)-&gt;d_ino)
2323 builtin-fsck.c:#define MAX_SHA1_ENTRIES (1024)
2324 builtin-fsck.c: if (SORT_DIRENT)
2325 </literal>
2326 </programlisting>
2328 <para>We can also search for lines that have one term and either of
2329 two other terms, for example, if we wanted to see where we defined
2330 constants that had either PATH or MAX in the name:</para>
2331 <programlisting>
2332 <literal>$ git grep -e '#define' --and \( -e PATH -e MAX \) 
2333 abspath.c:#define MAXDEPTH 5
2334 builtin-blame.c:#define MORE_THAN_ONE_PATH      (1u&lt;&lt;13)
2335 builtin-blame.c:#define MAXSG 16
2336 builtin-describe.c:#define MAX_TAGS     (FLAG_BITS - 1)
2337 builtin-fetch-pack.c:#define MAX_IN_VAIN 256
2338 builtin-fsck.c:#define MAX_SHA1_ENTRIES (1024)
2340 </literal>
2341 </programlisting>
2343 </sect2><sect2 id="Undoing_in_Git_-_Reset,_Checkout_and_Rever"><title>Undoing
2344 in Git - Reset, Checkout and Revert</title>
2345 <para>Git provides multiple methods for fixing up mistakes as you
2346 are developing. Selecting an appropriate method depends on whether
2347 or not you have committed the mistake, and if you have committed
2348 the mistake, whether you have shared the erroneous commit with
2349 anyone else.</para>
2350 <sect3 id="Fixing_un-committed_mistake"><title>Fixing un-committed
2351 mistakes</title>
2352 <para>If you've messed up the working tree, but haven't yet
2353 committed your mistake, you can return the entire working tree to
2354 the last committed state with</para>
2355 <programlisting>
2356 <literal>$ git reset --hard HEAD
2357 </literal>
2358 </programlisting>
2360 <para>This will throw away any changes you may have added to the
2361 git index and as well as any outstanding changes you have in your
2362 working tree. In other words, it causes the results of "git diff"
2363 and "git diff --cached" to both be empty.</para>
2364 <para>If you just want to restore just one file, say your hello.rb,
2366 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html">git
2367 checkout</ulink> instead</para>
2368 <programlisting>
2369 <literal>$ git checkout -- hello.rb
2370 $ git checkout HEAD hello.rb
2371 </literal>
2372 </programlisting>
2374 <para>The first command restores hello.rb to the version in the
2375 index, so that "git diff hello.rb" returns no differences. The
2376 second command will restore hello.rb to the version in the HEAD
2377 revision, so that both "git diff hello.rb" and "git diff --cached
2378 hello.rb" return no differences.</para>
2379 </sect3><sect3 id="Fixing_committed_mistake"><title>Fixing
2380 committed mistakes</title>
2381 <para>If you make a commit that you later wish you hadn't, there
2382 are two fundamentally different ways to fix the problem:</para>
2383 <orderedlist>
2384 <listitem>
2385 <para>You can create a new commit that undoes whatever was done by
2386 the old commit. This is the correct thing if your mistake has
2387 already been made public.</para>
2388 </listitem>
2389 <listitem>
2390 <para>You can go back and modify the old commit. You should never
2391 do this if you have already made the history public; git does not
2392 normally expect the "history" of a project to change, and cannot
2393 correctly perform repeated merges from a branch that has had its
2394 history changed.</para>
2395 </listitem>
2396 </orderedlist>
2398 <sect4 id="Fixing_a_mistake_with_a_new_commi"><title>Fixing a
2399 mistake with a new commit</title>
2400 <para>Creating a new commit that reverts an earlier change is very
2401 easy; just pass the
2402 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-revert.html">git
2403 revert</ulink> command a reference to the bad commit; for example,
2404 to revert the most recent commit:</para>
2405 <programlisting>
2406 <literal>$ git revert HEAD
2407 </literal>
2408 </programlisting>
2410 <para>This will create a new commit which undoes the change in
2411 HEAD. You will be given a chance to edit the commit message for the
2412 new commit.</para>
2413 <para>You can also revert an earlier change, for example, the
2414 next-to-last:</para>
2415 <programlisting>
2416 <literal>$ git revert HEAD^
2417 </literal>
2418 </programlisting>
2420 <para>In this case git will attempt to undo the old change while
2421 leaving intact any changes made since then. If more recent changes
2422 overlap with the changes to be reverted, then you will be asked to
2423 fix conflicts manually, just as in the case of resolving a
2424 merge.</para>
2425 </sect4><sect4 id="Fixing_a_mistake_by_modifying_a_commi"><title>Fixing
2426 a mistake by modifying a commit</title>
2427 <para>If you have just committed something but realize you need to
2428 fix up that commit, recent versions of
2429 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-commit.html">git
2430 commit</ulink> support an <emphasis>--amend</emphasis> flag which
2431 instructs git to replace the HEAD commit with a new one, based on
2432 the current contents of the index. This gives you an opportunity to
2433 add files that you forgot to add or correct typos in a commit
2434 message, prior to pushing the change out for the world to
2435 see.</para>
2436 <para>If you find a mistake in an older commit, but still one that
2437 you have not yet published to the world, you use
2438 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html">git
2439 rebase</ulink> in interactive mode, with "git rebase -i" marking
2440 the change that requires correction with <emphasis>edit</emphasis>.
2441 This will allow you to amend the commit during the rebasing
2442 process.</para>
2443 </sect4></sect3></sect2><sect2 id="Maintaining_Gi"><title>Maintaining
2444 Git</title>
2445 <sect3 id="Ensuring_good_performanc"><title>Ensuring good
2446 performance</title>
2447 <para>On large repositories, git depends on compression to keep the
2448 history information from taking up too much space on disk or in
2449 memory.</para>
2450 <para>This compression is not performed automatically. Therefore
2451 you should occasionally run
2452 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-gc.html">git
2453 gc</ulink>:</para>
2454 <programlisting>
2455 <literal>$ git gc
2456 </literal>
2457 </programlisting>
2459 <para>to recompress the archive. This can be very time-consuming,
2460 so you may prefer to run git-gc when you are not doing other
2461 work.</para>
2462 </sect3><sect3 id="Ensuring_reliabilit"><title>Ensuring
2463 reliability</title>
2464 <para>The
2465 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-fsck.html">git
2466 fsck</ulink> command runs a number of self-consistency checks on
2467 the repository, and reports on any problems. This may take some
2468 time. The most common warning by far is about "dangling"
2469 objects:</para>
2470 <programlisting>
2471 <literal>$ git fsck
2472 dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
2473 dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
2474 dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
2475 dangling blob 218761f9d90712d37a9c5e36f406f92202db07eb
2476 dangling commit bf093535a34a4d35731aa2bd90fe6b176302f14f
2477 dangling commit 8e4bec7f2ddaa268bef999853c25755452100f8e
2478 dangling tree d50bb86186bf27b681d25af89d3b5b68382e4085
2479 dangling tree b24c2473f1fd3d91352a624795be026d64c8841f
2481 </literal>
2482 </programlisting>
2484 <para>Dangling objects are not a problem. At worst they may take up
2485 a little extra disk space. They can sometimes provide a last-resort
2486 method for recovering lost work.</para>
2487 </sect3></sect2><sect2 id="Setting_Up_A_Public_Repositor"><title>Setting
2488 Up A Public Repository</title>
2489 <para>Assume your personal repository is in the directory ~/proj.
2490 We first create a new clone of the repository and tell git-daemon
2491 that it is meant to be public:</para>
2492 <programlisting>
2493 <literal>$ git clone --bare ~/proj proj.git
2494 $ touch proj.git/git-daemon-export-ok
2495 </literal>
2496 </programlisting>
2498 <para>The resulting directory proj.git contains a "bare" git
2499 repository--it is just the contents of the ".git" directory,
2500 without any files checked out around it.</para>
2501 <para>Next, copy proj.git to the server where you plan to host the
2502 public repository. You can use scp, rsync, or whatever is most
2503 convenient.</para>
2504 <sect3 id="Exporting_a_git_repository_via_the_git_proto"><title>Exporting
2505 a git repository via the git protocol</title>
2506 <para>This is the preferred method.</para>
2507 <para>If someone else administers the server, they should tell you
2508 what directory to put the repository in, and what git:// URL it
2509 will appear at.</para>
2510 <para>Otherwise, all you need to do is start
2511 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-daemon.html">git
2512 daemon</ulink>; it will listen on port 9418. By default, it will
2513 allow access to any directory that looks like a git directory and
2514 contains the magic file git-daemon-export-ok. Passing some
2515 directory paths as git-daemon arguments will further restrict the
2516 exports to those paths.</para>
2517 <para>You can also run git-daemon as an inetd service; see the
2518 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-daemon.html">git
2519 daemon</ulink> man page for details. (See especially the examples
2520 section.)</para>
2521 </sect3><sect3 id="Exporting_a_git_repository_via_htt"><title>Exporting
2522 a git repository via http</title>
2523 <para>The git protocol gives better performance and reliability,
2524 but on a host with a web server set up, http exports may be simpler
2525 to set up.</para>
2526 <para>All you need to do is place the newly created bare git
2527 repository in a directory that is exported by the web server, and
2528 make some adjustments to give web clients some extra information
2529 they need:</para>
2530 <programlisting>
2531 <literal>$ mv proj.git /home/you/public_html/proj.git
2532 $ cd proj.git
2533 $ git --bare update-server-info
2534 $ chmod a+x hooks/post-update
2535 </literal>
2536 </programlisting>
2538 <para>(For an explanation of the last two lines, see
2539 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-update-server-info.html">git
2540 update-server-info</ulink> and
2541 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/githooks.html">githooks</ulink>.)</para>
2542 <para>Advertise the URL of proj.git. Anybody else should then be
2543 able to clone or pull from that URL, for example with a command
2544 line like:</para>
2545 <programlisting>
2546 <literal>$ git clone http://yourserver.com/~you/proj.git
2547 </literal>
2548 </programlisting>
2550 </sect3></sect2><sect2 id="Setting_Up_a_Private_Repositor"><title>Setting
2551 Up a Private Repository</title>
2552 <para>If you need to setup a private repository and want to do so
2553 locally, rather than using a hosted solution, you have a number of
2554 options.</para>
2555 <sect3 id="Repo_Access_over_SS"><title>Repo Access over SSH</title>
2556 <para>Generally, the easiest solution is to simply use Git over
2557 SSH. If users already have ssh accounts on a machine, you can put
2558 the git repository anywhere on the box that they have access to and
2559 let them access it over normal ssh logins. For example, say you
2560 have a repository you want to host. You can export it as a bare
2561 repo and then scp it onto your server like so:</para>
2562 <programlisting>
2563 <literal>$ git clone --bare /home/user/myrepo/.git /tmp/myrepo.git
2564 $ scp -r /tmp/myrepo.git myserver.com:/opt/git/myrepo.git
2565 </literal>
2566 </programlisting>
2568 <para>Then someone else with an ssh account on myserver.com can
2569 clone via:</para>
2570 <programlisting>
2571 <literal>$ git clone myserver.com:/opt/git/myrepo.git
2572 </literal>
2573 </programlisting>
2575 <para>Which will simply prompt them for thier ssh password or use
2576 thier public key, however they have ssh authentication
2577 setup.</para>
2578 </sect3><sect3 id="Multiple_User_Access_using_Gitosi"><title>Multiple
2579 User Access using Gitosis</title>
2580 <para>If you don't want to setup seperate accounts for every user,
2581 you can use a tool called Gitosis. In gitosis, there is an
2582 authorized_keys file that contains the public keys of everyone
2583 authorized to access the repository, and then everyone uses the
2584 'git' user to do pushes and pulls.</para>
2585 <para><ulink url="http://www.urbanpuddle.com/articles/2008/07/11/installing-git-on-a-server-ubuntu-or-debian">Installing
2586 and Setting up Gitosis</ulink></para>
2587 </sect3></sect2></sect1><sect1 id="Advanced_Gi"><title>Advanced
2588 Git</title>
2589 <sect2 id="Creating_New_Empty_Branche"><title>Creating New Empty
2590 Branches</title>
2591 <para>Ocasionally, you may want to keep branches in your repository
2592 that do not share an ancestor with your normal code. Some examples
2593 of this might be generated documentation or something along those
2594 lines. If you want to create a new branch head that does not use
2595 your current codebase as a parent, you can create an empty branch
2596 like this:</para>
2597 <programlisting>
2598 <literal>git symbolic-ref HEAD refs/heads/newbranch 
2599 rm .git/index 
2600 git clean -fdx 
2601 &lt;do work&gt; 
2602 git add your files 
2603 git commit -m 'Initial commit'
2604 </literal>
2605 </programlisting>
2607 </sect2><sect2 id="Modifying_your_Histor"><title>Modifying your
2608 History</title>
2609 <para>Interactive rebasing is a good way to modify individual
2610 commits.</para>
2611 <para><ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-filter-branch.html">git
2612 filter-branch</ulink> is a good way to edit commits en
2613 masse.</para>
2614 </sect2><sect2 id="Advanced_Branching_And_Mergin"><title>Advanced
2615 Branching And Merging</title>
2616 <sect3 id="Getting_conflict-resolution_help_during_a_me"><title>Getting
2617 conflict-resolution help during a merge</title>
2618 <para>All of the changes that git was able to merge automatically
2619 are already added to the index file, so
2620 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-diff.html">git
2621 diff</ulink> shows only the conflicts. It uses an unusual
2622 syntax:</para>
2623 <programlisting>
2624 <literal>$ git diff
2625 diff --cc file.txt
2626 index 802992c,2b60207..0000000
2627 --- a/file.txt
2628 +++ b/file.txt
2629 @@@ -1,1 -1,1 +1,5 @@@
2630 ++&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD:file.txt
2631  +Hello world
2632 ++=======
2633 + Goodbye
2634 ++&gt;&gt;&gt;&gt;&gt;&gt;&gt; 77976da35a11db4580b80ae27e8d65caf5208086:file.txt
2635 </literal>
2636 </programlisting>
2638 <para>Recall that the commit which will be committed after we
2639 resolve this conflict will have two parents instead of the usual
2640 one: one parent will be HEAD, the tip of the current branch; the
2641 other will be the tip of the other branch, which is stored
2642 temporarily in MERGE_HEAD.</para>
2643 <para>During the merge, the index holds three versions of each
2644 file. Each of these three "file stages" represents a different
2645 version of the file:</para>
2646 <programlisting>
2647 <literal>$ git show :1:file.txt  # the file in a common ancestor of both branches
2648 $ git show :2:file.txt  # the version from HEAD.
2649 $ git show :3:file.txt  # the version from MERGE_HEAD.
2650 </literal>
2651 </programlisting>
2653 <para>When you ask
2654 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-diff.html">git
2655 diff</ulink> to show the conflicts, it runs a three-way diff
2656 between the conflicted merge results in the work tree with stages 2
2657 and 3 to show only hunks whose contents come from both sides, mixed
2658 (in other words, when a hunk's merge results come only from stage
2659 2, that part is not conflicting and is not shown. Same for stage
2660 3).</para>
2661 <para>The diff above shows the differences between the working-tree
2662 version of file.txt and the stage 2 and stage 3 versions. So
2663 instead of preceding each line by a single "+" or "-", it now uses
2664 two columns: the first column is used for differences between the
2665 first parent and the working directory copy, and the second for
2666 differences between the second parent and the working directory
2667 copy. (See the "COMBINED DIFF FORMAT" section of
2668 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-diff-files.html">git
2669 diff-files</ulink> for a details of the format.)</para>
2670 <para>After resolving the conflict in the obvious way (but before
2671 updating the index), the diff will look like:</para>
2672 <programlisting>
2673 <literal>$ git diff
2674 diff --cc file.txt
2675 index 802992c,2b60207..0000000
2676 --- a/file.txt
2677 +++ b/file.txt
2678 @@@ -1,1 -1,1 +1,1 @@@
2679 - Hello world
2680 -Goodbye
2681 ++Goodbye world
2682 </literal>
2683 </programlisting>
2685 <para>This shows that our resolved version deleted "Hello world"
2686 from the first parent, deleted "Goodbye" from the second parent,
2687 and added "Goodbye world", which was previously absent from
2688 both.</para>
2689 <para>Some special diff options allow diffing the working directory
2690 against any of these stages:</para>
2691 <programlisting>
2692 <literal>$ git diff -1 file.txt      # diff against stage 1
2693 $ git diff --base file.txt  # same as the above
2694 $ git diff -2 file.txt      # diff against stage 2
2695 $ git diff --ours file.txt  # same as the above
2696 $ git diff -3 file.txt      # diff against stage 3
2697 $ git diff --theirs file.txt    # same as the above.
2698 </literal>
2699 </programlisting>
2701 <para>The
2702 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-log.html">git
2703 log</ulink> and
2704 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/gitk.html">gitk</ulink>
2705 commands also provide special help for merges:</para>
2706 <programlisting>
2707 <literal>$ git log --merge
2708 $ gitk --merge
2709 </literal>
2710 </programlisting>
2712 <para>These will display all commits which exist only on HEAD or on
2713 MERGE_HEAD, and which touch an unmerged file.</para>
2714 <para>You may also use
2715 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-mergetool.html">git
2716 mergetool</ulink>, which lets you merge the unmerged files using
2717 external tools such as emacs or kdiff3.</para>
2718 <para>Each time you resolve the conflicts in a file and update the
2719 index:</para>
2720 <programlisting>
2721 <literal>$ git add file.txt
2722 </literal>
2723 </programlisting>
2725 <para>the different stages of that file will be "collapsed", after
2726 which git-diff will (by default) no longer show diffs for that
2727 file.</para>
2728 </sect3><sect3 id="Multiway_Merg"><title>Multiway Merge</title>
2729 <para>You can merge several heads at one time by simply listing
2730 them on the same
2731 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-merge.html">git
2732 merge</ulink> command. For instance,</para>
2733 <programlisting>
2734 <literal>$ git merge scott/master rick/master tom/master
2735 </literal>
2736 </programlisting>
2738 <para>is the equivalent of:</para>
2739 <programlisting>
2740 <literal>$ git merge scott/master
2741 $ git merge rick/master
2742 $ git merge tom/master
2743 </literal>
2744 </programlisting>
2746 </sect3><sect3 id="Subtre"><title>Subtree</title>
2747 <para>There are situations where you want to include contents in
2748 your project from an independently developed project. You can just
2749 pull from the other project as long as there are no conflicting
2750 paths.</para>
2751 <para>The problematic case is when there are conflicting files.
2752 Potential candidates are Makefiles and other standard filenames.
2753 You could merge these files but probably you do not want to. A
2754 better solution for this problem can be to merge the project as its
2755 own subdirectory. This is not supported by the recursive merge
2756 strategy, so just pulling won't work.</para>
2757 <para>What you want is the subtree merge strategy, which helps you
2758 in such a situation.</para>
2759 <para>In this example, let's say you have the repository at
2760 /path/to/B (but it can be an URL as well, if you want). You want to
2761 merge the master branch of that repository to the dir-B
2762 subdirectory in your current branch.</para>
2763 <para>Here is the command sequence you need:</para>
2764 <programlisting>
2765 <literal>$ git remote add -f Bproject /path/to/B (1)
2766 $ git merge -s ours --no-commit Bproject/master (2)
2767 $ git read-tree --prefix=dir-B/ -u Bproject/master (3)
2768 $ git commit -m "Merge B project as our subdirectory" (4)
2769 $ git pull -s subtree Bproject master (5)
2770 </literal>
2771 </programlisting>
2773 <para>The benefit of using subtree merge is that it requires less
2774 administrative burden from the users of your repository. It works
2775 with older (before Git v1.5.2) clients and you have the code right
2776 after clone.</para>
2777 <para>However if you use submodules then you can choose not to
2778 transfer the submodule objects. This may be a problem with the
2779 subtree merge.</para>
2780 <para>Also, in case you make changes to the other project, it is
2781 easier to submit changes if you just use submodules.</para>
2782 <para>(from
2783 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/howto/using-merge-subtree.html">Using
2784 Subtree Merge</ulink>)</para>
2785 </sect3></sect2><sect2 id="Finding_Issues_-_Git_Bisec"><title>Finding
2786 Issues - Git Bisect</title>
2787 <para>Suppose version 2.6.18 of your project worked, but the
2788 version at "master" crashes. Sometimes the best way to find the
2789 cause of such a regression is to perform a brute-force search
2790 through the project's history to find the particular commit that
2791 caused the problem. The
2792 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-bisect.html">git
2793 bisect</ulink> command can help you do this:</para>
2794 <programlisting>
2795 <literal>$ git bisect start
2796 $ git bisect good v2.6.18
2797 $ git bisect bad master
2798 Bisecting: 3537 revisions left to test after this
2799 [65934a9a028b88e83e2b0f8b36618fe503349f8e] BLOCK: Make USB storage depend on SCSI rather than selecting it [try #6]
2800 </literal>
2801 </programlisting>
2803 <para>If you run "git branch" at this point, you'll see that git
2804 has temporarily moved you to a new branch named "bisect". This
2805 branch points to a commit (with commit id 65934...) that is
2806 reachable from "master" but not from v2.6.18. Compile and test it,
2807 and see whether it crashes. Assume it does crash. Then:</para>
2808 <programlisting>
2809 <literal>$ git bisect bad
2810 Bisecting: 1769 revisions left to test after this
2811 [7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings
2812 </literal>
2813 </programlisting>
2815 <para>checks out an older version. Continue like this, telling git
2816 at each stage whether the version it gives you is good or bad, and
2817 notice that the number of revisions left to test is cut
2818 approximately in half each time.</para>
2819 <para>After about 13 tests (in this case), it will output the
2820 commit id of the guilty commit. You can then examine the commit
2821 with
2822 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-show.html">git
2823 show</ulink>, find out who wrote it, and mail them your bug report
2824 with the commit id. Finally, run</para>
2825 <programlisting>
2826 <literal>$ git bisect reset
2827 </literal>
2828 </programlisting>
2830 <para>to return you to the branch you were on before and delete the
2831 temporary "bisect" branch.</para>
2832 <para>Note that the version which git-bisect checks out for you at
2833 each point is just a suggestion, and you're free to try a different
2834 version if you think it would be a good idea. For example,
2835 occasionally you may land on a commit that broke something
2836 unrelated; run</para>
2837 <programlisting>
2838 <literal>$ git bisect visualize
2839 </literal>
2840 </programlisting>
2842 <para>which will run gitk and label the commit it chose with a
2843 marker that says "bisect". Choose a safe-looking commit nearby,
2844 note its commit id, and check it out with:</para>
2845 <programlisting>
2846 <literal>$ git reset --hard fb47ddb2db...
2847 </literal>
2848 </programlisting>
2850 <para>then test, run "bisect good" or "bisect bad" as appropriate,
2851 and continue.</para>
2852 </sect2><sect2 id="Finding_Issues_-_Git_Blam"><title>Finding Issues
2853 - Git Blame</title>
2854 <para>The
2855 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-blame.html">git
2856 blame</ulink> command is really helpful for figuring out who
2857 changed which sections of a file. If you simple run 'git blame
2858 [filename]' you'll get an output of the entire file with the last
2859 commit sha, date and author for every line in the file.</para>
2860 <programlisting>
2861 <literal>$ git blame sha1_file.c
2863 0fcfd160 (Linus Torvalds  2005-04-18 13:04:43 -0700    8)  */
2864 0fcfd160 (Linus Torvalds  2005-04-18 13:04:43 -0700    9) #include "cache.h"
2865 1f688557 (Junio C Hamano  2005-06-27 03:35:33 -0700   10) #include "delta.h"
2866 a733cb60 (Linus Torvalds  2005-06-28 14:21:02 -0700   11) #include "pack.h"
2867 8e440259 (Peter Eriksen   2006-04-02 14:44:09 +0200   12) #include "blob.h"
2868 8e440259 (Peter Eriksen   2006-04-02 14:44:09 +0200   13) #include "commit.h"
2869 8e440259 (Peter Eriksen   2006-04-02 14:44:09 +0200   14) #include "tag.h"
2870 8e440259 (Peter Eriksen   2006-04-02 14:44:09 +0200   15) #include "tree.h"
2871 f35a6d3b (Linus Torvalds  2007-04-09 21:20:29 -0700   16) #include "refs.h"
2872 70f5d5d3 (Nicolas Pitre   2008-02-28 00:25:19 -0500   17) #include "pack-revindex.h"628522ec (Junio C Hamano              2007-12-29 02:05:47 -0800   18) #include "sha1-lookup.h"
2874 </literal>
2875 </programlisting>
2877 <para>This is often helpful if a file had a line reverted or a
2878 mistake that broke the build to help you see who changed that line
2879 last.</para>
2880 <para>You can also specify a start and end line for the
2881 blame:</para>
2882 <programlisting>
2883 <literal>$&gt;git blame -L 160,+10 sha1_file.c 
2884 ace1534d (Junio C Hamano 2005-05-07 00:38:04 -0700       160)}
2885 ace1534d (Junio C Hamano 2005-05-07 00:38:04 -0700       161)
2886 0fcfd160 (Linus Torvalds 2005-04-18 13:04:43 -0700       162)/*
2887 0fcfd160 (Linus Torvalds 2005-04-18 13:04:43 -0700       163) * NOTE! This returns a statically allocate
2888 790296fd (Jim Meyering   2008-01-03 15:18:07 +0100       164) * careful about using it. Do an "xstrdup()
2889 0fcfd160 (Linus Torvalds 2005-04-18 13:04:43 -0700       165) * filename.
2890 ace1534d (Junio C Hamano 2005-05-07 00:38:04 -0700       166) *
2891 ace1534d (Junio C Hamano 2005-05-07 00:38:04 -0700       167) * Also note that this returns the location
2892 ace1534d (Junio C Hamano 2005-05-07 00:38:04 -0700       168) * SHA1 file can happen from any alternate 
2893 d19938ab (Junio C Hamano 2005-05-09 17:57:56 -0700       169) * DB_ENVIRONMENT environment variable if i
2894 </literal>
2895 </programlisting>
2897 </sect2><sect2 id="Git_and_Emai"><title>Git and Email</title>
2898 <sect3 id="Submitting_patches_to_a_projec"><title>Submitting
2899 patches to a project</title>
2900 <para>If you just have a few changes, the simplest way to submit
2901 them may just be to send them as patches in email:</para>
2902 <para>First, use
2903 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-format-patch.html">git
2904 format-patch</ulink>; for example:</para>
2905 <programlisting>
2906 <literal>$ git format-patch origin
2907 </literal>
2908 </programlisting>
2910 <para>will produce a numbered series of files in the current
2911 directory, one for each patch in the current branch but not in
2912 origin/HEAD.</para>
2913 <para>You can then import these into your mail client and send them
2914 by hand. However, if you have a lot to send at once, you may prefer
2915 to use the
2916 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-send-email.html">git
2917 send-email</ulink> script to automate the process. Consult the
2918 mailing list for your project first to determine how they prefer
2919 such patches be handled.</para>
2920 </sect3><sect3 id="Importing_patches_to_a_projec"><title>Importing
2921 patches to a project</title>
2922 <para>Git also provides a tool called
2923 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-am.html">git
2924 am</ulink> (am stands for "apply mailbox"), for importing such an
2925 emailed series of patches. Just save all of the patch-containing
2926 messages, in order, into a single mailbox file, say "patches.mbox",
2927 then run</para>
2928 <programlisting>
2929 <literal>$ git am -3 patches.mbox
2930 </literal>
2931 </programlisting>
2933 <para>Git will apply each patch in order; if any conflicts are
2934 found, it will stop, and you can manually fix the conflicts and
2935 resolve the merge. (The "-3" option tells git to perform a merge;
2936 if you would prefer it just to abort and leave your tree and index
2937 untouched, you may omit that option.)</para>
2938 <para>Once the index is updated with the results of the conflict
2939 resolution, instead of creating a new commit, just run</para>
2940 <programlisting>
2941 <literal>$ git am --resolved
2942 </literal>
2943 </programlisting>
2945 <para>and git will create the commit for you and continue applying
2946 the remaining patches from the mailbox.</para>
2947 <para>The final result will be a series of commits, one for each
2948 patch in the original mailbox, with authorship and commit log
2949 message each taken from the message containing each patch.</para>
2950 </sect3></sect2><sect2 id="Customizing_Gi"><title>Customizing
2951 Git</title>
2952 <para><ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-config.html">git
2953 config</ulink></para>
2954 <sect3 id="Changing_your_Edito"><title>Changing your Editor</title>
2955 <programlisting>
2956 <literal>$ git config --global core.editor emacs
2957 </literal>
2958 </programlisting>
2960 </sect3><sect3 id="Adding_Aliase"><title>Adding Aliases</title>
2961 <programlisting>
2962 <literal>$ git config --global alias.last 'cat-file commit HEAD'
2964 $ git last
2965 tree c85fbd1996b8e7e5eda1288b56042c0cdb91836b
2966 parent cdc9a0a28173b6ba4aca00eb34f5aabb39980735
2967 author Scott Chacon &lt;schacon@gmail.com&gt; 1220473867 -0700
2968 committer Scott Chacon &lt;schacon@gmail.com&gt; 1220473867 -0700
2970 fixed a weird formatting problem
2972 $ git cat-file commit HEAD
2973 tree c85fbd1996b8e7e5eda1288b56042c0cdb91836b
2974 parent cdc9a0a28173b6ba4aca00eb34f5aabb39980735
2975 author Scott Chacon &lt;schacon@gmail.com&gt; 1220473867 -0700
2976 committer Scott Chacon &lt;schacon@gmail.com&gt; 1220473867 -0700
2978 fixed a weird formatting problem
2979 </literal>
2980 </programlisting>
2982 </sect3><sect3 id="Adding_Colo"><title>Adding Color</title>
2983 <para>See all color.* options in the
2984 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-config.html">git
2985 config</ulink> docs</para>
2986 <programlisting>
2987 <literal>$ git config color.branch auto
2988 $ git config color.diff auto
2989 $ git config color.interactive auto
2990 $ git config color.status auto
2991 </literal>
2992 </programlisting>
2994 <para>Or, you can set all of them on with the color.ui
2995 option:</para>
2996 <programlisting>
2997 <literal>$ git config color.ui true
2998 </literal>
2999 </programlisting>
3001 </sect3><sect3 id="Commit_Templat"><title>Commit Template</title>
3002 <programlisting>
3003 <literal>$ git config commit.template '/etc/git-commit-template'
3004 </literal>
3005 </programlisting>
3007 </sect3><sect3 id="Log_Forma"><title>Log Format</title>
3008 <programlisting>
3009 <literal>$ git config format.pretty oneline
3010 </literal>
3011 </programlisting>
3013 </sect3><sect3 id="Other_Config_Option"><title>Other Config
3014 Options</title>
3015 <para>There are also a number of interesting options for packing,
3016 gc-ing, merging, remotes, branches, http transport, diffs, paging,
3017 whitespace and more. If you want to tweak these, check out the
3018 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-config.html">git
3019 config</ulink> docs.</para>
3020 </sect3></sect2><sect2 id="Git_Hook"><title>Git Hooks</title>
3021 <para>Hooks are little scripts you can place in $GIT_DIR/hooks
3022 directory to trigger action at certain points. When git-init is
3023 run, a handful example hooks are copied in the hooks directory of
3024 the new repository, but by default they are all disabled. To enable
3025 a hook, rename it by removing its .sample suffix.</para>
3026 <sect3 id="applypatch-ms"><title>applypatch-msg</title>
3027 <programlisting>
3028 <literal>GIT_DIR/hooks/applypatch-msg
3029 </literal>
3030 </programlisting>
3032 <para>This hook is invoked by git-am script. It takes a single
3033 parameter, the name of the file that holds the proposed commit log
3034 message. Exiting with non-zero status causes git-am to abort before
3035 applying the patch.</para>
3036 <para>The hook is allowed to edit the message file in place, and
3037 can be used to normalize the message into some project standard
3038 format (if the project has one). It can also be used to refuse the
3039 commit after inspecting the message file. The default
3040 applypatch-msg hook, when enabled, runs the commit-msg hook, if the
3041 latter is enabled.</para>
3042 </sect3><sect3 id="pre-applypatc"><title>pre-applypatch</title>
3043 <programlisting>
3044 <literal>GIT_DIR/hooks/pre-applypatch
3045 </literal>
3046 </programlisting>
3048 <para>This hook is invoked by git-am. It takes no parameter, and is
3049 invoked after the patch is applied, but before a commit is made. If
3050 it exits with non-zero status, then the working tree will not be
3051 committed after applying the patch.</para>
3052 <para>It can be used to inspect the current working tree and refuse
3053 to make a commit if it does not pass certain test. The default
3054 pre-applypatch hook, when enabled, runs the pre-commit hook, if the
3055 latter is enabled.</para>
3056 </sect3><sect3 id="post-applypatc"><title>post-applypatch</title>
3057 <programlisting>
3058 <literal>GIT_DIR/hooks/post-applypatch
3059 </literal>
3060 </programlisting>
3062 <para>This hook is invoked by 'git-am'. It takes no parameter, and
3063 is invoked after the patch is applied and a commit is made.</para>
3064 <para>This hook is meant primarily for notification, and cannot
3065 affect the outcome of 'git-am'.</para>
3066 </sect3><sect3 id="pre-commi"><title>pre-commit</title>
3067 <programlisting>
3068 <literal>GIT_DIR/hooks/pre-commit
3069 </literal>
3070 </programlisting>
3072 <para>This hook is invoked by 'git-commit', and can be bypassed
3073 with <literal>\--no-verify</literal> option. It takes no parameter,
3074 and is invoked before obtaining the proposed commit log message and
3075 making a commit. Exiting with non-zero status from this script
3076 causes the 'git-commit' to abort.</para>
3077 <para>The default 'pre-commit' hook, when enabled, catches
3078 introduction of lines with trailing whitespaces and aborts the
3079 commit when such a line is found.</para>
3080 <para>All the 'git-commit' hooks are invoked with the environment
3081 variable <literal>GIT_EDITOR=:</literal> if the command will not
3082 bring up an editor to modify the commit message.</para>
3083 <para>Here is an example of a Ruby script that runs RSpec tests
3084 before allowing a commit.</para>
3085 <programlisting>
3086   
3087 html_path = "spec_results.html"  
3088 `spec -f h:#{html_path} -f p spec` # run the spec. send progress to screen. save html results to html_path  
3090 # find out how many errors were found  
3091 html = open(html_path).read  
3092 examples = html.match(/(\d+) examples/)[0].to_i rescue 0  
3093 failures = html.match(/(\d+) failures/)[0].to_i rescue 0  
3094 pending = html.match(/(\d+) pending/)[0].to_i rescue 0  
3096 if failures.zero?  
3097   puts "0 failures! #{examples} run, #{pending} pending"  
3098 else  
3099   puts "\aDID NOT COMMIT YOUR FILES!"  
3100   puts "View spec results at #{File.expand_path(html_path)}"  
3101   puts  
3102   puts "#{failures} failures! #{examples} run, #{pending} pending"  
3103   exit 1  
3105 </programlisting>
3107 </sect3><sect3 id="prepare-commit-ms"><title>prepare-commit-msg</title>
3108 <programlisting>
3109 <literal>GIT_DIR/hooks/prepare-commit-msg
3110 </literal>
3111 </programlisting>
3113 <para>This hook is invoked by 'git-commit' right after preparing
3114 the default log message, and before the editor is started.</para>
3115 <para>It takes one to three parameters. The first is the name of
3116 the file that the commit log message. The second is the source of
3117 the commit message, and can be: <literal>message</literal> (if a
3118 <literal>-m</literal> or <literal>-F</literal> option was given);
3119 <literal>template</literal> (if a <literal>-t</literal> option was
3120 given or the configuration option
3121 <literal>commit.template</literal> is set);
3122 <literal>merge</literal> (if the commit is a merge or a
3123 <literal>.git/MERGE_MSG</literal> file exists);
3124 <literal>squash</literal> (if a <literal>.git/SQUASH_MSG</literal>
3125 file exists); or <literal>commit</literal>, followed by a commit
3126 SHA1 (if a <literal>-c</literal>, <literal>-C</literal> or
3127 <literal>\--amend</literal> option was given).</para>
3128 <para>If the exit status is non-zero, 'git-commit' will
3129 abort.</para>
3130 <para>The purpose of the hook is to edit the message file in place,
3131 and it is not suppressed by the <literal>\--no-verify</literal>
3132 option. A non-zero exit means a failure of the hook and aborts the
3133 commit. It should not be used as replacement for pre-commit
3134 hook.</para>
3135 <para>The sample <literal>prepare-commit-msg</literal> hook that
3136 comes with git comments out the <literal>Conflicts:</literal> part
3137 of a merge's commit message.</para>
3138 </sect3><sect3 id="commit-ms"><title>commit-msg</title>
3139 <programlisting>
3140 <literal>GIT_DIR/hooks/commit-msg
3141 </literal>
3142 </programlisting>
3144 <para>This hook is invoked by 'git-commit', and can be bypassed
3145 with <literal>\--no-verify</literal> option. It takes a single
3146 parameter, the name of the file that holds the proposed commit log
3147 message. Exiting with non-zero status causes the 'git-commit' to
3148 abort.</para>
3149 <para>The hook is allowed to edit the message file in place, and
3150 can be used to normalize the message into some project standard
3151 format (if the project has one). It can also be used to refuse the
3152 commit after inspecting the message file.</para>
3153 <para>The default 'commit-msg' hook, when enabled, detects
3154 duplicate "Signed-off-by" lines, and aborts the commit if one is
3155 found.</para>
3156 </sect3><sect3 id="post-commi"><title>post-commit</title>
3157 <programlisting>
3158 <literal>GIT_DIR/hooks/post-commit
3159 </literal>
3160 </programlisting>
3162 <para>This hook is invoked by 'git-commit'. It takes no parameter,
3163 and is invoked after a commit is made.</para>
3164 <para>This hook is meant primarily for notification, and cannot
3165 affect the outcome of 'git-commit'.</para>
3166 </sect3><sect3 id="pre-rebas"><title>pre-rebase</title>
3167 <programlisting>
3168 <literal>GIT_DIR/hooks/pre-rebase
3169 </literal>
3170 </programlisting>
3172 <para>This hook is called by 'git-rebase' and can be used to
3173 prevent a branch from getting rebased.</para>
3174 </sect3><sect3 id="post-checkou"><title>post-checkout</title>
3175 <programlisting>
3176 <literal>GIT_DIR/hooks/post-checkout
3177 </literal>
3178 </programlisting>
3180 <para>This hook is invoked when a 'git-checkout' is run after
3181 having updated the worktree. The hook is given three parameters:
3182 the ref of the previous HEAD, the ref of the new HEAD (which may or
3183 may not have changed), and a flag indicating whether the checkout
3184 was a branch checkout (changing branches, flag=1) or a file
3185 checkout (retrieving a file from the index, flag=0). This hook
3186 cannot affect the outcome of 'git-checkout'.</para>
3187 <para>This hook can be used to perform repository validity checks,
3188 auto-display differences from the previous HEAD if different, or
3189 set working dir metadata properties.</para>
3190 </sect3><sect3 id="post-merg"><title>post-merge</title>
3191 <programlisting>
3192 <literal>GIT_DIR/hooks/post-merge
3193 </literal>
3194 </programlisting>
3196 <para>This hook is invoked by 'git-merge', which happens when a
3197 'git-pull' is done on a local repository. The hook takes a single
3198 parameter, a status flag specifying whether or not the merge being
3199 done was a squash merge. This hook cannot affect the outcome of
3200 'git-merge' and is not executed, if the merge failed due to
3201 conflicts.</para>
3202 <para>This hook can be used in conjunction with a corresponding
3203 pre-commit hook to save and restore any form of metadata associated
3204 with the working tree (eg: permissions/ownership, ACLS, etc). See
3205 contrib/hooks/setgitperms.perl for an example of how to do
3206 this.</para>
3207 </sect3><sect3 id="pre-receiv"><title>pre-receive</title>
3208 <programlisting>
3209 <literal>GIT_DIR/hooks/pre-receive
3210 </literal>
3211 </programlisting>
3213 <para>This hook is invoked by 'git-receive-pack' on the remote
3214 repository, which happens when a 'git-push' is done on a local
3215 repository. Just before starting to update refs on the remote
3216 repository, the pre-receive hook is invoked. Its exit status
3217 determines the success or failure of the update.</para>
3218 <para>This hook executes once for the receive operation. It takes
3219 no arguments, but for each ref to be updated it receives on
3220 standard input a line of the format:</para>
3221 <para>&lt;old-value&gt; SP &lt;new-value&gt; SP &lt;ref-name&gt;
3222 LF</para>
3223 <para>where <literal>&lt;old-value&gt;</literal> is the old object
3224 name stored in the ref, <literal>&lt;new-value&gt;</literal> is the
3225 new object name to be stored in the ref and
3226 <literal>&lt;ref-name&gt;</literal> is the full name of the ref.
3227 When creating a new ref, <literal>&lt;old-value&gt;</literal> is 40
3228 <literal>0</literal>.</para>
3229 <para>If the hook exits with non-zero status, none of the refs will
3230 be updated. If the hook exits with zero, updating of individual
3231 refs can still be prevented by the &lt;&lt;update,'update'&gt;&gt;
3232 hook.</para>
3233 <para>Both standard output and standard error output are forwarded
3234 to 'git-send-pack' on the other end, so you can simply
3235 <literal>echo</literal> messages for the user.</para>
3236 <para>If you wrote it in Ruby, you might get the args this
3237 way:</para>
3238 <programlisting>
3239 rev_old, rev_new, ref = STDIN.read.split(" ")
3240 </programlisting>
3242 <para>Or in a bash script, something like this would work:</para>
3243 <programlisting>
3244 <literal>#!/bin/sh
3245 # &lt;oldrev&gt; &lt;newrev&gt; &lt;refname&gt;
3246 # update a blame tree
3247 while read oldrev newrev ref
3249     echo "STARTING [$oldrev $newrev $ref]"
3250     for path in `git diff-tree -r $oldrev..$newrev | awk '{print $6}'`
3251     do
3252       echo "git update-ref refs/blametree/$ref/$path $newrev"
3253       `git update-ref refs/blametree/$ref/$path $newrev`
3254     done
3255 done
3256 </literal>
3257 </programlisting>
3259 </sect3><sect3 id="updat"><title>update</title>
3260 <programlisting>
3261 <literal>GIT_DIR/hooks/update
3262 </literal>
3263 </programlisting>
3265 <para>This hook is invoked by 'git-receive-pack' on the remote
3266 repository, which happens when a 'git-push' is done on a local
3267 repository. Just before updating the ref on the remote repository,
3268 the update hook is invoked. Its exit status determines the success
3269 or failure of the ref update.</para>
3270 <para>The hook executes once for each ref to be updated, and takes
3271 three parameters:</para>
3272 <itemizedlist>
3273 <listitem>the name of the ref being updated,</listitem>
3274 <listitem>the old object name stored in the ref,</listitem>
3275 <listitem>and the new objectname to be stored in the
3276 ref.</listitem>
3277 </itemizedlist>
3278 <para>A zero exit from the update hook allows the ref to be
3279 updated. Exiting with a non-zero status prevents 'git-receive-pack'
3280 from updating that ref.</para>
3281 <para>This hook can be used to prevent 'forced' update on certain
3282 refs by making sure that the object name is a commit object that is
3283 a descendant of the commit object named by the old object name.
3284 That is, to enforce a "fast forward only" policy.</para>
3285 <para>It could also be used to log the old..new status. However, it
3286 does not know the entire set of branches, so it would end up firing
3287 one e-mail per ref when used naively, though. The
3288 &lt;&lt;post-receive,'post-receive'&gt;&gt; hook is more suited to
3289 that.</para>
3290 <para>Another use suggested on the mailing list is to use this hook
3291 to implement access control which is finer grained than the one
3292 based on filesystem group.</para>
3293 <para>Both standard output and standard error output are forwarded
3294 to 'git-send-pack' on the other end, so you can simply
3295 <literal>echo</literal> messages for the user.</para>
3296 <para>The default 'update' hook, when enabled--and with
3297 <literal>hooks.allowunannotated</literal> config option turned
3298 on--prevents unannotated tags to be pushed.</para>
3299 </sect3><sect3 id="post-receiv"><title>post-receive</title>
3300 <programlisting>
3301 <literal>GIT_DIR/hooks/post-receive
3302 </literal>
3303 </programlisting>
3305 <para>This hook is invoked by 'git-receive-pack' on the remote
3306 repository, which happens when a 'git-push' is done on a local
3307 repository. It executes on the remote repository once after all the
3308 refs have been updated.</para>
3309 <para>This hook executes once for the receive operation. It takes
3310 no arguments, but gets the same information as the
3311 &lt;&lt;pre-receive,'pre-receive'&gt;&gt; hook does on its standard
3312 input.</para>
3313 <para>This hook does not affect the outcome of 'git-receive-pack',
3314 as it is called after the real work is done.</para>
3315 <para>This supersedes the &lt;&lt;post-update,'post-update'&gt;&gt;
3316 hook in that it gets both old and new values of all the refs in
3317 addition to their names.</para>
3318 <para>Both standard output and standard error output are forwarded
3319 to 'git-send-pack' on the other end, so you can simply
3320 <literal>echo</literal> messages for the user.</para>
3321 <para>The default 'post-receive' hook is empty, but there is a
3322 sample script <literal>post-receive-email</literal> provided in the
3323 <literal>contrib/hooks</literal> directory in git distribution,
3324 which implements sending commit emails.</para>
3325 </sect3><sect3 id="post-updat"><title>post-update</title>
3326 <programlisting>
3327 <literal>GIT_DIR/hooks/post-update
3328 </literal>
3329 </programlisting>
3331 <para>This hook is invoked by 'git-receive-pack' on the remote
3332 repository, which happens when a 'git-push' is done on a local
3333 repository. It executes on the remote repository once after all the
3334 refs have been updated.</para>
3335 <para>It takes a variable number of parameters, each of which is
3336 the name of ref that was actually updated.</para>
3337 <para>This hook is meant primarily for notification, and cannot
3338 affect the outcome of 'git-receive-pack'.</para>
3339 <para>The 'post-update' hook can tell what are the heads that were
3340 pushed, but it does not know what their original and updated values
3341 are, so it is a poor place to do log old..new. The
3342 &lt;&lt;post-receive,'post-receive'&gt;&gt; hook does get both
3343 original and updated values of the refs. You might consider it
3344 instead if you need them.</para>
3345 <para>When enabled, the default 'post-update' hook runs
3346 'git-update-server-info' to keep the information used by dumb
3347 transports (e.g., HTTP) up-to-date. If you are publishing a git
3348 repository that is accessible via HTTP, you should probably enable
3349 this hook.</para>
3350 <para>Both standard output and standard error output are forwarded
3351 to 'git-send-pack' on the other end, so you can simply
3352 <literal>echo</literal> messages for the user.</para>
3353 </sect3><sect3 id="pre-auto-g"><title>pre-auto-gc</title>
3354 <programlisting>
3355 <literal>GIT_DIR/hooks/pre-auto-gc
3356 </literal>
3357 </programlisting>
3359 <para>This hook is invoked by 'git-gc --auto'. It takes no
3360 parameter, and exiting with non-zero status from this script causes
3361 the 'git-gc --auto' to abort.</para>
3362 </sect3><sect3 id="Reference"><title>References</title>
3363 <para><ulink url="http://www.kernel.org/pub/software/scm/git/docs/githooks.html">Git
3364 Hooks</ulink> *
3365 http://probablycorey.wordpress.com/2008/03/07/git-hooks-make-me-giddy/</para>
3366 </sect3></sect2><sect2 id="Recovering_Corrupted_Object"><title>Recovering
3367 Corrupted Objects</title>
3368 <para><ulink url="http://programblings.com/2008/06/07/the-illustrated-guide-to-recovering-lost-commits-with-git">Recovering
3369 Lost Commits Blog Post</ulink></para>
3370 <para><ulink url="http://www.kernel.org/pub/software/scm/git/docs/howto/recover-corrupted-blob-object.txt">Recovering
3371 Corrupted Blobs by Linus</ulink></para>
3372 </sect2><sect2 id="Submodule"><title>Submodules</title>
3373 <para>Large projects are often composed of smaller, self-contained
3374 modules. For example, an embedded Linux distribution's source tree
3375 would include every piece of software in the distribution with some
3376 local modifications; a movie player might need to build against a
3377 specific, known-working version of a decompression library; several
3378 independent programs might all share the same build scripts.</para>
3379 <para>With centralized revision control systems this is often
3380 accomplished by including every module in one single repository.
3381 Developers can check out all modules or only the modules they need
3382 to work with. They can even modify files across several modules in
3383 a single commit while moving things around or updating APIs and
3384 translations.</para>
3385 <para>Git does not allow partial checkouts, so duplicating this
3386 approach in Git would force developers to keep a local copy of
3387 modules they are not interested in touching. Commits in an enormous
3388 checkout would be slower than you'd expect as Git would have to
3389 scan every directory for changes. If modules have a lot of local
3390 history, clones would take forever.</para>
3391 <para>On the plus side, distributed revision control systems can
3392 much better integrate with external sources. In a centralized
3393 model, a single arbitrary snapshot of the external project is
3394 exported from its own revision control and then imported into the
3395 local revision control on a vendor branch. All the history is
3396 hidden. With distributed revision control you can clone the entire
3397 external history and much more easily follow development and
3398 re-merge local changes.</para>
3399 <para>Git's submodule support allows a repository to contain, as a
3400 subdirectory, a checkout of an external project. Submodules
3401 maintain their own identity; the submodule support just stores the
3402 submodule repository location and commit ID, so other developers
3403 who clone the containing project ("superproject") can easily clone
3404 all the submodules at the same revision. Partial checkouts of the
3405 superproject are possible: you can tell Git to clone none, some or
3406 all of the submodules.</para>
3407 <para>The
3408 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html">git
3409 submodule</ulink> command is available since Git 1.5.3. Users with
3410 Git 1.5.2 can look up the submodule commits in the repository and
3411 manually check them out; earlier versions won't recognize the
3412 submodules at all.</para>
3413 <para>To see how submodule support works, create (for example) four
3414 example repositories that can be used later as a submodule:</para>
3415 <programlisting>
3416 <literal>$ mkdir ~/git
3417 $ cd ~/git
3418 $ for i in a b c d
3420     mkdir $i
3421     cd $i
3422     git init
3423     echo "module $i" &gt; $i.txt
3424     git add $i.txt
3425     git commit -m "Initial commit, submodule $i"
3426     cd ..
3427 done
3428 </literal>
3429 </programlisting>
3431 <para>Now create the superproject and add all the
3432 submodules:</para>
3433 <programlisting>
3434 <literal>$ mkdir super
3435 $ cd super
3436 $ git init
3437 $ for i in a b c d
3439     git submodule add ~/git/$i $i
3440 done
3441 </literal>
3442 </programlisting>
3444 <para>NOTE: Do not use local URLs here if you plan to publish your
3445 superproject!</para>
3446 <para>See what files <literal>git-submodule</literal>
3447 created:</para>
3448 <programlisting>
3449 <literal>$ ls -a
3450 .  ..  .git  .gitmodules  a  b  c  d
3451 </literal>
3452 </programlisting>
3454 <para>The <literal>git-submodule add</literal> command does a
3455 couple of things:</para>
3456 <itemizedlist>
3457 <listitem>It clones the submodule under the current directory and
3458 by default checks out the master branch.</listitem>
3459 <listitem>It adds the submodule's clone path to the
3460 <para><ulink url="http://www.kernel.org/pub/software/scm/git/docs/gitmodules.html">gitmodules</ulink></para>
3461 file and adds this file to the index, ready to be
3462 committed.</listitem>
3463 <listitem>It adds the submodule's current commit ID to the index,
3464 ready to be committed.</listitem>
3465 </itemizedlist>
3466 <para>Commit the superproject:</para>
3467 <programlisting>
3468 <literal>$ git commit -m "Add submodules a, b, c and d."
3469 </literal>
3470 </programlisting>
3472 <para>Now clone the superproject:</para>
3473 <programlisting>
3474 <literal>$ cd ..
3475 $ git clone super cloned
3476 $ cd cloned
3477 </literal>
3478 </programlisting>
3480 <para>The submodule directories are there, but they're
3481 empty:</para>
3482 <programlisting>
3483 <literal>$ ls -a a
3484 .  ..
3485 $ git submodule status
3486 -d266b9873ad50488163457f025db7cdd9683d88b a
3487 -e81d457da15309b4fef4249aba9b50187999670d b
3488 -c1536a972b9affea0f16e0680ba87332dc059146 c
3489 -d96249ff5d57de5de093e6baff9e0aafa5276a74 d
3490 </literal>
3491 </programlisting>
3493 <para>NOTE: The commit object names shown above would be different
3494 for you, but they should match the HEAD commit object names of your
3495 repositories. You can check it by running <literal>git ls-remote
3496 ../git/a</literal>.</para>
3497 <para>Pulling down the submodules is a two-step process. First run
3498 <literal>git submodule init</literal> to add the submodule
3499 repository URLs to <literal>.git/config</literal>:</para>
3500 <programlisting>
3501 <literal>$ git submodule init
3502 </literal>
3503 </programlisting>
3505 <para>Now use <literal>git-submodule update</literal> to clone the
3506 repositories and check out the commits specified in the
3507 superproject:</para>
3508 <programlisting>
3509 <literal>$ git submodule update
3510 $ cd a
3511 $ ls -a
3512 .  ..  .git  a.txt
3513 </literal>
3514 </programlisting>
3516 <para>One major difference between <literal>git-submodule
3517 update</literal> and <literal>git-submodule add</literal> is that
3518 <literal>git-submodule update</literal> checks out a specific
3519 commit, rather than the tip of a branch. It's like checking out a
3520 tag: the head is detached, so you're not working on a
3521 branch.</para>
3522 <programlisting>
3523 <literal>$ git branch
3524 * (no branch)
3525 master
3526 </literal>
3527 </programlisting>
3529 <para>If you want to make a change within a submodule and you have
3530 a detached head, then you should create or checkout a branch, make
3531 your changes, publish the change within the submodule, and then
3532 update the superproject to reference the new commit:</para>
3533 <programlisting>
3534 <literal>$ git checkout master
3535 </literal>
3536 </programlisting>
3538 <para>or</para>
3539 <programlisting>
3540 <literal>$ git checkout -b fix-up
3541 </literal>
3542 </programlisting>
3544 <para>then</para>
3545 <programlisting>
3546 <literal>$ echo "adding a line again" &gt;&gt; a.txt
3547 $ git commit -a -m "Updated the submodule from within the superproject."
3548 $ git push
3549 $ cd ..
3550 $ git diff
3551 diff --git a/a b/a
3552 index d266b98..261dfac 160000
3553 --- a/a
3554 +++ b/a
3555 @@ -1 +1 @@
3556 -Subproject commit d266b9873ad50488163457f025db7cdd9683d88b
3557 +Subproject commit 261dfac35cb99d380eb966e102c1197139f7fa24
3558 $ git add a
3559 $ git commit -m "Updated submodule a."
3560 $ git push
3561 </literal>
3562 </programlisting>
3564 <para>You have to run <literal>git submodule update</literal> after
3565 <literal>git pull</literal> if you want to update submodules,
3566 too.</para>
3567 <sect3 id="Pitfalls_with_submodule"><title>Pitfalls with
3568 submodules</title>
3569 <para>Always publish the submodule change before publishing the
3570 change to the superproject that references it. If you forget to
3571 publish the submodule change, others won't be able to clone the
3572 repository:</para>
3573 <programlisting>
3574 <literal>$ cd ~/git/super/a
3575 $ echo i added another line to this file &gt;&gt; a.txt
3576 $ git commit -a -m "doing it wrong this time"
3577 $ cd ..
3578 $ git add a
3579 $ git commit -m "Updated submodule a again."
3580 $ git push
3581 $ cd ~/git/cloned
3582 $ git pull
3583 $ git submodule update
3584 error: pathspec '261dfac35cb99d380eb966e102c1197139f7fa24' did not match any file(s) known to git.
3585 Did you forget to 'git add'?
3586 Unable to checkout '261dfac35cb99d380eb966e102c1197139f7fa24' in submodule path 'a'
3587 </literal>
3588 </programlisting>
3590 <para>If you are staging an updated submodule for commit manually,
3591 be careful to not add a trailing slash when specifying the path.
3592 With the slash appended, Git will assume you are removing the
3593 submodule and checking that directory's contents into the
3594 containing repository.</para>
3595 <programlisting>
3596 <literal>$ cd ~/git/super/a
3597 $ echo i added another line to this file &gt;&gt; a.txt
3598 $ git commit -a -m "doing it wrong this time"
3599 $ cd ..
3600 $ git add a/
3601 $ git status
3602 # On branch master
3603 # Changes to be committed:
3604 #   (use "git reset HEAD &lt;file&gt;..." to unstage)
3606 #       deleted:    a
3607 #       new file:   a/a.txt
3609 # Modified submodules:
3611 # * a aa5c351...0000000 (1):
3612 #   &lt; Initial commit, submodule a
3614 </literal>
3615 </programlisting>
3617 <para>To fix the index after performing this operation, reset the
3618 changes and then add the submodule without the trailing
3619 slash.</para>
3620 <programlisting>
3621 <literal>$ git reset HEAD A
3622 $ git add a
3623 $ git status
3624 # On branch master
3625 # Changes to be committed:
3626 #   (use "git reset HEAD &lt;file&gt;..." to unstage)
3628 #       modified:   a
3630 # Modified submodules:
3632 # * a aa5c351...8d3ba36 (1):
3633 #   &gt; doing it wrong this time
3635 </literal>
3636 </programlisting>
3638 <para>You also should not rewind branches in a submodule beyond
3639 commits that were ever recorded in any superproject.</para>
3640 <para>It's not safe to run <literal>git submodule update</literal>
3641 if you've made and committed changes within a submodule without
3642 checking out a branch first. They will be silently
3643 overwritten:</para>
3644 <programlisting>
3645 <literal>$ cat a.txt
3646 module a
3647 $ echo line added from private2 &gt;&gt; a.txt
3648 $ git commit -a -m "line added inside private2"
3649 $ cd ..
3650 $ git submodule update
3651 Submodule path 'a': checked out 'd266b9873ad50488163457f025db7cdd9683d88b'
3652 $ cd a
3653 $ cat a.txt
3654 module a
3655 </literal>
3656 </programlisting>
3658 <para>NOTE: The changes are still visible in the submodule's
3659 reflog.</para>
3660 <para>This is not the case if you did not commit your
3661 changes.</para>
3662 </sect3></sect2></sect1><sect1 id="Working_with_Gi"><title>Working
3663 with Git</title>
3664 <sect2 id="Git_on_Window"><title>Git on Windows</title>
3665 <para>(mSysGit)</para>
3666 </sect2><sect2 id="Deploying_with_Gi"><title>Deploying with
3667 Git</title>
3668 <sect3 id="Capistrano_and_Gi"><title>Capistrano and Git</title>
3669 <para><ulink url="http://github.com/guides/deploying-with-capistrano">GitHub
3670 Guide on Deploying with Cap</ulink></para>
3671 <para><ulink url="http://www.vimeo.com/369095">Git and Capistrano
3672 Screencast</ulink></para>
3673 </sect3></sect2><sect2 id="Subversion_Integratio"><title>Subversion
3674 Integration</title>
3675 </sect2><sect2 id="SCM_Migratio"><title>SCM Migration</title>
3676 <para>So you've made the decision to move away from your existing
3677 system and convert your whole project to Git. How can you do that
3678 easily?</para>
3679 <sect3 id="Importing_Subversio"><title>Importing Subversion</title>
3680 <para>Git comes with a script called git-svn that has a clone
3681 command that will import a subversion repository into a new git
3682 repository. There is also a free tool on the GitHub service that
3683 will do this for you.</para>
3684 <programlisting>
3685 <literal>$ git-svn clone http://my-project.googlecode.com/svn/trunk new-project
3686 </literal>
3687 </programlisting>
3689 <para>This will give you a new Git repository with all the history
3690 of the original Subversion repo. This takes a pretty good amount of
3691 time, generally, since it starts with version 1 and checks out and
3692 commits locally every single revision one by one.</para>
3693 </sect3><sect3 id="Importing_Perforc"><title>Importing
3694 Perforce</title>
3695 <para>In contrib/fast-import you will find the git-p4 script, which
3696 is a Python script that will import a Perforce repository for
3697 you.</para>
3698 <programlisting>
3699 <literal>$ ~/git.git/contrib/fast-import/git-p4 clone //depot/project/main@all myproject
3700 </literal>
3701 </programlisting>
3703 </sect3><sect3 id="Importing_Other"><title>Importing Others</title>
3704 <para>These are other SCMs that listed high on the Git Survey,
3705 should find import docs for them. !!TODO!!</para>
3706 <itemizedlist>
3707 <listitem>CVS</listitem>
3708 <listitem>
3709 <para>Mercurial (hg)</para>
3710 </listitem>
3711 <listitem>
3712 <para>Bazaar-NG</para>
3713 </listitem>
3714 <listitem>Darcs</listitem>
3715 <listitem>ClearCase</listitem>
3716 </itemizedlist>
3717 </sect3></sect2><sect2 id="Graphical_Gi"><title>Graphical
3718 Git</title>
3719 <para>Git has a couple of fairly popular Graphial User Interfaces
3720 that can read and/or maniplulate Git repositories.</para>
3721 <sect3 id="Bundled_GUI"><title>Bundled GUIs</title>
3722 <para>Git comes with two major GUI programs written in Tcl/Tk. Gitk
3723 is a repository browser and commit history visualization
3724 tool.</para>
3725 <para><ulink url="http://www.kernel.org/pub/software/scm/git/docs/gitk.html">gitk</ulink></para>
3726 <para><ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-gui.html">git
3727 gui</ulink> is a tool that helps you visualize the index
3728 operations, like add, remove and commit. It won't do everything you
3729 can do on the command line, but for many of the basic operations,
3730 it's pretty good.</para>
3731 <para><ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-gui.html">git
3732 gui</ulink></para>
3733 </sect3><sect3 id="Third_Party_Project"><title>Third Party
3734 Projects</title>
3735 <para>For Mac users, there are
3736 <ulink url="http://gitx.frim.nl/">GitX</ulink> and
3737 <ulink url="http://github.com/Caged/gitnub/wikis">GitNub</ulink></para>
3738 <para>For Linux or other Qt users, there is
3739 <ulink url="http://digilander.libero.it/mcostalba/">QGit</ulink></para>
3740 </sect3></sect2><sect2 id="Hosted_Gi"><title>Hosted Git</title>
3741 <para>github</para>
3742 <para>repoorcz</para>
3743 </sect2><sect2 id="Alternative_Use"><title>Alternative Uses</title>
3744 <para>ContentDistribution</para>
3745 <para>TicGit</para>
3746 </sect2><sect2 id="Scripting_and_Gi"><title>Scripting and
3747 Git</title>
3748 <sect3 id="Ruby_and_Gi"><title>Ruby and Git</title>
3749 <para>grit</para>
3750 <para>jgit + jruby</para>
3751 </sect3><sect3 id="PHP_and_Gi"><title>PHP and Git</title>
3752 </sect3><sect3 id="Python_and_Gi"><title>Python and Git</title>
3753 <para>pygit</para>
3754 </sect3><sect3 id="Perl_and_Gi"><title>Perl and Git</title>
3755 <para>perlgit</para>
3756 </sect3></sect2><sect2 id="Git_and_Editor"><title>Git and
3757 Editors</title>
3758 <para>textmate</para>
3759 <para>eclipse</para>
3760 <para>netbeans</para>
3761 </sect2></sect1><sect1 id="Internals_and_Plumbin"><title>Internals
3762 and Plumbing</title>
3763 <sect2 id="How_Git_Stores_Object"><title>How Git Stores
3764 Objects</title>
3765 <para>This chapter goes into detail about how Git physically stores
3766 objects.</para>
3767 <para>All objects are stored as compressed contents by their sha
3768 values. They contain the object type, size and contents in a
3769 gzipped format.</para>
3770 <para>There are two formats that Git keeps objects in - loose
3771 objects and packed objects.</para>
3772 <sect3 id="Loose_Object"><title>Loose Objects</title>
3773 <para>Loose objects are the simpler format. It is simply the
3774 compressed data stored in a single file on disk. Every object
3775 written to a seperate file.</para>
3776 <para>If the sha of your object is
3777 <literal>ab04d884140f7b0cf8bbf86d6883869f16a46f65</literal>, then
3778 the file will be stored in the following path:</para>
3779 <programlisting>
3780 <literal>GIT_DIR/objects/ab/04d884140f7b0cf8bbf86d6883869f16a46f65
3781 </literal>
3782 </programlisting>
3784 <para>It pulls the first two characters off and uses that as the
3785 subdirectory, so that there are never too many objects in one
3786 directory. The actual file name is the remaining 38
3787 characters.</para>
3788 <para>The easiest way to describe exactly how the object data is
3789 stored is this Ruby implementation of object storage:</para>
3790 <programlisting>
3791 def put_raw_object(content, type)
3792   size = content.length.to_s
3794   header = "#{type} #{size}\0" # type(space)size(null byte)
3795   store = header + content
3797   sha1 = Digest::SHA1.hexdigest(store)
3798   path = @git_dir + '/' + sha1[0...2] + '/' + sha1[2..40]
3800 if !File.exists?(path)
3801     content = Zlib::Deflate.deflate(store)
3803 FileUtils.mkdir_p(@directory+'/'+sha1[0...2])
3804 File.open(path, 'w') do |f|
3805       f.write content
3808 return sha1
3810 </programlisting>
3812 </sect3><sect3 id="Packed_Object"><title>Packed Objects</title>
3813 <para>The other format for object storage is the packfile. Since
3814 Git stores each version of each file as a seperate object, it can
3815 get pretty inefficient. Imagine having a file several thousand
3816 lines long and changing a single line. Git will store the second
3817 file in it's entirety, which is a great big waste of space.</para>
3818 <para>In order to save that space, Git utilizes the packfile. This
3819 is a format where Git will only save the part that has changed in
3820 the second file, with a pointer to the file it is similar
3821 to.</para>
3822 <para>When objects are written to disk, it is often in the loose
3823 format, since that format is less expensive to access. However,
3824 eventually you'll want to save the space by packing up the objects
3825 - this is done with the
3826 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-gc.html">git
3827 gc</ulink> command. It will use a rather complicated heuristic to
3828 determine which files are likely most similar and base the deltas
3829 off that analysis. There can be multiple packfiles, they can be
3830 repacked if neccesary
3831 (<ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-repack.html">git
3832 repack</ulink>) or unpacked back into loose files
3833 (<ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-unpack-objects.html">git
3834 unpack-objects</ulink>) relatively easily.</para>
3835 <para>Git will also write out an index file for each packfile that
3836 is much smaller and contains offsets into the packfile to more
3837 quickly find specific objects by sha.</para>
3838 <para>The actual details of the packfile implementation are found
3839 in the Packfile chapter a little later on.</para>
3840 </sect3></sect2><sect2 id="Browsing_Git_Object"><title>Browsing Git
3841 Objects</title>
3842 <para>We can ask git about particular objects with the cat-file
3843 command. Note that you can shorten the shas to only a few
3844 characters to save yourself typing all 40 hex digits:</para>
3845 <programlisting>
3846 <literal>$ git-cat-file -t 54196cc2
3847 commit
3848 $ git-cat-file commit 54196cc2
3849 tree 92b8b694ffb1675e5975148e1121810081dbdffe
3850 author J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
3851 committer J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
3853 initial commit
3854 </literal>
3855 </programlisting>
3857 <para>A tree can refer to one or more "blob" objects, each
3858 corresponding to a file. In addition, a tree can also refer to
3859 other tree objects, thus creating a directory hierarchy. You can
3860 examine the contents of any tree using ls-tree (remember that a
3861 long enough initial portion of the SHA1 will also work):</para>
3862 <programlisting>
3863 <literal>$ git ls-tree 92b8b694
3864 100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad    file.txt
3865 </literal>
3866 </programlisting>
3868 <para>Thus we see that this tree has one file in it. The SHA1 hash
3869 is a reference to that file's data:</para>
3870 <programlisting>
3871 <literal>$ git cat-file -t 3b18e512
3872 blob
3873 </literal>
3874 </programlisting>
3876 <para>A "blob" is just file data, which we can also examine with
3877 cat-file:</para>
3878 <programlisting>
3879 <literal>$ git cat-file blob 3b18e512
3880 hello world
3881 </literal>
3882 </programlisting>
3884 <para>Note that this is the old file data; so the object that git
3885 named in its response to the initial tree was a tree with a
3886 snapshot of the directory state that was recorded by the first
3887 commit.</para>
3888 <para>All of these objects are stored under their SHA1 names inside
3889 the git directory:</para>
3890 <programlisting>
3891 <literal>$ find .git/objects/
3892 .git/objects/
3893 .git/objects/pack
3894 .git/objects/info
3895 .git/objects/3b
3896 .git/objects/3b/18e512dba79e4c8300dd08aeb37f8e728b8dad
3897 .git/objects/92
3898 .git/objects/92/b8b694ffb1675e5975148e1121810081dbdffe
3899 .git/objects/54
3900 .git/objects/54/196cc2703dc165cbd373a65a4dcf22d50ae7f7
3901 .git/objects/a0
3902 .git/objects/a0/423896973644771497bdc03eb99d5281615b51
3903 .git/objects/d0
3904 .git/objects/d0/492b368b66bdabf2ac1fd8c92b39d3db916e59
3905 .git/objects/c4
3906 .git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241
3907 </literal>
3908 </programlisting>
3910 <para>and the contents of these files is just the compressed data
3911 plus a header identifying their length and their type. The type is
3912 either a blob, a tree, a commit, or a tag.</para>
3913 <para>The simplest commit to find is the HEAD commit, which we can
3914 find from .git/HEAD:</para>
3915 <programlisting>
3916 <literal>$ cat .git/HEAD
3917 ref: refs/heads/master
3918 </literal>
3919 </programlisting>
3921 <para>As you can see, this tells us which branch we're currently
3922 on, and it tells us this by naming a file under the .git directory,
3923 which itself contains a SHA1 name referring to a commit object,
3924 which we can examine with cat-file:</para>
3925 <programlisting>
3926 <literal>$ cat .git/refs/heads/master
3927 c4d59f390b9cfd4318117afde11d601c1085f241
3928 $ git cat-file -t c4d59f39
3929 commit
3930 $ git cat-file commit c4d59f39
3931 tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59
3932 parent 54196cc2703dc165cbd373a65a4dcf22d50ae7f7
3933 author J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143418702 -0500
3934 committer J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143418702 -0500
3936 add emphasis
3937 </literal>
3938 </programlisting>
3940 <para>The "tree" object here refers to the new state of the
3941 tree:</para>
3942 <programlisting>
3943 <literal>$ git ls-tree d0492b36
3944 100644 blob a0423896973644771497bdc03eb99d5281615b51    file.txt
3945 $ git cat-file blob a0423896
3946 hello world!
3947 </literal>
3948 </programlisting>
3950 <para>and the "parent" object refers to the previous commit:</para>
3951 <programlisting>
3952 <literal>$ git-cat-file commit 54196cc2
3953 tree 92b8b694ffb1675e5975148e1121810081dbdffe
3954 author J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
3955 committer J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
3956 </literal>
3957 </programlisting>
3959 </sect2><sect2 id="Git_Reference"><title>Git References</title>
3960 <para>Branches, remote-tracking branches, and tags are all
3961 references to commits. All references are named with a
3962 slash-separated path name starting with "refs"; the names we've
3963 been using so far are actually shorthand:</para>
3964 <programlisting>
3965 <literal>- The branch "test" is short for "refs/heads/test".
3966 - The tag "v2.6.18" is short for "refs/tags/v2.6.18".
3967 - "origin/master" is short for "refs/remotes/origin/master".
3968 </literal>
3969 </programlisting>
3971 <para>The full name is occasionally useful if, for example, there
3972 ever exists a tag and a branch with the same name.</para>
3973 <para>(Newly created refs are actually stored in the .git/refs
3974 directory, under the path given by their name. However, for
3975 efficiency reasons they may also be packed together in a single
3976 file; see
3977 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-pack-refs.html">git
3978 pack-refs</ulink>).</para>
3979 <para>As another useful shortcut, the "HEAD" of a repository can be
3980 referred to just using the name of that repository. So, for
3981 example, "origin" is usually a shortcut for the HEAD branch in the
3982 repository "origin".</para>
3983 <para>For the complete list of paths which git checks for
3984 references, and the order it uses to decide which to choose when
3985 there are multiple references with the same shorthand name, see the
3986 "SPECIFYING REVISIONS" section of
3987 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html">git
3988 rev-parse</ulink>.</para>
3989 <sect3 id="Showing_commits_unique_to_a_given_branc"><title>Showing
3990 commits unique to a given branch</title>
3991 <para>Suppose you would like to see all the commits reachable from
3992 the branch head named "master" but not from any other head in your
3993 repository.</para>
3994 <para>We can list all the heads in this repository with
3995 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-show-ref.html">git
3996 show-ref</ulink>:</para>
3997 <programlisting>
3998 <literal>$ git show-ref --heads
3999 bf62196b5e363d73353a9dcf094c59595f3153b7 refs/heads/core-tutorial
4000 db768d5504c1bb46f63ee9d6e1772bd047e05bf9 refs/heads/maint
4001 a07157ac624b2524a059a3414e99f6f44bebc1e7 refs/heads/master
4002 24dbc180ea14dc1aebe09f14c8ecf32010690627 refs/heads/tutorial-2
4003 1e87486ae06626c2f31eaa63d26fc0fd646c8af2 refs/heads/tutorial-fixes
4004 </literal>
4005 </programlisting>
4007 <para>We can get just the branch-head names, and remove "master",
4008 with the help of the standard utilities cut and grep:</para>
4009 <programlisting>
4010 <literal>$ git show-ref --heads | cut -d' ' -f2 | grep -v '^refs/heads/master'
4011 refs/heads/core-tutorial
4012 refs/heads/maint
4013 refs/heads/tutorial-2
4014 refs/heads/tutorial-fixes
4015 </literal>
4016 </programlisting>
4018 <para>And then we can ask to see all the commits reachable from
4019 master but not from these other heads:</para>
4020 <programlisting>
4021 <literal>$ gitk master --not $( git show-ref --heads | cut -d' ' -f2 |
4022                 grep -v '^refs/heads/master' )
4023 </literal>
4024 </programlisting>
4026 <para>Obviously, endless variations are possible; for example, to
4027 see all commits reachable from some head but not from any tag in
4028 the repository:</para>
4029 <programlisting>
4030 <literal>$ gitk $( git show-ref --heads ) --not  $( git show-ref --tags )
4031 </literal>
4032 </programlisting>
4034 <para>(See
4035 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html">git
4036 rev-parse</ulink> for explanations of commit-selecting syntax such
4037 as <literal>--not</literal>.)</para>
4038 <para>(!!update-ref!!)</para>
4039 </sect3></sect2><sect2 id="The_Git_Inde"><title>The Git
4040 Index</title>
4041 <para>The index is a binary file (generally kept in .git/index)
4042 containing a sorted list of path names, each with permissions and
4043 the SHA1 of a blob object;
4044 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-ls-files.html">git
4045 ls-files</ulink> can show you the contents of the index:</para>
4046 <programlisting>
4047 <literal>$ git ls-files --stage
4048 100644 63c918c667fa005ff12ad89437f2fdc80926e21c 0   .gitignore
4049 100644 5529b198e8d14decbe4ad99db3f7fb632de0439d 0   .mailmap
4050 100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0   COPYING
4051 100644 a37b2152bd26be2c2289e1f57a292534a51a93c7 0   Documentation/.gitignore
4052 100644 fbefe9a45b00a54b58d94d06eca48b03d40a50e0 0   Documentation/Makefile
4054 100644 2511aef8d89ab52be5ec6a5e46236b4b6bcd07ea 0   xdiff/xtypes.h
4055 100644 2ade97b2574a9f77e7ae4002a4e07a6a38e46d07 0   xdiff/xutils.c
4056 100644 d5de8292e05e7c36c4b68857c1cf9855e3d2f70a 0   xdiff/xutils.h
4057 </literal>
4058 </programlisting>
4060 <para>Note that in older documentation you may see the index called
4061 the "current directory cache" or just the "cache". It has three
4062 important properties:</para>
4063 <orderedlist>
4064 <listitem>
4065 <para>The index contains all the information necessary to generate
4066 a single (uniquely determined) tree object.</para>
4067 <para>For example, running
4068 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-commit.html">git
4069 commit</ulink> generates this tree object from the index, stores it
4070 in the object database, and uses it as the tree object associated
4071 with the new commit.</para>
4072 </listitem>
4073 <listitem>
4074 <para>The index enables fast comparisons between the tree object it
4075 defines and the working tree.</para>
4076 <para>It does this by storing some additional data for each entry
4077 (such as the last modified time). This data is not displayed above,
4078 and is not stored in the created tree object, but it can be used to
4079 determine quickly which files in the working directory differ from
4080 what was stored in the index, and thus save git from having to read
4081 all of the data from such files to look for changes.</para>
4082 </listitem>
4083 <listitem>
4084 <para>It can efficiently represent information about merge
4085 conflicts between different tree objects, allowing each pathname to
4086 be associated with sufficient information about the trees involved
4087 that you can create a three-way merge between them.</para>
4088 <para>During a merge, the index can store multiple versions of a
4089 single file (called "stages"). The third column in the
4090 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-ls-files.html">git
4091 ls-files</ulink> output above is the stage number, and will take on
4092 values other than 0 for files with merge conflicts.</para>
4093 </listitem>
4094 </orderedlist>
4096 <para>The index is thus a sort of temporary staging area, which is
4097 filled with a tree which you are in the process of working
4098 on.</para>
4099 </sect2><sect2 id="The_Packfil"><title>The Packfile</title>
4100 <para>This chapter explains in detail, down to the bits, how the
4101 packfile and pack index files are formatted.</para>
4102 <sect3 id="The_Packfile_Inde"><title>The Packfile Index</title>
4103 <para>First off, we have the packfile index, which is basically
4104 just a series of bookmarks into a packfile.</para>
4105 <para>There are two versions of the packfile index - version one,
4106 which is the default in versions of Git earlier than 1.6, and
4107 version two, which is the default from 1.6 forward, but which can
4108 be read by Git versions going back to 1.5.2, and has been further
4109 backported to 1.4.4.5 if you are still on the 1.4 series.</para>
4110 <para>Version 2 also includes a CRC checksum of each object so
4111 compressed data can be copied directly from pack to pack during
4112 repacking without undetected data corruption. Version 2 indexes can
4113 also handle packfiles larger than 4 Gb.</para>
4114 <inlinemediaobject><imageobject>
4115 <imagedata fileref="../images/packfile-index.png"/>
4116 </imageobject></inlinemediaobject>
4119 <para>In both formats, the fanout table is simply a way to find the
4120 offset of a particular sha faster within the index file. The
4121 offset/sha1[] tables are sorted by sha1[] values (this is to allow
4122 binary search of this table), and fanout[] table points at the
4123 offset/sha1[] table in a specific way (so that part of the latter
4124 table that covers all hashes that start with a given byte can be
4125 found to avoid 8 iterations of the binary search).</para>
4126 <para>In version 1, the offsets and shas are in the same space,
4127 where in version two, there are seperate tables for the shas, crc
4128 checksums and offsets. At the end of both files are checksum shas
4129 for both the index file and the packfile it references.</para>
4130 <para>Importantly, packfile indexes are <emphasis>not</emphasis>
4131 neccesary to extract objects from a packfile, they are simply used
4132 to <emphasis>quickly</emphasis> retrieve individual objects from a
4133 pack. The packfile format is used in upload-pack and receieve-pack
4134 programs (push and fetch protocols) to transfer objects and there
4135 is no index used then - it can be built after the fact by scanning
4136 the packfile.</para>
4137 </sect3><sect3 id="The_Packfile_Forma"><title>The Packfile
4138 Format</title>
4139 <para>The packfile itself is a very simple format. There is a
4140 header, a series of packed objects (each with it's own header and
4141 body) and then a checksum trailer. The first four bytes is the
4142 string 'PACK', which is sort of used to make sure you're getting
4143 the start of the packfile correctly. This is followed by a 4-byte
4144 packfile version number and then a 4-byte number of entries in that
4145 file. In Ruby, you might read the header data like this:</para>
4146 <programlisting>
4147 def read_pack_header
4148   sig = @session.recv(4)
4149   ver = @session.recv(4).unpack("N")[0]
4150   entries = @session.recv(4).unpack("N")[0]
4151   [sig, ver, entries]
4153 </programlisting>
4155 <para>After that, you get a series of packed objects, in order of
4156 thier SHAs which each consist of an object header and object
4157 contents. At the end of the packfile is a 20-byte SHA1 sum of all
4158 the shas (in sorted order) in that packfile.</para>
4159 <inlinemediaobject><imageobject>
4160 <imagedata fileref="../images/packfile-format.png"/>
4161 </imageobject></inlinemediaobject>
4164 <para>The object header is a series of one or more 1 byte (8 bit)
4165 hunks that specify the type of object the following data is, and
4166 the size of the data when expanded. Each byte is really 7 bits of
4167 data, with the first bit being used to say if that hunk is the last
4168 one or not before the data starts. If the first bit is a 1, you
4169 will read another byte, otherwise the data starts next. The first 3
4170 bits in the first byte specifies the type of data, according to the
4171 table below.</para>
4172 <para>(Currently, of the 8 values that can be expressed with 3 bits
4173 (0-7), 0 (000) is 'undefined' and 5 (101) is not yet used.)</para>
4174 <para>Here, we can see an example of a header of two bytes, where
4175 the first specifies that the following data is a commit, and the
4176 remainder of the first and the last 7 bits of the second specifies
4177 that the data will be 144 bytes when expanded.</para>
4178 <inlinemediaobject><imageobject>
4179 <imagedata fileref="../images/packfile-logic.png"/>
4180 </imageobject></inlinemediaobject>
4183 <para>It is important to note that the size specified in the header
4184 data is not the size of the data that actually follows, but the
4185 size of that data <emphasis>when expanded</emphasis>. This is why
4186 the offsets in the packfile index are so useful, otherwise you have
4187 to expand every object just to tell when the next header
4188 starts.</para>
4189 <para>The data part is just zlib stream for non-delta object types;
4190 for the two delta object representations, the data portion contains
4191 something that identifies which base object this delta
4192 representation depends on, and the delta to apply on the base
4193 object to resurrect this object. <literal>ref-delta</literal> uses
4194 20-byte hash of the base object at the beginning of data, while
4195 <literal>ofs-delta</literal> stores an offset within the same
4196 packfile to identify the base object. In either case, two important
4197 constraints a reimplementor must adhere to are:</para>
4198 <itemizedlist>
4199 <listitem>
4200 <para>delta representation must be based on some other object
4201 within the same packfile;</para>
4202 </listitem>
4203 <listitem>
4204 <para>the base object must be of the same underlying type (blob,
4205 tree, commit or tag);</para>
4206 </listitem>
4207 </itemizedlist>
4208 </sect3></sect2>
4209 <sect2 id="Raw_Gi"><title>Raw Git</title>
4210 <para>Here we will take a look at how to manipulate git at a more
4211 raw level, in case you would like to write a tool that generates
4212 new blobs, trees or commits in a more artificial way. If you want
4213 to write a script that uses more low-level git plumbing to do
4214 something new, here are some of the tools you'll need.</para>
4215 <sect3 id="Creating_Blob"><title>Creating Blobs</title>
4216 <para>Creating a blob in your Git repository and getting a SHA back
4217 is pretty easy. The
4218 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-hash-object.html">git
4219 hash-object</ulink> command is all you'll need. To create a blob
4220 object from an existing file, just run it with the '-w' option
4221 (which tells it to write the blob, not just compute the
4222 SHA).</para>
4223 <programlisting>
4224 <literal>$ git hash-object -w myfile.txt
4225 6ff87c4664981e4397625791c8ea3bbb5f2279a3
4227 $ git hash-object -w myfile2.txt
4228 3bb0e8592a41ae3185ee32266c860714980dbed7
4229 </literal>
4230 </programlisting>
4232 <para>The STDOUT output of the command will the the SHA of the blob
4233 that was created.</para>
4234 </sect3><sect3 id="Creating_Tree"><title>Creating Trees</title>
4235 <para>Now lets say you want to create a tree from your new objects.
4237 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-mktree.html">git
4238 mktree</ulink> command makes it pretty simple to generate new tree
4239 objects from
4240 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-ls-tree.html">git
4241 ls-tree</ulink> formatted output. For example, if you write the
4242 following to a file named '/tmp/tree.txt' :</para>
4243 <programlisting>
4244 <literal>100644 blob 6ff87c4664981e4397625791c8ea3bbb5f2279a3    file1
4245 100644 blob 3bb0e8592a41ae3185ee32266c860714980dbed7    file2
4246 </literal>
4247 </programlisting>
4249 <para>and then piped that through the
4250 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-mktree.html">git
4251 mktree</ulink> command, Git will write a new tree to the object
4252 database and give you back the new sha of that tree.</para>
4253 <programlisting>
4254 <literal>$ cat /tmp/tree.txt | git mk-tree
4255 f66a66ab6a7bfe86d52a66516ace212efa00fe1f
4256 </literal>
4257 </programlisting>
4259 <para>Then, we can take that and make it a subdirectory of yet
4260 another tree, and so on. If we wanted to create a new tree with
4261 that one as a subtree, we just create a new file (/tmp/newtree.txt)
4262 with our new SHA as a tree in it:</para>
4263 <programlisting>
4264 <literal>100644 blob 6ff87c4664981e4397625791c8ea3bbb5f2279a3    file1-copy
4265 040000 tree f66a66ab6a7bfe86d52a66516ace212efa00fe1f    our_files
4266 </literal>
4267 </programlisting>
4269 <para>and then use
4270 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-mk-tree.html">git
4271 mk-tree</ulink> again:</para>
4272 <programlisting>
4273 <literal>$ cat /tmp/newtree.txt | git mk-tree
4274 5bac6559179bd543a024d6d187692343e2d8ae83
4275 </literal>
4276 </programlisting>
4278 <para>And we now have an artificial directory structure in Git that
4279 looks like this:</para>
4280 <programlisting>
4281 <literal>.
4282 |-- file1-copy
4283 `-- our_files
4284     |-- file1
4285     `-- file2
4287 1 directory, 3 files
4288 </literal>
4289 </programlisting>
4291 <para>without that structure ever having actually existed on disk.
4292 Plus, we have a SHA (<literal>5bac6559</literal>) that points to
4293 it.</para>
4294 </sect3><sect3 id="Rearranging_Tree"><title>Rearranging
4295 Trees</title>
4296 <para>We can also do tree manipulation by combining trees into new
4297 structures using the index file. As a simple example, let's take
4298 the tree we just created and make a new tree that has two copies of
4299 our <literal>5bac6559</literal> tree in it using a temporary index
4300 file. (You can do this by resetting the GIT_INDEX_FILE environment
4301 variable or on the command line)</para>
4302 <para>First, we read the tree into our index file under a new
4303 prefix using the
4304 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html">git
4305 read-tree</ulink> command, and then write the index contents as a
4306 tree using the
4307 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-write-tree.html">git
4308 write-tree</ulink> command:</para>
4309 <programlisting>
4310 <literal>$ export GIT_INDEX_FILE=/tmp/index
4311 $ git read-tree --prefix=copy1/  5bac6559
4312 $ git read-tree --prefix=copy2/  5bac6559
4313 $ git write-tree 
4314 bb2fa6de7625322322382215d9ea78cfe76508c1
4316 $&gt;git ls-tree bb2fa
4317 040000 tree 5bac6559179bd543a024d6d187692343e2d8ae83    copy1
4318 040000 tree 5bac6559179bd543a024d6d187692343e2d8ae83    copy2
4319 </literal>
4320 </programlisting>
4322 <para>So now we can see that we've created a new tree just from
4323 index manipulation. You can also do interesting merge operations
4324 and such in a temporary index this way - see the
4325 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html">git
4326 read-tree</ulink> docs for more information.</para>
4327 </sect3><sect3 id="Creating_Commit"><title>Creating Commits</title>
4328 <para>Now that we have a tree SHA, we can create a commit object
4329 that points to it. We can do this using the
4330 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-commit-tree.html">git
4331 commit-tree</ulink> command. Most of the data that goes into the
4332 commit has to be set as environment variables, so you'll want to
4333 set the following:</para>
4334 <programlisting>
4335 <literal>GIT_AUTHOR_NAME
4336 GIT_AUTHOR_EMAIL
4337 GIT_AUTHOR_DATE
4338 GIT_COMMITTER_NAME
4339 GIT_COMMITTER_EMAIL
4340 GIT_COMMITTER_DATE
4341 </literal>
4342 </programlisting>
4344 <para>Then you will need to write your commit message to a file or
4345 somehow pipe it into the command through STDIN. Then, you can
4346 create your commit object based on the tree sha we have.</para>
4347 <programlisting>
4348 <literal>$ git commit-tree bb2fa &lt; /tmp/message
4349 a5f85ba5875917319471dfd98dfc636c1dc65650
4350 </literal>
4351 </programlisting>
4353 <para>If you want to specify one or more parent commits, simply add
4354 the shas on the command line with a '-p' option before each. The
4355 SHA of the new commit object will be returned via STDOUT.</para>
4356 </sect3><sect3 id="Updating_a_Branch_Re"><title>Updating a Branch
4357 Ref</title>
4358 <para>Now that we have a new commit object SHA, we can update a
4359 branch to point to it if we want to. Lets say we want to update our
4360 'master' branch to point to the new commit we just created - we
4361 would use the
4362 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-update-ref.html">git
4363 update-ref</ulink> command:</para>
4364 <programlisting>
4365 <literal>$ git update-ref refs/heads/master a5f85ba5875917319471dfd98dfc636c1dc65650
4366 </literal>
4367 </programlisting>
4369 </sect3></sect2><sect2 id="Transfer_Protocol"><title>Transfer
4370 Protocols</title>
4371 <para>Here we will go over how clients and servers talk to each
4372 other to transfer Git data around.</para>
4373 <sect3 id="Fetching_Data_over_HTT"><title>Fetching Data over
4374 HTTP</title>
4375 <para>Fetching over an http/s URL will make Git use a slightly
4376 dumber protocol. In this case, all of the logic is entirely on the
4377 client side. The server requires no special setup - any static
4378 webserver will work fine if the git directory you are fetching from
4379 is in the webserver path.</para>
4380 <para>In order for this to work, you do need to run a single
4381 command on the server repo everytime anything is updated, though -
4382 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-update-server-info.html">git
4383 update-server-info</ulink>, which updates the objects/info/packs
4384 and info/refs files to list which refs and packfiles are available,
4385 since you can't do a listing over http. When that command is run,
4386 the objects/info/packs file looks something like this:</para>
4387 <programlisting>
4388 <literal>P pack-ce2bd34abc3d8ebc5922dc81b2e1f30bf17c10cc.pack
4389 P pack-7ad5f5d05f5e20025898c95296fe4b9c861246d8.pack
4390 </literal>
4391 </programlisting>
4393 <para>So that if the fetch can't find a loose file, it can try
4394 these packfiles. The info/refs file will look something like
4395 this:</para>
4396 <programlisting>
4397 <literal>184063c9b594f8968d61a686b2f6052779551613    refs/heads/development
4398 32aae7aef7a412d62192f710f2130302997ec883    refs/heads/master
4399 </literal>
4400 </programlisting>
4402 <para>Then when you fetch from this repo, it will start with these
4403 refs and walk the commit objects until the client has all the
4404 objects that it needs.</para>
4405 <para>For instance, if you ask to fetch the master branch, it will
4406 see that master is pointing to <literal>32aae7ae</literal> and that
4407 your master is pointing to <literal>ab04d88</literal>, so you need
4408 <literal>32aae7ae</literal>. You fetch that object</para>
4409 <programlisting>
4410 <literal>CONNECT http://myserver.com
4411 GET /git/myproject.git/objects/32/aae7aef7a412d62192f710f2130302997ec883 - 200
4412 </literal>
4413 </programlisting>
4415 <para>and it looks like this:</para>
4416 <programlisting>
4417 <literal>tree aa176fb83a47d00386be237b450fb9dfb5be251a
4418 parent bd71cad2d597d0f1827d4a3f67bb96a646f02889
4419 author Scott Chacon &lt;schacon@gmail.com&gt; 1220463037 -0700
4420 committer Scott Chacon &lt;schacon@gmail.com&gt; 1220463037 -0700
4422 added chapters on private repo setup, scm migration, raw git
4423 </literal>
4424 </programlisting>
4426 <para>So now it fetches the tree
4427 <literal>aa176fb8</literal>:</para>
4428 <programlisting>
4429 <literal>GET /git/myproject.git/objects/aa/176fb83a47d00386be237b450fb9dfb5be251a - 200
4430 </literal>
4431 </programlisting>
4433 <para>which looks like this:</para>
4434 <programlisting>
4435 <literal>100644 blob 6ff87c4664981e4397625791c8ea3bbb5f2279a3    COPYING
4436 100644 blob 97b51a6d3685b093cfb345c9e79516e5099a13fb    README
4437 100644 blob 9d1b23b8660817e4a74006f15fae86e2a508c573    Rakefile
4438 </literal>
4439 </programlisting>
4441 <para>So then it fetches those objects:</para>
4442 <programlisting>
4443 <literal>GET /git/myproject.git/objects/6f/f87c4664981e4397625791c8ea3bbb5f2279a3 - 200
4444 GET /git/myproject.git/objects/97/b51a6d3685b093cfb345c9e79516e5099a13fb - 200
4445 GET /git/myproject.git/objects/9d/1b23b8660817e4a74006f15fae86e2a508c573 - 200
4446 </literal>
4447 </programlisting>
4449 <para>It actually does this with Curl, and can open up multiple
4450 parallel threads to speed up this process. When it's done recursing
4451 the tree pointed to by the commit, it fetches the next
4452 parent.</para>
4453 <programlisting>
4454 <literal>GET /git/myproject.git/objects/bd/71cad2d597d0f1827d4a3f67bb96a646f02889 - 200
4455 </literal>
4456 </programlisting>
4458 <para>Now in this case, the commit that comes back looks like
4459 this:</para>
4460 <programlisting>
4461 <literal>tree b4cc00cf8546edd4fcf29defc3aec14de53e6cf8
4462 parent ab04d884140f7b0cf8bbf86d6883869f16a46f65
4463 author Scott Chacon &lt;schacon@gmail.com&gt; 1220421161 -0700
4464 committer Scott Chacon &lt;schacon@gmail.com&gt; 1220421161 -0700
4466 added chapters on the packfile and how git stores objects
4467 </literal>
4468 </programlisting>
4470 <para>and we can see that the parent, <literal>ab04d88</literal> is
4471 where our master branch is currently pointing. So, we recursively
4472 fetch this tree and then stop, since we know we have everything
4473 before this point. You can force Git to double check that we have
4474 everything with the '--recover' option. See
4475 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-http-fetch.html">git
4476 http-fetch</ulink> for more information.</para>
4477 <para>If one of the loose object fetches fails, Git will download
4478 the packfile indexes looking for the sha that it needs, then
4479 download that packfile.</para>
4480 <para>It is important if you are running a git server that serves
4481 repos this way to implement a post-receive hook that runs the 'git
4482 update-server-info' command each time or there will be
4483 confusion.</para>
4484 </sect3><sect3 id="Fetching_Data_with_Upload_Pac"><title>Fetching
4485 Data with Upload Pack</title>
4486 <para>For the smarter protocols, fetching objects is much more
4487 efficient. A socket is opened, either over ssh or over port 9418
4488 (in the case of the git:// protocol), and the
4489 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-fetch-pack.html">git
4490 fetch-pack</ulink> command on the client begins communicating with
4491 a forked
4492 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-upload-pack.html">git
4493 upload-pack</ulink> process on the server.</para>
4494 <para>Then the server will tell the client which SHAs it has for
4495 each ref, and the client figures out what it needs and responds
4496 with a list of SHAs it wants and already has.</para>
4497 <para>At this point, the server will generate a packfile with all
4498 the objects that the client needs and begin streaming it down to
4499 the client.</para>
4500 <para>Let's look at an example.</para>
4501 <para>The client connects and sends the request header. The clone
4502 command</para>
4503 <programlisting>
4504 <literal>$ git clone git://myserver.com/project.git
4505 </literal>
4506 </programlisting>
4508 <para>produces the following request:</para>
4509 <programlisting>
4510 <literal>0032git-upload-pack /project.git\000host=myserver.com\000
4511 </literal>
4512 </programlisting>
4514 <para>The first four bytes contain the hex length of the line
4515 (including 4 byte line length and trailing newline if present).
4516 Following are the command and arguments. This is followed by a null
4517 byte and then the host information. The request is terminated by a
4518 null byte.</para>
4519 <para>The request is processed and turned into a call to
4520 git-upload-pack:</para>
4521 <programlisting>
4522 <literal>$ git-upload-pack /path/to/repos/project.git
4523 </literal>
4524 </programlisting>
4526 <para>This immediately returns information of the repo:</para>
4527 <programlisting>
4528 <literal>007c74730d410fcb6603ace96f1dc55ea6196122532d HEAD\000multi_ack thin-pack side-band side-band-64k ofs-delta shallow no-progress
4529 003e7d1665144a3a975c05f1f43902ddaf084e784dbe refs/heads/debug
4530 003d5a3f6be755bbb7deae50065988cbfa1ffa9ab68a refs/heads/dist
4531 003e7e47fe2bd8d01d481f44d7af0531bd93d3b21c01 refs/heads/local
4532 003f74730d410fcb6603ace96f1dc55ea6196122532d refs/heads/master
4533 0000
4534 </literal>
4535 </programlisting>
4537 <para>Each line starts with a four byte line length declaration in
4538 hex. The section is terminated by a line length declaration of
4539 0000.</para>
4540 <para>This is sent back to the client verbatim. The client responds
4541 with another request:</para>
4542 <programlisting>
4543 <literal>0054want 74730d410fcb6603ace96f1dc55ea6196122532d multi_ack side-band-64k ofs-delta
4544 0032want 7d1665144a3a975c05f1f43902ddaf084e784dbe
4545 0032want 5a3f6be755bbb7deae50065988cbfa1ffa9ab68a
4546 0032want 7e47fe2bd8d01d481f44d7af0531bd93d3b21c01
4547 0032want 74730d410fcb6603ace96f1dc55ea6196122532d
4548 00000009done
4549 </literal>
4550 </programlisting>
4552 <para>The is sent to the open git-upload-pack process which then
4553 streams out the final response:</para>
4554 <programlisting>
4555 <literal>"0008NAK\n"
4556 "0023\002Counting objects: 2797, done.\n"
4557 "002b\002Compressing objects:   0% (1/1177)   \r"
4558 "002c\002Compressing objects:   1% (12/1177)   \r"
4559 "002c\002Compressing objects:   2% (24/1177)   \r"
4560 "002c\002Compressing objects:   3% (36/1177)   \r"
4561 "002c\002Compressing objects:   4% (48/1177)   \r"
4562 "002c\002Compressing objects:   5% (59/1177)   \r"
4563 "002c\002Compressing objects:   6% (71/1177)   \r"
4564 "0053\002Compressing objects:   7% (83/1177)   \rCompressing objects:   8% (95/1177)   \r"
4566 "005b\002Compressing objects: 100% (1177/1177)   \rCompressing objects: 100% (1177/1177), done.\n"
4567 "2004\001PACK\000\000\000\002\000\000\n\355\225\017x\234\235\216K\n\302"...
4568 "2005\001\360\204{\225\376\330\345]z2673"...
4570 "0037\002Total 2797 (delta 1799), reused 2360 (delta 1529)\n"
4572 "&lt;\276\255L\273s\005\001w0006\001[0000"
4573 </literal>
4574 </programlisting>
4576 <para>See the Packfile chapter previously for the actual format of
4577 the packfile data in the response.</para>
4578 </sect3><sect3 id="Pushing_Dat"><title>Pushing Data</title>
4579 <para>Pushing data over the git and ssh protocols is similar, but
4580 simpler. Basically what happens is the client requests a
4581 receive-pack instance, which is started up if the client has
4582 access, then the server returns all the ref head shas it has again
4583 and the client generates a packfile of everything the server needs
4584 (generally only if what is on the server is a direct ancestor of
4585 what it is pushing) and sends that packfile upstream, where the
4586 server either stores it on disk and builds an index for it, or
4587 unpacks it (if there aren't many objects in it)</para>
4588 <para>This entire process is accomplished through the
4589 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-send-pack.html">git
4590 send-pack</ulink> command on the client, which is invoked by
4591 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-push.html">git
4592 push</ulink> and the
4593 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-receive-pack.html">git
4594 receive-pack</ulink> command on the server side, which is invoked
4595 by the ssh connect process or git daemon (if it's an open push
4596 server).</para>
4597 </sect3></sect2><sect2 id="Glossar"><title>Glossary</title>
4598 <para>Here we have the meanings of some terms used into Git
4599 context.These terms were entirely copied from
4600 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/gitglossary.html">Git
4601 Glossary</ulink>.</para>
4602 <para><emphasis>alternate object database</emphasis></para>
4603 <blockquote>
4604 <para>Via the alternates mechanism, a repository</para>
4605 <programlisting>
4606 <literal>can inherit part of its object database
4607 from another object database, which is called "alternate". 
4608 </literal>
4609 </programlisting>
4610 </blockquote>
4611 <para><emphasis>bare repository</emphasis></para>
4612 <blockquote>
4613 <para>A bare repository is normally an appropriately</para>
4614 <programlisting>
4615 <literal>named directory with a `.git` suffix that does not
4616 have a locally checked-out copy of any of the files under
4617 revision control. That is, all of the `git`
4618 administrative and control files that would normally be present in the
4619 hidden `.git` sub-directory are directly present in the
4620 `repository.git` directory instead,
4621 and no other files are present and checked out. Usually publishers of
4622 public repositories make bare repositories available.
4623 </literal>
4624 </programlisting>
4625 </blockquote>
4626 <para><emphasis>blob object</emphasis></para>
4627 <blockquote>
4628 <para>Untyped object, e.g. the contents of a file.</para>
4629 </blockquote>
4630 <para><emphasis>branch</emphasis></para>
4631 <blockquote>
4632 <para>A "branch" is an active line of development. The most
4633 recent</para>
4634 <programlisting>
4635 <literal>commit on a branch is referred to as the tip of
4636 that branch.  The tip of the branch is referenced by a branch
4637 head, which moves forward as additional development
4638 is done on the branch.  A single git
4639 repository can track an arbitrary number of
4640 branches, but your working tree is
4641 associated with just one of them (the "current" or "checked out"
4642 branch), and HEAD points to that branch.
4643 </literal>
4644 </programlisting>
4645 </blockquote>
4646 <para><emphasis>cache</emphasis></para>
4647 <blockquote>
4648 <para>Obsolete for: index.</para>
4649 </blockquote>
4650 <para><emphasis>chain</emphasis></para>
4651 <blockquote>
4652 <para>A list of objects, where each object in the list
4653 contains</para>
4654 <programlisting>
4655 <literal>a reference to its successor (for example, the successor of a
4656 commit could be one of its parents).
4657 </literal>
4658 </programlisting>
4659 </blockquote>
4660 <para><emphasis>changeset</emphasis></para>
4661 <blockquote>
4662 <para>BitKeeper/cvsps speak for "commit". Since git does not</para>
4663 <programlisting>
4664 <literal>store changes, but states, it really does not make sense to use the term
4665 "changesets" with git.
4666 </literal>
4667 </programlisting>
4668 </blockquote>
4669 <para><emphasis>checkout</emphasis></para>
4670 <blockquote>
4671 <para>The action of updating all or part of the</para>
4672 <programlisting>
4673 <literal>working tree with a tree object
4674 or blob from the
4675 object database, and updating the
4676 index and HEAD if the whole working tree has
4677 been pointed at a new branch.
4678 </literal>
4679 </programlisting>
4680 </blockquote>
4681 <para><emphasis>cherry-picking</emphasis></para>
4682 <blockquote>
4683 <para>In SCM jargon, "cherry pick" means to choose a subset
4684 of</para>
4685 <programlisting>
4686 <literal>changes out of a series of changes (typically commits) and record them
4687 as a new series of changes on top of a different codebase. In GIT, this is
4688 performed by the "git cherry-pick" command to extract the change introduced
4689 by an existing commit and to record it based on the tip
4690 of the current branch as a new commit.
4691 </literal>
4692 </programlisting>
4693 </blockquote>
4694 <para><emphasis>clean</emphasis></para>
4695 <blockquote>
4696 <para>A working tree is clean, if it</para>
4697 <programlisting>
4698 <literal>corresponds to the revision referenced by the current
4699 head. Also see "dirty".
4700 </literal>
4701 </programlisting>
4702 </blockquote>
4703 <para><emphasis>commit</emphasis></para>
4704 <blockquote>
4705 <para>As a noun: A single point in the</para>
4706 <programlisting>
4707 <literal>git history; the entire history of a project is represented as a
4708 set of interrelated commits.  The word "commit" is often
4709 used by git in the same places other revision control systems
4710 use the words "revision" or "version".  Also used as a short
4711 hand for commit object.
4712 </literal>
4713 </programlisting>
4715 <para>As a verb: The action of storing a new snapshot of the
4716 project's</para>
4717 <programlisting>
4718 <literal>state in the git history, by creating a new commit representing the current
4719 state of the index and advancing HEAD
4720 to point at the new commit.
4721 </literal>
4722 </programlisting>
4723 </blockquote>
4724 <para><emphasis>commit object</emphasis></para>
4725 <blockquote>
4726 <para>An object which contains the information about a</para>
4727 <programlisting>
4728 <literal>particular revision, such as parents, committer,
4729 author, date and the tree object which corresponds
4730 to the top directory of the stored
4731 revision.
4732 </literal>
4733 </programlisting>
4734 </blockquote>
4735 <para><emphasis>core git</emphasis></para>
4736 <blockquote>
4737 <para>Fundamental data structures and utilities of git. Exposes
4738 only limited</para>
4739 <programlisting>
4740 <literal>source code management tools.
4741 </literal>
4742 </programlisting>
4743 </blockquote>
4744 <para><emphasis>DAG</emphasis></para>
4745 <blockquote>
4746 <para>Directed acyclic graph. The commit objects form a</para>
4747 <programlisting>
4748 <literal>directed acyclic graph, because they have parents (directed), and the
4749 graph of commit objects is acyclic (there is no chain
4750 which begins and ends with the same object).
4751 </literal>
4752 </programlisting>
4753 </blockquote>
4754 <para><emphasis>dangling object</emphasis></para>
4755 <blockquote>
4756 <para>An unreachable object which is not</para>
4757 <programlisting>
4758 <literal>reachable even from other unreachable objects; a
4759 dangling object has no references to it from any
4760 reference or object in the repository.
4761 </literal>
4762 </programlisting>
4763 </blockquote>
4764 <para><emphasis>detached HEAD</emphasis></para>
4765 <blockquote>
4766 <para>Normally the HEAD stores the name of a</para>
4767 <programlisting>
4768 <literal>branch.  However, git also allows you to check out
4769 an arbitrary commit that isn't necessarily the tip of any
4770 particular branch.  In this case HEAD is said to be "detached".
4771 </literal>
4772 </programlisting>
4773 </blockquote>
4774 <para><emphasis>dircache</emphasis></para>
4775 <blockquote>
4776 <para>You are <emphasis>waaaaay</emphasis> behind. See
4777 index.</para>
4778 </blockquote>
4779 <para><emphasis>directory</emphasis></para>
4780 <blockquote>
4781 <para>The list you get with "ls" :-)</para>
4782 </blockquote>
4783 <para><emphasis>dirty</emphasis></para>
4784 <blockquote>
4785 <para>A working tree is said to be "dirty" if</para>
4786 <programlisting>
4787 <literal>it contains modifications which have not been committed to the current
4788 branch.
4789 </literal>
4790 </programlisting>
4791 </blockquote>
4792 <para><emphasis>ent</emphasis></para>
4793 <blockquote>
4794 <para>Favorite synonym to "tree-ish" by some total geeks.
4795 See</para>
4796 <programlisting>
4797 <literal>`http://en.wikipedia.org/wiki/Ent_(Middle-earth)` for an in-depth
4798 explanation. Avoid this term, not to confuse people.
4799 </literal>
4800 </programlisting>
4801 </blockquote>
4802 <para><emphasis>evil merge</emphasis></para>
4803 <blockquote>
4804 <para>An evil merge is a merge that introduces changes that</para>
4805 <programlisting>
4806 <literal>do not appear in any parent.
4807 </literal>
4808 </programlisting>
4809 </blockquote>
4810 <para><emphasis>fast forward</emphasis></para>
4811 <blockquote>
4812 <para>A fast-forward is a special type of merge where you have
4813 a</para>
4814 <programlisting>
4815 <literal>revision and you are "merging" another
4816 branch's changes that happen to be a descendant of what
4817 you have. In such these cases, you do not make a new merge
4818 commit but instead just update to his
4819 revision. This will happen frequently on a
4820 tracking branch of a remote
4821 repository.
4822 </literal>
4823 </programlisting>
4824 </blockquote>
4825 <para><emphasis>fetch</emphasis></para>
4826 <blockquote>
4827 <para>Fetching a branch means to get the</para>
4828 <programlisting>
4829 <literal>branch's head ref from a remote
4830 repository, to find out which objects are
4831 missing from the local object database,
4832 and to get them, too.  See also <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html">git fetch</ulink>.
4833 </literal>
4834 </programlisting>
4835 </blockquote>
4836 <para><emphasis>file system</emphasis></para>
4837 <blockquote>
4838 <para>Linus Torvalds originally designed git to be a user space
4839 file system,</para>
4840 <programlisting>
4841 <literal>i.e. the infrastructure to hold files and directories. That ensured the
4842 efficiency and speed of git.
4843 </literal>
4844 </programlisting>
4845 </blockquote>
4846 <para><emphasis>git archive</emphasis></para>
4847 <blockquote>
4848 <para>Synonym for repository (for arch people).</para>
4849 </blockquote>
4850 <para><emphasis>grafts</emphasis></para>
4851 <blockquote>
4852 <para>Grafts enables two otherwise different lines of development
4853 to be joined</para>
4854 <programlisting>
4855 <literal>together by recording fake ancestry information for commits. This way
4856 you can make git pretend the set of parents a commit has
4857 is different from what was recorded when the commit was
4858 created. Configured via the `.git/info/grafts` file.
4859 </literal>
4860 </programlisting>
4861 </blockquote>
4862 <para><emphasis>hash</emphasis></para>
4863 <blockquote>
4864 <para>In git's context, synonym to object name.</para>
4865 </blockquote>
4866 <para><emphasis>head</emphasis></para>
4867 <blockquote>
4868 <para>A named reference to the commit at the tip of a</para>
4869 <programlisting>
4870 <literal>branch.  Heads are stored in
4871 `$GIT_DIR/refs/heads/`, except when using packed refs. (See
4872 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-pack-refs.html">git pack-refs</ulink>.)
4873 </literal>
4874 </programlisting>
4875 </blockquote>
4876 <para><emphasis>HEAD</emphasis></para>
4877 <blockquote>
4878 <para>The current branch. In more detail: Your working tree is
4879 normally derived</para>
4880 <programlisting>
4881 <literal>from the state of the tree referred to by HEAD.  HEAD is a reference to one
4882 of the heads in your repository, except when using a detached HEAD, in which
4883 case it may reference an arbitrary commit.
4884 </literal>
4885 </programlisting>
4886 </blockquote>
4887 <para><emphasis>head ref</emphasis></para>
4888 <blockquote>
4889 <para>A synonym for head.</para>
4890 </blockquote>
4891 <para><emphasis>hook</emphasis></para>
4892 <blockquote>
4893 <para>During the normal execution of several git commands,
4894 call-outs are made</para>
4895 <programlisting>
4896 <literal>to optional scripts that allow a developer to add functionality or
4897 checking. Typically, the hooks allow for a command to be pre-verified
4898 and potentially aborted, and allow for a post-notification after the
4899 operation is done. The hook scripts are found in the
4900 `$GIT_DIR/hooks/` directory, and are enabled by simply
4901 removing the `.sample` suffix from the filename. In earlier versions
4902 of git you had to make them executable.
4903 </literal>
4904 </programlisting>
4905 </blockquote>
4906 <para><emphasis>index</emphasis></para>
4907 <blockquote>
4908 <para>A collection of files with stat information, whose contents
4909 are stored</para>
4910 <programlisting>
4911 <literal>as objects. The index is a stored version of your
4912 working tree. Truth be told, it can also contain a second, and even
4913 a third version of a working tree, which are used
4914 when merging.
4915 </literal>
4916 </programlisting>
4917 </blockquote>
4918 <para><emphasis>index entry</emphasis></para>
4919 <blockquote>
4920 <para>The information regarding a particular file, stored in
4921 the</para>
4922 <programlisting>
4923 <literal>index. An index entry can be unmerged, if a
4924 merge was started, but not yet finished (i.e. if
4925 the index contains multiple versions of that file).
4926 </literal>
4927 </programlisting>
4928 </blockquote>
4929 <para><emphasis>master</emphasis></para>
4930 <blockquote>
4931 <para>The default development branch. Whenever you</para>
4932 <programlisting>
4933 <literal>create a git repository, a branch named
4934 "master" is created, and becomes the active branch. In most
4935 cases, this contains the local development, though that is
4936 purely by convention and is not required.
4937 </literal>
4938 </programlisting>
4939 </blockquote>
4940 <para><emphasis>merge</emphasis></para>
4941 <blockquote>
4942 <para>As a verb: To bring the contents of another</para>
4943 <programlisting>
4944 <literal>branch (possibly from an external
4945 repository) into the current branch.  In the
4946 case where the merged-in branch is from a different repository,
4947 this is done by first fetching the remote branch
4948 and then merging the result into the current branch.  This
4949 combination of fetch and merge operations is called a
4950 pull.  Merging is performed by an automatic process
4951 that identifies changes made since the branches diverged, and
4952 then applies all those changes together.  In cases where changes
4953 conflict, manual intervention may be required to complete the
4954 merge.
4955 </literal>
4956 </programlisting>
4958 <para>As a noun: unless it is a fast forward, a</para>
4959 <programlisting>
4960 <literal>successful merge results in the creation of a new commit
4961 representing the result of the merge, and having as
4962 parents the tips of the merged branches.
4963 This commit is referred to as a "merge commit", or sometimes just a
4964 "merge".
4965 </literal>
4966 </programlisting>
4967 </blockquote>
4968 <para><emphasis>object</emphasis></para>
4969 <blockquote>
4970 <para>The unit of storage in git. It is uniquely identified by
4971 the</para>
4972 <programlisting>
4973 <literal>SHA1&gt; of its contents. Consequently, an
4974 object can not be changed.
4975 </literal>
4976 </programlisting>
4977 </blockquote>
4978 <para><emphasis>object database</emphasis></para>
4979 <blockquote>
4980 <para>Stores a set of "objects", and an individual object is</para>
4981 <programlisting>
4982 <literal>identified by its object name. The objects usually
4983 live in `$GIT_DIR/objects/`.
4984 </literal>
4985 </programlisting>
4986 </blockquote>
4987 <para><emphasis>object identifier</emphasis></para>
4988 <blockquote>
4989 <para>Synonym for object name.</para>
4990 </blockquote>
4991 <para><emphasis>object name</emphasis></para>
4992 <blockquote>
4993 <para>The unique identifier of an object. The hash</para>
4994 <programlisting>
4995 <literal>of the object's contents using the Secure Hash Algorithm
4996 1 and usually represented by the 40 character hexadecimal encoding of
4997 the hash of the object.
4998 </literal>
4999 </programlisting>
5000 </blockquote>
5001 <para><emphasis>object type</emphasis></para>
5002 <blockquote>
5003 <para>One of the identifiers "commit", "tree", "tag" or "blob"
5004 describing the</para>
5005 <programlisting>
5006 <literal>type of an object.
5007 </literal>
5008 </programlisting>
5009 </blockquote>
5010 <para><emphasis>octopus</emphasis></para>
5011 <blockquote>
5012 <para>To merge more than two branches. Also denotes an</para>
5013 <programlisting>
5014 <literal>intelligent predator.
5015 </literal>
5016 </programlisting>
5017 </blockquote>
5018 <para><emphasis>origin</emphasis></para>
5019 <blockquote>
5020 <para>The default upstream repository. Most projects have</para>
5021 <programlisting>
5022 <literal>at least one upstream project which they track. By default
5023 'origin' is used for that purpose. New upstream updates
5024 will be fetched into remote tracking branches named
5025 origin/name-of-upstream-branch, which you can see using
5026 "`git branch -r`".
5027 </literal>
5028 </programlisting>
5029 </blockquote>
5030 <para><emphasis>pack</emphasis></para>
5031 <blockquote>
5032 <para>A set of objects which have been compressed into one file (to
5033 save space</para>
5034 <programlisting>
5035 <literal>or to transmit them efficiently).
5036 </literal>
5037 </programlisting>
5038 </blockquote>
5039 <para><emphasis>pack index</emphasis></para>
5040 <blockquote>
5041 <para>The list of identifiers, and other information, of the
5042 objects in a</para>
5043 <programlisting>
5044 <literal>pack, to assist in efficiently accessing the contents of a
5045 pack.
5046 </literal>
5047 </programlisting>
5048 </blockquote>
5049 <para><emphasis>parent</emphasis></para>
5050 <blockquote>
5051 <para>A commit object contains a (possibly empty) list</para>
5052 <programlisting>
5053 <literal>of the logical predecessor(s) in the line of development, i.e. its
5054 parents.
5055 </literal>
5056 </programlisting>
5057 </blockquote>
5058 <para><emphasis>pickaxe</emphasis></para>
5059 <blockquote>
5060 <para>The term pickaxe refers to an option to the diffcore</para>
5061 <programlisting>
5062 <literal>routines that help select changes that add or delete a given text
5063 string. With the `--pickaxe-all` option, it can be used to view the full
5064 changeset that introduced or removed, say, a
5065 particular line of text. See <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-diff.html">git diff</ulink>.
5066 </literal>
5067 </programlisting>
5068 </blockquote>
5069 <para><emphasis>plumbing</emphasis></para>
5070 <blockquote>
5071 <para>Cute name for core git.</para>
5072 </blockquote>
5073 <para><emphasis>porcelain</emphasis></para>
5074 <blockquote>
5075 <para>Cute name for programs and program suites depending on</para>
5076 <programlisting>
5077 <literal>core git, presenting a high level access to
5078 core git. Porcelains expose more of a SCM
5079 interface than the plumbing.
5080 </literal>
5081 </programlisting>
5082 </blockquote>
5083 <para><emphasis>pull</emphasis></para>
5084 <blockquote>
5085 <para>Pulling a branch means to fetch it and</para>
5086 <programlisting>
5087 <literal>merge it.  See also <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-pull.html">git pull</ulink>.
5088 </literal>
5089 </programlisting>
5090 </blockquote>
5091 <para><emphasis>push</emphasis></para>
5092 <blockquote>
5093 <para>Pushing a branch means to get the branch's</para>
5094 <programlisting>
5095 <literal>head ref from a remote repository,
5096 find out if it is a direct ancestor to the branch's local
5097 head ref, and in that case, putting all
5098 objects, which are reachable from the local
5099 head ref, and which are missing from the remote
5100 repository, into the remote
5101 object database, and updating the remote
5102 head ref. If the remote head is not an
5103 ancestor to the local head, the push fails.
5104 </literal>
5105 </programlisting>
5106 </blockquote>
5107 <para><emphasis>reachable</emphasis></para>
5108 <blockquote>
5109 <para>All of the ancestors of a given commit are said to be</para>
5110 <programlisting>
5111 <literal>"reachable" from that commit. More
5112 generally, one object is reachable from
5113 another if we can reach the one from the other by a chain
5114 that follows tags to whatever they tag,
5115 commits to their parents or trees, and
5116 trees to the trees or blobs
5117 that they contain.
5118 </literal>
5119 </programlisting>
5120 </blockquote>
5121 <para><emphasis>rebase</emphasis></para>
5122 <blockquote>
5123 <para>To reapply a series of changes from a branch to a</para>
5124 <programlisting>
5125 <literal>different base, and reset the head of that branch
5126 to the result.
5127 </literal>
5128 </programlisting>
5129 </blockquote>
5130 <para><emphasis>ref</emphasis></para>
5131 <blockquote>
5132 <para>A 40-byte hex representation of a SHA1 or a name that</para>
5133 <programlisting>
5134 <literal>denotes a particular object. These may be stored in
5135 `$GIT_DIR/refs/`.
5136 </literal>
5137 </programlisting>
5138 </blockquote>
5139 <para><emphasis>reflog</emphasis></para>
5140 <blockquote>
5141 <para>A reflog shows the local "history" of a ref. In other
5142 words,</para>
5143 <programlisting>
5144 <literal>it can tell you what the 3rd last revision in _this_ repository
5145 was, and what was the current state in _this_ repository,
5146 yesterday 9:14pm.  See <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-reflog.html">git reflog</ulink> for details.
5147 </literal>
5148 </programlisting>
5149 </blockquote>
5150 <para><emphasis>refspec</emphasis></para>
5151 <blockquote>
5152 <para>A "refspec" is used by fetch and</para>
5153 <programlisting>
5154 <literal>push to describe the mapping between remote
5155 ref and local ref. They are combined with a colon in
5156 the format &lt;src&gt;:&lt;dst&gt;, preceded by an optional plus sign, +.
5157 For example: `git fetch $URL
5158 refs/heads/master:refs/heads/origin` means "grab the master
5159 branch head from the $URL and store
5160 it as my origin branch head". And `git push
5161 $URL refs/heads/master:refs/heads/to-upstream` means "publish my
5162 master branch head as to-upstream branch at $URL". See also
5163 <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-push.html">git push</ulink>.
5164 </literal>
5165 </programlisting>
5166 </blockquote>
5167 <para><emphasis>repository</emphasis></para>
5168 <blockquote>
5169 <para>A collection of refs together with an</para>
5170 <programlisting>
5171 <literal>object database containing all objects
5172 which are reachable from the refs, possibly
5173 accompanied by meta data from one or more porcelains. A
5174 repository can share an object database with other repositories
5175 via alternates mechanism.
5176 </literal>
5177 </programlisting>
5178 </blockquote>
5179 <para><emphasis>resolve</emphasis></para>
5180 <blockquote>
5181 <para>The action of fixing up manually what a failed
5182 automatic</para>
5183 <programlisting>
5184 <literal>merge left behind.
5185 </literal>
5186 </programlisting>
5187 </blockquote>
5188 <para><emphasis>revision</emphasis></para>
5189 <blockquote>
5190 <para>A particular state of files and directories which was stored
5191 in the</para>
5192 <programlisting>
5193 <literal>object database. It is referenced by a
5194 commit object.
5195 </literal>
5196 </programlisting>
5197 </blockquote>
5198 <para><emphasis>rewind</emphasis></para>
5199 <blockquote>
5200 <para>To throw away part of the development, i.e. to assign
5201 the</para>
5202 <programlisting>
5203 <literal>head to an earlier revision.
5204 </literal>
5205 </programlisting>
5206 </blockquote>
5207 <para><emphasis>SCM</emphasis></para>
5208 <blockquote>
5209 <para>Source code management (tool).</para>
5210 </blockquote>
5211 <para><emphasis>SHA1</emphasis></para>
5212 <blockquote>
5213 <para>Synonym for object name.</para>
5214 </blockquote>
5215 <para><emphasis>shallow repository</emphasis></para>
5216 <blockquote>
5217 <para>A shallow repository has an incomplete</para>
5218 <programlisting>
5219 <literal>history some of whose commits have parents cauterized away (in other
5220 words, git is told to pretend that these commits do not have the
5221 parents, even though they are recorded in the commit
5222 object). This is sometimes useful when you are interested only in the
5223 recent history of a project even though the real history recorded in the
5224 upstream is much larger. A shallow repository
5225 is created by giving the `--depth` option to <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html">git clone</ulink>, and
5226 its history can be later deepened with <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html">git fetch</ulink>.
5227 </literal>
5228 </programlisting>
5229 </blockquote>
5230 <para><emphasis>symref</emphasis></para>
5231 <blockquote>
5232 <para>Symbolic reference: instead of containing the SHA1</para>
5233 <programlisting>
5234 <literal>id itself, it is of the format 'ref: refs/some/thing' and when
5235 referenced, it recursively dereferences to this reference.
5236 'HEAD' is a prime example of a symref. Symbolic
5237 references are manipulated with the <ulink url="http://www.kernel.org/pub/software/scm/git/docs/git-symbolic-ref.html">git symbolic-ref</ulink>
5238 command.
5239 </literal>
5240 </programlisting>
5241 </blockquote>
5242 <para><emphasis>tag</emphasis></para>
5243 <blockquote>
5244 <para>A ref pointing to a tag or</para>
5245 <programlisting>
5246 <literal>commit object. In contrast to a head,
5247 a tag is not changed by a commit. Tags (not
5248 tag objects) are stored in `$GIT_DIR/refs/tags/`. A
5249 git tag has nothing to do with a Lisp tag (which would be
5250 called an object type in git's context). A
5251 tag is most typically used to mark a particular point in the
5252 commit ancestry chain.
5253 </literal>
5254 </programlisting>
5255 </blockquote>
5256 <para><emphasis>tag object</emphasis></para>
5257 <blockquote>
5258 <para>An object containing a ref pointing to</para>
5259 <programlisting>
5260 <literal>another object, which can contain a message just like a
5261 commit object. It can also contain a (PGP)
5262 signature, in which case it is called a "signed tag object".
5263 </literal>
5264 </programlisting>
5265 </blockquote>
5266 <para><emphasis>topic branch</emphasis></para>
5267 <blockquote>
5268 <para>A regular git branch that is used by a developer to</para>
5269 <programlisting>
5270 <literal>identify a conceptual line of development. Since branches are very easy
5271 and inexpensive, it is often desirable to have several small branches
5272 that each contain very well defined concepts or small incremental yet
5273 related changes.
5274 </literal>
5275 </programlisting>
5276 </blockquote>
5277 <para><emphasis>tracking branch</emphasis></para>
5278 <blockquote>
5279 <para>A regular git branch that is used to follow changes
5280 from</para>
5281 <programlisting>
5282 <literal>another repository. A tracking
5283 branch should not contain direct modifications or have local commits
5284 made to it. A tracking branch can usually be
5285 identified as the right-hand-side ref in a Pull:
5286 refspec.
5287 </literal>
5288 </programlisting>
5289 </blockquote>
5290 <para><emphasis>tree</emphasis></para>
5291 <blockquote>
5292 <para>Either a working tree, or a tree object together with the
5293 dependent</para>
5294 <programlisting>
5295 <literal>blob and tree objects (i.e. a stored representation of a working tree).
5296 </literal>
5297 </programlisting>
5298 </blockquote>
5299 <para><emphasis>tree object</emphasis></para>
5300 <blockquote>
5301 <para>An object containing a list of file names and modes
5302 along</para>
5303 <programlisting>
5304 <literal>with refs to the associated blob and/or tree objects. A
5305 tree is equivalent to a directory.
5306 </literal>
5307 </programlisting>
5308 </blockquote>
5309 <para><emphasis>tree-ish</emphasis></para>
5310 <blockquote>
5311 <para>A ref pointing to either a commit object, a tree object, or a
5312 tag</para>
5313 <programlisting>
5314 <literal>object pointing to a tag or commit or tree object.
5315 </literal>
5316 </programlisting>
5317 </blockquote>
5318 <para><emphasis>unmerged index</emphasis></para>
5319 <blockquote>
5320 <para>An index which contains unmerged</para>
5321 <programlisting>
5322 <literal>index entries.
5323 </literal>
5324 </programlisting>
5325 </blockquote>
5326 <para><emphasis>unreachable object</emphasis></para>
5327 <blockquote>
5328 <para>An object which is not reachable from a</para>
5329 <programlisting>
5330 <literal>branch, tag, or any other reference.
5331 </literal>
5332 </programlisting>
5333 </blockquote>
5334 <para><emphasis>working tree</emphasis></para>
5335 <blockquote>
5336 <para>The tree of actual checked out files. The working tree
5337 is</para>
5338 <programlisting>
5339 <literal>normally equal to the HEAD plus any local changes
5340 that you have made but not yet committed.
5341 </literal>
5342 </programlisting>
5343 </blockquote>
5349 </sect2></sect1></chapter>