blob: 1ceb9d939f831e8e75d89c2db1a5f805bc34b2f8 [file] [log] [blame]
Junio C Hamanodfccbb02008-05-26 01:16:141<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Junio C Hamanoba4b9282008-07-06 05:20:316<meta name="generator" content="AsciiDoc 8.2.5" />
Junio C Hamanodfccbb02008-05-26 01:16:147<style type="text/css">
8/* Debug borders */
9p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
10/*
11 border: 1px solid red;
12*/
13}
14
15body {
16 margin: 1em 5% 1em 5%;
17}
18
Junio C Hamanoba4b9282008-07-06 05:20:3119a {
20 color: blue;
21 text-decoration: underline;
22}
23a:visited {
24 color: fuchsia;
25}
Junio C Hamanodfccbb02008-05-26 01:16:1426
27em {
28 font-style: italic;
29}
30
31strong {
32 font-weight: bold;
33}
34
35tt {
36 color: navy;
37}
38
39h1, h2, h3, h4, h5, h6 {
40 color: #527bbd;
41 font-family: sans-serif;
42 margin-top: 1.2em;
43 margin-bottom: 0.5em;
44 line-height: 1.3;
45}
46
Junio C Hamanoba4b9282008-07-06 05:20:3147h1, h2, h3 {
Junio C Hamanodfccbb02008-05-26 01:16:1448 border-bottom: 2px solid silver;
49}
50h2 {
Junio C Hamanodfccbb02008-05-26 01:16:1451 padding-top: 0.5em;
52}
Junio C Hamanoba4b9282008-07-06 05:20:3153h3 {
54 float: left;
55}
56h3 + * {
57 clear: left;
58}
Junio C Hamanodfccbb02008-05-26 01:16:1459
60div.sectionbody {
61 font-family: serif;
62 margin-left: 0;
63}
64
65hr {
66 border: 1px solid silver;
67}
68
69p {
70 margin-top: 0.5em;
71 margin-bottom: 0.5em;
72}
73
74pre {
75 padding: 0;
76 margin: 0;
77}
78
79span#author {
80 color: #527bbd;
81 font-family: sans-serif;
82 font-weight: bold;
Junio C Hamanoba4b9282008-07-06 05:20:3183 font-size: 1.1em;
Junio C Hamanodfccbb02008-05-26 01:16:1484}
85span#email {
86}
87span#revision {
88 font-family: sans-serif;
89}
90
91div#footer {
92 font-family: sans-serif;
93 font-size: small;
94 border-top: 2px solid silver;
95 padding-top: 0.5em;
96 margin-top: 4.0em;
97}
98div#footer-text {
99 float: left;
100 padding-bottom: 0.5em;
101}
102div#footer-badges {
103 float: right;
104 padding-bottom: 0.5em;
105}
106
107div#preamble,
108div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
109div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
110div.admonitionblock {
111 margin-right: 10%;
112 margin-top: 1.5em;
113 margin-bottom: 1.5em;
114}
115div.admonitionblock {
116 margin-top: 2.5em;
117 margin-bottom: 2.5em;
118}
119
120div.content { /* Block element content. */
121 padding: 0;
122}
123
124/* Block element titles. */
125div.title, caption.title {
126 font-family: sans-serif;
127 font-weight: bold;
128 text-align: left;
129 margin-top: 1.0em;
130 margin-bottom: 0.5em;
131}
132div.title + * {
133 margin-top: 0;
134}
135
136td div.title:first-child {
137 margin-top: 0.0em;
138}
139div.content div.title:first-child {
140 margin-top: 0.0em;
141}
142div.content + div.title {
143 margin-top: 0.0em;
144}
145
146div.sidebarblock > div.content {
147 background: #ffffee;
148 border: 1px solid silver;
149 padding: 0.5em;
150}
151
Junio C Hamanoba4b9282008-07-06 05:20:31152div.listingblock {
153 margin-right: 0%;
154}
Junio C Hamanodfccbb02008-05-26 01:16:14155div.listingblock > div.content {
156 border: 1px solid silver;
157 background: #f4f4f4;
158 padding: 0.5em;
159}
160
161div.quoteblock > div.content {
162 padding-left: 2.0em;
163}
Junio C Hamanoba4b9282008-07-06 05:20:31164
165div.attribution {
Junio C Hamanodfccbb02008-05-26 01:16:14166 text-align: right;
167}
Junio C Hamanoba4b9282008-07-06 05:20:31168div.verseblock + div.attribution {
169 text-align: left;
170}
Junio C Hamanodfccbb02008-05-26 01:16:14171
172div.admonitionblock .icon {
173 vertical-align: top;
174 font-size: 1.1em;
175 font-weight: bold;
176 text-decoration: underline;
177 color: #527bbd;
178 padding-right: 0.5em;
179}
180div.admonitionblock td.content {
181 padding-left: 0.5em;
182 border-left: 2px solid silver;
183}
184
185div.exampleblock > div.content {
186 border-left: 2px solid silver;
187 padding: 0.5em;
188}
189
190div.verseblock div.content {
191 white-space: pre;
192}
193
194div.imageblock div.content { padding-left: 0; }
195div.imageblock img { border: 1px solid silver; }
196span.image img { border-style: none; }
197
198dl {
199 margin-top: 0.8em;
200 margin-bottom: 0.8em;
201}
202dt {
203 margin-top: 0.5em;
204 margin-bottom: 0;
205 font-style: italic;
206}
207dd > *:first-child {
208 margin-top: 0;
209}
210
211ul, ol {
212 list-style-position: outside;
213}
Junio C Hamanoba4b9282008-07-06 05:20:31214div.olist2 ol {
Junio C Hamanodfccbb02008-05-26 01:16:14215 list-style-type: lower-alpha;
216}
217
218div.tableblock > table {
Junio C Hamanoba4b9282008-07-06 05:20:31219 border: 3px solid #527bbd;
Junio C Hamanodfccbb02008-05-26 01:16:14220}
221thead {
222 font-family: sans-serif;
223 font-weight: bold;
224}
225tfoot {
226 font-weight: bold;
227}
228
229div.hlist {
230 margin-top: 0.8em;
231 margin-bottom: 0.8em;
232}
Junio C Hamanoba4b9282008-07-06 05:20:31233div.hlist td {
234 padding-bottom: 5px;
235}
Junio C Hamanodfccbb02008-05-26 01:16:14236td.hlist1 {
237 vertical-align: top;
238 font-style: italic;
239 padding-right: 0.8em;
240}
241td.hlist2 {
242 vertical-align: top;
243}
244
245@media print {
246 div#footer-badges { display: none; }
247}
Junio C Hamanoba4b9282008-07-06 05:20:31248
249div#toctitle {
250 color: #527bbd;
251 font-family: sans-serif;
252 font-size: 1.1em;
253 font-weight: bold;
254 margin-top: 1.0em;
255 margin-bottom: 0.1em;
256}
257
258div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
259 margin-top: 0;
260 margin-bottom: 0;
261}
262div.toclevel2 {
263 margin-left: 2em;
264 font-size: 0.9em;
265}
266div.toclevel3 {
267 margin-left: 4em;
268 font-size: 0.9em;
269}
270div.toclevel4 {
271 margin-left: 6em;
272 font-size: 0.9em;
273}
274include1::./stylesheets/xhtml11-manpage.css[]
Junio C Hamanodfccbb02008-05-26 01:16:14275/* Workarounds for IE6's broken and incomplete CSS2. */
276
277div.sidebar-content {
278 background: #ffffee;
279 border: 1px solid silver;
280 padding: 0.5em;
281}
282div.sidebar-title, div.image-title {
283 font-family: sans-serif;
284 font-weight: bold;
285 margin-top: 0.0em;
286 margin-bottom: 0.5em;
287}
288
289div.listingblock div.content {
290 border: 1px solid silver;
291 background: #f4f4f4;
292 padding: 0.5em;
293}
294
295div.quoteblock-content {
296 padding-left: 2.0em;
297}
298
299div.exampleblock-content {
300 border-left: 2px solid silver;
301 padding-left: 0.5em;
302}
Junio C Hamanoba4b9282008-07-06 05:20:31303
304/* IE6 sets dynamically generated links as visited. */
305div#toc a:visited { color: blue; }
Junio C Hamanodfccbb02008-05-26 01:16:14306</style>
307<title>gittutorial-2(7)</title>
308</head>
309<body>
310<div id="header">
311<h1>
312gittutorial-2(7) Manual Page
313</h1>
314<h2>NAME</h2>
315<div class="sectionbody">
316<p>gittutorial-2 -
317 A tutorial introduction to git: part two
318</p>
319</div>
320</div>
321<h2>SYNOPSIS</h2>
322<div class="sectionbody">
Junio C Hamanoba4b9282008-07-06 05:20:31323<div class="para"><p>git *</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14324</div>
Junio C Hamanoba4b9282008-07-06 05:20:31325<h2 id="_description">DESCRIPTION</h2>
Junio C Hamanodfccbb02008-05-26 01:16:14326<div class="sectionbody">
Junio C Hamanoba4b9282008-07-06 05:20:31327<div class="para"><p>You should work through <a href="gittutorial.html">gittutorial(7)</a> before reading this tutorial.</p></div>
328<div class="para"><p>The goal of this tutorial is to introduce two fundamental pieces of
Junio C Hamanodfccbb02008-05-26 01:16:14329git's architecture--the object database and the index file--and to
330provide the reader with everything necessary to understand the rest
Junio C Hamanoba4b9282008-07-06 05:20:31331of the git documentation.</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14332</div>
Junio C Hamanoba4b9282008-07-06 05:20:31333<h2 id="_the_git_object_database">The git object database</h2>
Junio C Hamanodfccbb02008-05-26 01:16:14334<div class="sectionbody">
Junio C Hamanoba4b9282008-07-06 05:20:31335<div class="para"><p>Let's start a new project and create a small amount of history:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14336<div class="listingblock">
337<div class="content">
338<pre><tt>$ mkdir test-project
339$ cd test-project
340$ git init
341Initialized empty Git repository in .git/
342$ echo 'hello world' &gt; file.txt
343$ git add .
344$ git commit -a -m "initial commit"
Junio C Hamanobc4852e2009-01-21 10:51:14345[master (root-commit) 54196cc] initial commit
Junio C Hamanoa2deec52008-10-18 18:58:09346 1 files changed, 1 insertions(+), 0 deletions(-)
Junio C Hamanodfccbb02008-05-26 01:16:14347 create mode 100644 file.txt
348$ echo 'hello world!' &gt;file.txt
349$ git commit -a -m "add emphasis"
Junio C Hamanobc4852e2009-01-21 10:51:14350[master c4d59f3] add emphasis
Junio C Hamanoa2deec52008-10-18 18:58:09351 1 files changed, 1 insertions(+), 1 deletions(-)</tt></pre>
Junio C Hamanodfccbb02008-05-26 01:16:14352</div></div>
Junio C Hamanoa2deec52008-10-18 18:58:09353<div class="para"><p>What are the 7 digits of hex that git responded to the commit with?</p></div>
Junio C Hamanoba4b9282008-07-06 05:20:31354<div class="para"><p>We saw in part one of the tutorial that commits have names like this.
Junio C Hamanodfccbb02008-05-26 01:16:14355It turns out that every object in the git history is stored under
Junio C Hamanoa2deec52008-10-18 18:58:09356a 40-digit hex name. That name is the SHA1 hash of the object's
Junio C Hamanodfccbb02008-05-26 01:16:14357contents; among other things, this ensures that git will never store
358the same data twice (since identical data is given an identical SHA1
359name), and that the contents of a git object will never change (since
Junio C Hamanoa2deec52008-10-18 18:58:09360that would change the object's name as well). The 7 char hex strings
361here are simply the abbreviation of such 40 character long strings.
362Abbreviations can be used everywhere where the 40 character strings
363can be used, so long as they are unambiguous.</p></div>
Junio C Hamanoba4b9282008-07-06 05:20:31364<div class="para"><p>It is expected that the content of the commit object you created while
Junio C Hamanodfccbb02008-05-26 01:16:14365following the example above generates a different SHA1 hash than
366the one shown above because the commit object records the time when
Junio C Hamanoba4b9282008-07-06 05:20:31367it was created and the name of the person performing the commit.</p></div>
368<div class="para"><p>We can ask git about this particular object with the <tt>cat-file</tt>
Junio C Hamanodfccbb02008-05-26 01:16:14369command. Don't copy the 40 hex digits from this example but use those
370from your own version. Note that you can shorten it to only a few
Junio C Hamanoba4b9282008-07-06 05:20:31371characters to save yourself typing all 40 hex digits:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14372<div class="listingblock">
373<div class="content">
Junio C Hamano432d8ee2008-06-27 08:26:43374<pre><tt>$ git cat-file -t 54196cc2
Junio C Hamanodfccbb02008-05-26 01:16:14375commit
Junio C Hamano432d8ee2008-06-27 08:26:43376$ git cat-file commit 54196cc2
Junio C Hamanodfccbb02008-05-26 01:16:14377tree 92b8b694ffb1675e5975148e1121810081dbdffe
378author J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
379committer J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
380
381initial commit</tt></pre>
382</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31383<div class="para"><p>A tree can refer to one or more "blob" objects, each corresponding to
Junio C Hamanodfccbb02008-05-26 01:16:14384a file. In addition, a tree can also refer to other tree objects,
385thus creating a directory hierarchy. You can examine the contents of
386any tree using ls-tree (remember that a long enough initial portion
Junio C Hamanoba4b9282008-07-06 05:20:31387of the SHA1 will also work):</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14388<div class="listingblock">
389<div class="content">
390<pre><tt>$ git ls-tree 92b8b694
391100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad file.txt</tt></pre>
392</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31393<div class="para"><p>Thus we see that this tree has one file in it. The SHA1 hash is a
394reference to that file's data:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14395<div class="listingblock">
396<div class="content">
397<pre><tt>$ git cat-file -t 3b18e512
398blob</tt></pre>
399</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31400<div class="para"><p>A "blob" is just file data, which we can also examine with cat-file:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14401<div class="listingblock">
402<div class="content">
403<pre><tt>$ git cat-file blob 3b18e512
404hello world</tt></pre>
405</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31406<div class="para"><p>Note that this is the old file data; so the object that git named in
Junio C Hamanodfccbb02008-05-26 01:16:14407its response to the initial tree was a tree with a snapshot of the
Junio C Hamanoba4b9282008-07-06 05:20:31408directory state that was recorded by the first commit.</p></div>
409<div class="para"><p>All of these objects are stored under their SHA1 names inside the git
410directory:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14411<div class="listingblock">
412<div class="content">
413<pre><tt>$ find .git/objects/
414.git/objects/
415.git/objects/pack
416.git/objects/info
417.git/objects/3b
418.git/objects/3b/18e512dba79e4c8300dd08aeb37f8e728b8dad
419.git/objects/92
420.git/objects/92/b8b694ffb1675e5975148e1121810081dbdffe
421.git/objects/54
422.git/objects/54/196cc2703dc165cbd373a65a4dcf22d50ae7f7
423.git/objects/a0
424.git/objects/a0/423896973644771497bdc03eb99d5281615b51
425.git/objects/d0
426.git/objects/d0/492b368b66bdabf2ac1fd8c92b39d3db916e59
427.git/objects/c4
428.git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241</tt></pre>
429</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31430<div class="para"><p>and the contents of these files is just the compressed data plus a
Junio C Hamanodfccbb02008-05-26 01:16:14431header identifying their length and their type. The type is either a
Junio C Hamanoba4b9282008-07-06 05:20:31432blob, a tree, a commit, or a tag.</p></div>
433<div class="para"><p>The simplest commit to find is the HEAD commit, which we can find
434from .git/HEAD:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14435<div class="listingblock">
436<div class="content">
437<pre><tt>$ cat .git/HEAD
438ref: refs/heads/master</tt></pre>
439</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31440<div class="para"><p>As you can see, this tells us which branch we're currently on, and it
Junio C Hamanodfccbb02008-05-26 01:16:14441tells us this by naming a file under the .git directory, which itself
442contains a SHA1 name referring to a commit object, which we can
Junio C Hamanoba4b9282008-07-06 05:20:31443examine with cat-file:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14444<div class="listingblock">
445<div class="content">
446<pre><tt>$ cat .git/refs/heads/master
447c4d59f390b9cfd4318117afde11d601c1085f241
448$ git cat-file -t c4d59f39
449commit
450$ git cat-file commit c4d59f39
451tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59
452parent 54196cc2703dc165cbd373a65a4dcf22d50ae7f7
453author J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143418702 -0500
454committer J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143418702 -0500
455
456add emphasis</tt></pre>
457</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31458<div class="para"><p>The "tree" object here refers to the new state of the tree:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14459<div class="listingblock">
460<div class="content">
461<pre><tt>$ git ls-tree d0492b36
462100644 blob a0423896973644771497bdc03eb99d5281615b51 file.txt
463$ git cat-file blob a0423896
464hello world!</tt></pre>
465</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31466<div class="para"><p>and the "parent" object refers to the previous commit:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14467<div class="listingblock">
468<div class="content">
Junio C Hamano432d8ee2008-06-27 08:26:43469<pre><tt>$ git cat-file commit 54196cc2
Junio C Hamanodfccbb02008-05-26 01:16:14470tree 92b8b694ffb1675e5975148e1121810081dbdffe
471author J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
472committer J. Bruce Fields &lt;bfields@puzzle.fieldses.org&gt; 1143414668 -0500
473
474initial commit</tt></pre>
475</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31476<div class="para"><p>The tree object is the tree we examined first, and this commit is
477unusual in that it lacks any parent.</p></div>
478<div class="para"><p>Most commits have only one parent, but it is also common for a commit
Junio C Hamanodfccbb02008-05-26 01:16:14479to have multiple parents. In that case the commit represents a
480merge, with the parent references pointing to the heads of the merged
Junio C Hamanoba4b9282008-07-06 05:20:31481branches.</p></div>
482<div class="para"><p>Besides blobs, trees, and commits, the only remaining type of object
Junio C Hamanodfccbb02008-05-26 01:16:14483is a "tag", which we won't discuss here; refer to <a href="git-tag.html">git-tag(1)</a>
Junio C Hamanoba4b9282008-07-06 05:20:31484for details.</p></div>
485<div class="para"><p>So now we know how git uses the object database to represent a
486project's history:</p></div>
487<div class="ilist"><ul>
Junio C Hamanodfccbb02008-05-26 01:16:14488<li>
489<p>
490"commit" objects refer to "tree" objects representing the
491 snapshot of a directory tree at a particular point in the
492 history, and refer to "parent" commits to show how they're
493 connected into the project history.
494</p>
495</li>
496<li>
497<p>
498"tree" objects represent the state of a single directory,
499 associating directory names to "blob" objects containing file
500 data and "tree" objects containing subdirectory information.
501</p>
502</li>
503<li>
504<p>
505"blob" objects contain file data without any other structure.
506</p>
507</li>
508<li>
509<p>
510References to commit objects at the head of each branch are
511 stored in files under .git/refs/heads/.
512</p>
513</li>
514<li>
515<p>
516The name of the current branch is stored in .git/HEAD.
517</p>
518</li>
Junio C Hamanoba4b9282008-07-06 05:20:31519</ul></div>
520<div class="para"><p>Note, by the way, that lots of commands take a tree as an argument.
Junio C Hamanodfccbb02008-05-26 01:16:14521But as we can see above, a tree can be referred to in many different
522ways--by the SHA1 name for that tree, by the name of a commit that
523refers to the tree, by the name of a branch whose head refers to that
Junio C Hamanoba4b9282008-07-06 05:20:31524tree, etc.--and most such commands can accept any of these names.</p></div>
525<div class="para"><p>In command synopses, the word "tree-ish" is sometimes used to
526designate such an argument.</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14527</div>
Junio C Hamanoba4b9282008-07-06 05:20:31528<h2 id="_the_index_file">The index file</h2>
Junio C Hamanodfccbb02008-05-26 01:16:14529<div class="sectionbody">
Junio C Hamanoba4b9282008-07-06 05:20:31530<div class="para"><p>The primary tool we've been using to create commits is <tt>git-commit
Junio C Hamanofce7c7e2008-07-02 03:06:38531-a</tt>, which creates a commit including every change you've made to
Junio C Hamanodfccbb02008-05-26 01:16:14532your working tree. But what if you want to commit changes only to
Junio C Hamanoba4b9282008-07-06 05:20:31533certain files? Or only certain changes to certain files?</p></div>
534<div class="para"><p>If we look at the way commits are created under the cover, we'll see
535that there are more flexible ways creating commits.</p></div>
536<div class="para"><p>Continuing with our test-project, let's modify file.txt again:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14537<div class="listingblock">
538<div class="content">
539<pre><tt>$ echo "hello world, again" &gt;&gt;file.txt</tt></pre>
540</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31541<div class="para"><p>but this time instead of immediately making the commit, let's take an
Junio C Hamanodfccbb02008-05-26 01:16:14542intermediate step, and ask for diffs along the way to keep track of
Junio C Hamanoba4b9282008-07-06 05:20:31543what's happening:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14544<div class="listingblock">
545<div class="content">
546<pre><tt>$ git diff
547--- a/file.txt
548+++ b/file.txt
549@@ -1 +1,2 @@
550 hello world!
551+hello world, again
552$ git add file.txt
553$ git diff</tt></pre>
554</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31555<div class="para"><p>The last diff is empty, but no new commits have been made, and the
556head still doesn't contain the new line:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14557<div class="listingblock">
558<div class="content">
Junio C Hamano432d8ee2008-06-27 08:26:43559<pre><tt>$ git diff HEAD
Junio C Hamanodfccbb02008-05-26 01:16:14560diff --git a/file.txt b/file.txt
561index a042389..513feba 100644
562--- a/file.txt
563+++ b/file.txt
564@@ -1 +1,2 @@
565 hello world!
566+hello world, again</tt></pre>
567</div></div>
Junio C Hamano1aa40d22010-01-21 17:46:43568<div class="para"><p>So <em>git diff</em> is comparing against something other than the head.
Junio C Hamanodfccbb02008-05-26 01:16:14569The thing that it's comparing against is actually the index file,
570which is stored in .git/index in a binary format, but whose contents
Junio C Hamanoba4b9282008-07-06 05:20:31571we can examine with ls-files:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14572<div class="listingblock">
573<div class="content">
574<pre><tt>$ git ls-files --stage
575100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt
576$ git cat-file -t 513feba2
577blob
578$ git cat-file blob 513feba2
579hello world!
580hello world, again</tt></pre>
581</div></div>
Junio C Hamano1aa40d22010-01-21 17:46:43582<div class="para"><p>So what our <em>git add</em> did was store a new blob and then put
Junio C Hamanodfccbb02008-05-26 01:16:14583a reference to it in the index file. If we modify the file again,
Junio C Hamano1aa40d22010-01-21 17:46:43584we'll see that the new modifications are reflected in the <em>git diff</em>
Junio C Hamanoba4b9282008-07-06 05:20:31585output:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14586<div class="listingblock">
587<div class="content">
588<pre><tt>$ echo 'again?' &gt;&gt;file.txt
589$ git diff
590index 513feba..ba3da7b 100644
591--- a/file.txt
592+++ b/file.txt
593@@ -1,2 +1,3 @@
594 hello world!
595 hello world, again
596+again?</tt></pre>
597</div></div>
Junio C Hamano1aa40d22010-01-21 17:46:43598<div class="para"><p>With the right arguments, <em>git diff</em> can also show us the difference
Junio C Hamanodfccbb02008-05-26 01:16:14599between the working directory and the last commit, or between the
Junio C Hamanoba4b9282008-07-06 05:20:31600index and the last commit:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14601<div class="listingblock">
602<div class="content">
603<pre><tt>$ git diff HEAD
604diff --git a/file.txt b/file.txt
605index a042389..ba3da7b 100644
606--- a/file.txt
607+++ b/file.txt
608@@ -1 +1,3 @@
609 hello world!
610+hello world, again
611+again?
612$ git diff --cached
613diff --git a/file.txt b/file.txt
614index a042389..513feba 100644
615--- a/file.txt
616+++ b/file.txt
617@@ -1 +1,2 @@
618 hello world!
619+hello world, again</tt></pre>
620</div></div>
Junio C Hamano1aa40d22010-01-21 17:46:43621<div class="para"><p>At any time, we can create a new commit using <em>git commit</em> (without
Junio C Hamanofce7c7e2008-07-02 03:06:38622the "-a" option), and verify that the state committed only includes the
Junio C Hamanodfccbb02008-05-26 01:16:14623changes stored in the index file, not the additional change that is
Junio C Hamanoba4b9282008-07-06 05:20:31624still only in our working tree:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14625<div class="listingblock">
626<div class="content">
627<pre><tt>$ git commit -m "repeat"
628$ git diff HEAD
629diff --git a/file.txt b/file.txt
630index 513feba..ba3da7b 100644
631--- a/file.txt
632+++ b/file.txt
633@@ -1,2 +1,3 @@
634 hello world!
635 hello world, again
636+again?</tt></pre>
637</div></div>
Junio C Hamano1aa40d22010-01-21 17:46:43638<div class="para"><p>So by default <em>git commit</em> uses the index to create the commit, not
Junio C Hamanofce7c7e2008-07-02 03:06:38639the working tree; the "-a" option to commit tells it to first update
Junio C Hamanoba4b9282008-07-06 05:20:31640the index with all changes in the working tree.</p></div>
Junio C Hamano1aa40d22010-01-21 17:46:43641<div class="para"><p>Finally, it's worth looking at the effect of <em>git add</em> on the index
Junio C Hamanoba4b9282008-07-06 05:20:31642file:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14643<div class="listingblock">
644<div class="content">
645<pre><tt>$ echo "goodbye, world" &gt;closing.txt
646$ git add closing.txt</tt></pre>
647</div></div>
Junio C Hamano1aa40d22010-01-21 17:46:43648<div class="para"><p>The effect of the <em>git add</em> was to add one entry to the index file:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14649<div class="listingblock">
650<div class="content">
651<pre><tt>$ git ls-files --stage
652100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138 0 closing.txt
653100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt</tt></pre>
654</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31655<div class="para"><p>And, as you can see with cat-file, this new entry refers to the
656current contents of the file:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14657<div class="listingblock">
658<div class="content">
659<pre><tt>$ git cat-file blob 8b9743b2
660goodbye, world</tt></pre>
661</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31662<div class="para"><p>The "status" command is a useful way to get a quick summary of the
663situation:</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14664<div class="listingblock">
665<div class="content">
666<pre><tt>$ git status
667# On branch master
668# Changes to be committed:
669# (use "git reset HEAD &lt;file&gt;..." to unstage)
670#
671# new file: closing.txt
672#
673# Changed but not updated:
674# (use "git add &lt;file&gt;..." to update what will be committed)
675#
676# modified: file.txt
677#</tt></pre>
678</div></div>
Junio C Hamanoba4b9282008-07-06 05:20:31679<div class="para"><p>Since the current state of closing.txt is cached in the index file,
Junio C Hamanodfccbb02008-05-26 01:16:14680it is listed as "Changes to be committed". Since file.txt has
681changes in the working directory that aren't reflected in the index,
682it is marked "changed but not updated". At this point, running "git
683commit" would create a commit that added closing.txt (with its new
Junio C Hamanoba4b9282008-07-06 05:20:31684contents), but that didn't modify file.txt.</p></div>
685<div class="para"><p>Also, note that a bare <tt>git diff</tt> shows the changes to file.txt, but
Junio C Hamanodfccbb02008-05-26 01:16:14686not the addition of closing.txt, because the version of closing.txt
Junio C Hamanoba4b9282008-07-06 05:20:31687in the index file is identical to the one in the working directory.</p></div>
688<div class="para"><p>In addition to being the staging area for new commits, the index file
Junio C Hamanodfccbb02008-05-26 01:16:14689is also populated from the object database when checking out a
690branch, and is used to hold the trees involved in a merge operation.
Junio C Hamanofce7c7e2008-07-02 03:06:38691See <a href="gitcore-tutorial.html">gitcore-tutorial(7)</a> and the relevant man
Junio C Hamanoba4b9282008-07-06 05:20:31692pages for details.</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14693</div>
Junio C Hamanoba4b9282008-07-06 05:20:31694<h2 id="_what_next">What next?</h2>
Junio C Hamanodfccbb02008-05-26 01:16:14695<div class="sectionbody">
Junio C Hamanoba4b9282008-07-06 05:20:31696<div class="para"><p>At this point you should know everything necessary to read the man
Junio C Hamanodfccbb02008-05-26 01:16:14697pages for any of the git commands; one good place to start would be
698with the commands mentioned in <a href="everyday.html">Everyday git</a>. You
Junio C Hamanoba4b9282008-07-06 05:20:31699should be able to find any unknown jargon in <a href="gitglossary.html">gitglossary(7)</a>.</p></div>
700<div class="para"><p>The <a href="user-manual.html">Git User's Manual</a> provides a more
701comprehensive introduction to git.</p></div>
702<div class="para"><p><a href="gitcvs-migration.html">gitcvs-migration(7)</a> explains how to
Junio C Hamanodfccbb02008-05-26 01:16:14703import a CVS repository into git, and shows how to use git in a
Junio C Hamanoba4b9282008-07-06 05:20:31704CVS-like way.</p></div>
705<div class="para"><p>For some interesting examples of git use, see the
706<a href="howto-index.html">howtos</a>.</p></div>
707<div class="para"><p>For git developers, <a href="gitcore-tutorial.html">gitcore-tutorial(7)</a> goes
Junio C Hamanodfccbb02008-05-26 01:16:14708into detail on the lower-level git mechanisms involved in, for
Junio C Hamanoba4b9282008-07-06 05:20:31709example, creating a new commit.</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14710</div>
Junio C Hamanoba4b9282008-07-06 05:20:31711<h2 id="_see_also">SEE ALSO</h2>
Junio C Hamanodfccbb02008-05-26 01:16:14712<div class="sectionbody">
Junio C Hamanoba4b9282008-07-06 05:20:31713<div class="para"><p><a href="gittutorial.html">gittutorial(7)</a>,
Junio C Hamanodfccbb02008-05-26 01:16:14714<a href="gitcvs-migration.html">gitcvs-migration(7)</a>,
Junio C Hamano9e1793f2008-06-02 07:31:16715<a href="gitcore-tutorial.html">gitcore-tutorial(7)</a>,
716<a href="gitglossary.html">gitglossary(7)</a>,
Junio C Hamanof66ecee2008-11-17 18:25:43717<a href="git-help.html">git-help(1)</a>,
Junio C Hamanodfccbb02008-05-26 01:16:14718<a href="everyday.html">Everyday git</a>,
Junio C Hamanoba4b9282008-07-06 05:20:31719<a href="user-manual.html">The Git User's Manual</a></p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14720</div>
Junio C Hamanoba4b9282008-07-06 05:20:31721<h2 id="_git">GIT</h2>
Junio C Hamanodfccbb02008-05-26 01:16:14722<div class="sectionbody">
Junio C Hamanoba4b9282008-07-06 05:20:31723<div class="para"><p>Part of the <a href="git.html">git(1)</a> suite.</p></div>
Junio C Hamanodfccbb02008-05-26 01:16:14724</div>
725<div id="footer">
726<div id="footer-text">
Junio C Hamanoac752fa2010-09-19 00:01:00727Last updated 2010-09-18 23:57:05 UTC
Junio C Hamanodfccbb02008-05-26 01:16:14728</div>
729</div>
730</body>
731</html>