Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
| 2 | "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <meta name="generator" content="AsciiDoc 7.0.2" /> |
| 7 | <style type="text/css"> |
| 8 | /* Debug borders */ |
| 9 | p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 { |
| 10 | /* |
| 11 | border: 1px solid red; |
| 12 | */ |
| 13 | } |
| 14 | |
| 15 | body { |
| 16 | margin: 1em 5% 1em 5%; |
| 17 | } |
| 18 | |
| 19 | a { color: blue; } |
| 20 | a:visited { color: fuchsia; } |
| 21 | |
| 22 | em { |
| 23 | font-style: italic; |
| 24 | } |
| 25 | |
| 26 | strong { |
| 27 | font-weight: bold; |
| 28 | } |
| 29 | |
| 30 | tt { |
| 31 | color: navy; |
| 32 | } |
| 33 | |
| 34 | h1, h2, h3, h4, h5, h6 { |
| 35 | color: #527bbd; |
| 36 | font-family: sans-serif; |
| 37 | margin-top: 1.2em; |
| 38 | margin-bottom: 0.5em; |
| 39 | line-height: 1.3; |
| 40 | } |
| 41 | |
| 42 | h1 { |
| 43 | border-bottom: 2px solid silver; |
| 44 | } |
| 45 | h2 { |
| 46 | border-bottom: 2px solid silver; |
| 47 | padding-top: 0.5em; |
| 48 | } |
| 49 | |
| 50 | div.sectionbody { |
| 51 | font-family: serif; |
| 52 | margin-left: 0; |
| 53 | } |
| 54 | |
| 55 | hr { |
| 56 | border: 1px solid silver; |
| 57 | } |
| 58 | |
| 59 | p { |
| 60 | margin-top: 0.5em; |
| 61 | margin-bottom: 0.5em; |
| 62 | } |
| 63 | |
| 64 | pre { |
| 65 | padding: 0; |
| 66 | margin: 0; |
| 67 | } |
| 68 | |
| 69 | span#author { |
| 70 | color: #527bbd; |
| 71 | font-family: sans-serif; |
| 72 | font-weight: bold; |
| 73 | font-size: 1.2em; |
| 74 | } |
| 75 | span#email { |
| 76 | } |
| 77 | span#revision { |
| 78 | font-family: sans-serif; |
| 79 | } |
| 80 | |
| 81 | div#footer { |
| 82 | font-family: sans-serif; |
| 83 | font-size: small; |
| 84 | border-top: 2px solid silver; |
| 85 | padding-top: 0.5em; |
| 86 | margin-top: 4.0em; |
| 87 | } |
| 88 | div#footer-text { |
| 89 | float: left; |
| 90 | padding-bottom: 0.5em; |
| 91 | } |
| 92 | div#footer-badges { |
| 93 | float: right; |
| 94 | padding-bottom: 0.5em; |
| 95 | } |
| 96 | |
| 97 | div#preamble, |
| 98 | div.tableblock, div.imageblock, div.exampleblock, div.verseblock, |
| 99 | div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock, |
| 100 | div.admonitionblock { |
| 101 | margin-right: 10%; |
| 102 | margin-top: 1.5em; |
| 103 | margin-bottom: 1.5em; |
| 104 | } |
| 105 | div.admonitionblock { |
| 106 | margin-top: 2.5em; |
| 107 | margin-bottom: 2.5em; |
| 108 | } |
| 109 | |
| 110 | div.content { /* Block element content. */ |
| 111 | padding: 0; |
| 112 | } |
| 113 | |
| 114 | /* Block element titles. */ |
| 115 | div.title, caption.title { |
| 116 | font-family: sans-serif; |
| 117 | font-weight: bold; |
| 118 | text-align: left; |
| 119 | margin-top: 1.0em; |
| 120 | margin-bottom: 0.5em; |
| 121 | } |
| 122 | div.title + * { |
| 123 | margin-top: 0; |
| 124 | } |
| 125 | |
| 126 | td div.title:first-child { |
| 127 | margin-top: 0.0em; |
| 128 | } |
| 129 | div.content div.title:first-child { |
| 130 | margin-top: 0.0em; |
| 131 | } |
| 132 | div.content + div.title { |
| 133 | margin-top: 0.0em; |
| 134 | } |
| 135 | |
| 136 | div.sidebarblock > div.content { |
| 137 | background: #ffffee; |
| 138 | border: 1px solid silver; |
| 139 | padding: 0.5em; |
| 140 | } |
| 141 | |
| 142 | div.listingblock > div.content { |
| 143 | border: 1px solid silver; |
| 144 | background: #f4f4f4; |
| 145 | padding: 0.5em; |
| 146 | } |
| 147 | |
| 148 | div.quoteblock > div.content { |
| 149 | padding-left: 2.0em; |
| 150 | } |
| 151 | div.quoteblock .attribution { |
| 152 | text-align: right; |
| 153 | } |
| 154 | |
| 155 | div.admonitionblock .icon { |
| 156 | vertical-align: top; |
| 157 | font-size: 1.1em; |
| 158 | font-weight: bold; |
| 159 | text-decoration: underline; |
| 160 | color: #527bbd; |
| 161 | padding-right: 0.5em; |
| 162 | } |
| 163 | div.admonitionblock td.content { |
| 164 | padding-left: 0.5em; |
| 165 | border-left: 2px solid silver; |
| 166 | } |
| 167 | |
| 168 | div.exampleblock > div.content { |
| 169 | border-left: 2px solid silver; |
| 170 | padding: 0.5em; |
| 171 | } |
| 172 | |
| 173 | div.verseblock div.content { |
| 174 | white-space: pre; |
| 175 | } |
| 176 | |
| 177 | div.imageblock div.content { padding-left: 0; } |
| 178 | div.imageblock img { border: 1px solid silver; } |
| 179 | span.image img { border-style: none; } |
| 180 | |
| 181 | dl { |
| 182 | margin-top: 0.8em; |
| 183 | margin-bottom: 0.8em; |
| 184 | } |
| 185 | dt { |
| 186 | margin-top: 0.5em; |
| 187 | margin-bottom: 0; |
| 188 | font-style: italic; |
| 189 | } |
| 190 | dd > *:first-child { |
| 191 | margin-top: 0; |
| 192 | } |
| 193 | |
| 194 | ul, ol { |
| 195 | list-style-position: outside; |
| 196 | } |
| 197 | ol.olist2 { |
| 198 | list-style-type: lower-alpha; |
| 199 | } |
| 200 | |
| 201 | div.tableblock > table { |
| 202 | border-color: #527bbd; |
| 203 | border-width: 3px; |
| 204 | } |
| 205 | thead { |
| 206 | font-family: sans-serif; |
| 207 | font-weight: bold; |
| 208 | } |
| 209 | tfoot { |
| 210 | font-weight: bold; |
| 211 | } |
| 212 | |
| 213 | div.hlist { |
| 214 | margin-top: 0.8em; |
| 215 | margin-bottom: 0.8em; |
| 216 | } |
| 217 | td.hlist1 { |
| 218 | vertical-align: top; |
| 219 | font-style: italic; |
| 220 | padding-right: 0.8em; |
| 221 | } |
| 222 | td.hlist2 { |
| 223 | vertical-align: top; |
| 224 | } |
| 225 | |
| 226 | @media print { |
| 227 | div#footer-badges { display: none; } |
| 228 | } |
| 229 | /* Workarounds for IE6's broken and incomplete CSS2. */ |
| 230 | |
| 231 | div.sidebar-content { |
| 232 | background: #ffffee; |
| 233 | border: 1px solid silver; |
| 234 | padding: 0.5em; |
| 235 | } |
| 236 | div.sidebar-title, div.image-title { |
| 237 | font-family: sans-serif; |
| 238 | font-weight: bold; |
| 239 | margin-top: 0.0em; |
| 240 | margin-bottom: 0.5em; |
| 241 | } |
| 242 | |
| 243 | div.listingblock div.content { |
| 244 | border: 1px solid silver; |
| 245 | background: #f4f4f4; |
| 246 | padding: 0.5em; |
| 247 | } |
| 248 | |
| 249 | div.quoteblock-content { |
| 250 | padding-left: 2.0em; |
| 251 | } |
| 252 | |
| 253 | div.exampleblock-content { |
| 254 | border-left: 2px solid silver; |
| 255 | padding-left: 0.5em; |
| 256 | } |
| 257 | </style> |
| 258 | <title>A tutorial introduction to git: part two</title> |
| 259 | </head> |
| 260 | <body> |
| 261 | <div id="header"> |
| 262 | <h1>A tutorial introduction to git: part two</h1> |
| 263 | </div> |
| 264 | <div id="preamble"> |
| 265 | <div class="sectionbody"> |
| 266 | <p>You should work through <a href="tutorial.html">A tutorial introduction to |
| 267 | git</a> before reading this tutorial.</p> |
| 268 | <p>The goal of this tutorial is to introduce two fundamental pieces of |
| 269 | git's architecture—the object database and the index file—and to |
| 270 | provide the reader with everything necessary to understand the rest |
| 271 | of the git documentation.</p> |
| 272 | </div> |
| 273 | </div> |
| 274 | <h2>The git object database</h2> |
| 275 | <div class="sectionbody"> |
| 276 | <p>Let's start a new project and create a small amount of history:</p> |
| 277 | <div class="listingblock"> |
| 278 | <div class="content"> |
| 279 | <pre><tt>$ mkdir test-project |
| 280 | $ cd test-project |
Junio C Hamano | fc4d38c | 2007-01-08 06:53:32 | [diff] [blame] | 281 | $ git init |
Junio C Hamano | 7d23f5e | 2006-12-16 07:44:04 | [diff] [blame] | 282 | Initialized empty Git repository in .git/ |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 283 | $ echo 'hello world' > file.txt |
| 284 | $ git add . |
| 285 | $ git commit -a -m "initial commit" |
Junio C Hamano | 7d23f5e | 2006-12-16 07:44:04 | [diff] [blame] | 286 | Created initial commit 54196cc2703dc165cbd373a65a4dcf22d50ae7f7 |
Junio C Hamano | e7935c4 | 2006-12-13 21:32:17 | [diff] [blame] | 287 | create mode 100644 file.txt |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 288 | $ echo 'hello world!' >file.txt |
Junio C Hamano | 7d23f5e | 2006-12-16 07:44:04 | [diff] [blame] | 289 | $ git commit -a -m "add emphasis" |
| 290 | Created commit c4d59f390b9cfd4318117afde11d601c1085f241</tt></pre> |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 291 | </div></div> |
Junio C Hamano | 7d23f5e | 2006-12-16 07:44:04 | [diff] [blame] | 292 | <p>What are the 40 digits of hex that git responded to the commit with?</p> |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 293 | <p>We saw in part one of the tutorial that commits have names like this. |
| 294 | It turns out that every object in the git history is stored under |
| 295 | such a 40-digit hex name. That name is the SHA1 hash of the object's |
| 296 | contents; among other things, this ensures that git will never store |
| 297 | the same data twice (since identical data is given an identical SHA1 |
| 298 | name), and that the contents of a git object will never change (since |
| 299 | that would change the object's name as well).</p> |
Junio C Hamano | 7d23f5e | 2006-12-16 07:44:04 | [diff] [blame] | 300 | <p>It is expected that the content of the commit object you created while |
| 301 | following the example above generates a different SHA1 hash than |
| 302 | the one shown above because the commit object records the time when |
| 303 | it was created and the name of the person performing the commit.</p> |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 304 | <p>We can ask git about this particular object with the cat-file |
Junio C Hamano | 7d23f5e | 2006-12-16 07:44:04 | [diff] [blame] | 305 | command. Don't copy the 40 hex digits from this example but use those |
| 306 | from your own version. Note that you can shorten it to only a few |
| 307 | characters to save yourself typing all 40 hex digits:</p> |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 308 | <div class="listingblock"> |
| 309 | <div class="content"> |
Junio C Hamano | 7d23f5e | 2006-12-16 07:44:04 | [diff] [blame] | 310 | <pre><tt>$ git-cat-file -t 54196cc2 |
| 311 | commit |
| 312 | $ git-cat-file commit 54196cc2 |
| 313 | tree 92b8b694ffb1675e5975148e1121810081dbdffe |
| 314 | author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500 |
| 315 | committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500 |
| 316 | |
| 317 | initial commit</tt></pre> |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 318 | </div></div> |
| 319 | <p>A tree can refer to one or more "blob" objects, each corresponding to |
| 320 | a file. In addition, a tree can also refer to other tree objects, |
Junio C Hamano | 341071d | 2006-06-04 07:24:48 | [diff] [blame] | 321 | thus creating a directory hierarchy. You can examine the contents of |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 322 | any tree using ls-tree (remember that a long enough initial portion |
| 323 | of the SHA1 will also work):</p> |
| 324 | <div class="listingblock"> |
| 325 | <div class="content"> |
| 326 | <pre><tt>$ git ls-tree 92b8b694 |
| 327 | 100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad file.txt</tt></pre> |
| 328 | </div></div> |
| 329 | <p>Thus we see that this tree has one file in it. The SHA1 hash is a |
| 330 | reference to that file's data:</p> |
| 331 | <div class="listingblock"> |
| 332 | <div class="content"> |
| 333 | <pre><tt>$ git cat-file -t 3b18e512 |
| 334 | blob</tt></pre> |
| 335 | </div></div> |
| 336 | <p>A "blob" is just file data, which we can also examine with cat-file:</p> |
| 337 | <div class="listingblock"> |
| 338 | <div class="content"> |
| 339 | <pre><tt>$ git cat-file blob 3b18e512 |
| 340 | hello world</tt></pre> |
| 341 | </div></div> |
| 342 | <p>Note that this is the old file data; so the object that git named in |
| 343 | its response to the initial tree was a tree with a snapshot of the |
| 344 | directory state that was recorded by the first commit.</p> |
| 345 | <p>All of these objects are stored under their SHA1 names inside the git |
| 346 | directory:</p> |
| 347 | <div class="listingblock"> |
| 348 | <div class="content"> |
| 349 | <pre><tt>$ find .git/objects/ |
| 350 | .git/objects/ |
| 351 | .git/objects/pack |
| 352 | .git/objects/info |
| 353 | .git/objects/3b |
| 354 | .git/objects/3b/18e512dba79e4c8300dd08aeb37f8e728b8dad |
| 355 | .git/objects/92 |
| 356 | .git/objects/92/b8b694ffb1675e5975148e1121810081dbdffe |
| 357 | .git/objects/54 |
| 358 | .git/objects/54/196cc2703dc165cbd373a65a4dcf22d50ae7f7 |
| 359 | .git/objects/a0 |
| 360 | .git/objects/a0/423896973644771497bdc03eb99d5281615b51 |
| 361 | .git/objects/d0 |
| 362 | .git/objects/d0/492b368b66bdabf2ac1fd8c92b39d3db916e59 |
| 363 | .git/objects/c4 |
| 364 | .git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241</tt></pre> |
| 365 | </div></div> |
| 366 | <p>and the contents of these files is just the compressed data plus a |
| 367 | header identifying their length and their type. The type is either a |
Junio C Hamano | 7d23f5e | 2006-12-16 07:44:04 | [diff] [blame] | 368 | blob, a tree, a commit, or a tag.</p> |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 369 | <p>The simplest commit to find is the HEAD commit, which we can find |
| 370 | from .git/HEAD:</p> |
| 371 | <div class="listingblock"> |
| 372 | <div class="content"> |
| 373 | <pre><tt>$ cat .git/HEAD |
| 374 | ref: refs/heads/master</tt></pre> |
| 375 | </div></div> |
| 376 | <p>As you can see, this tells us which branch we're currently on, and it |
| 377 | tells us this by naming a file under the .git directory, which itself |
| 378 | contains a SHA1 name referring to a commit object, which we can |
| 379 | examine with cat-file:</p> |
| 380 | <div class="listingblock"> |
| 381 | <div class="content"> |
| 382 | <pre><tt>$ cat .git/refs/heads/master |
| 383 | c4d59f390b9cfd4318117afde11d601c1085f241 |
| 384 | $ git cat-file -t c4d59f39 |
| 385 | commit |
| 386 | $ git cat-file commit c4d59f39 |
| 387 | tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59 |
| 388 | parent 54196cc2703dc165cbd373a65a4dcf22d50ae7f7 |
| 389 | author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500 |
| 390 | committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500 |
| 391 | |
| 392 | add emphasis</tt></pre> |
| 393 | </div></div> |
| 394 | <p>The "tree" object here refers to the new state of the tree:</p> |
| 395 | <div class="listingblock"> |
| 396 | <div class="content"> |
| 397 | <pre><tt>$ git ls-tree d0492b36 |
| 398 | 100644 blob a0423896973644771497bdc03eb99d5281615b51 file.txt |
Junio C Hamano | 7da87bb | 2006-06-06 01:23:49 | [diff] [blame] | 399 | $ git cat-file blob a0423896 |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 400 | hello world!</tt></pre> |
| 401 | </div></div> |
| 402 | <p>and the "parent" object refers to the previous commit:</p> |
| 403 | <div class="listingblock"> |
| 404 | <div class="content"> |
| 405 | <pre><tt>$ git-cat-file commit 54196cc2 |
| 406 | tree 92b8b694ffb1675e5975148e1121810081dbdffe |
| 407 | author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500 |
| 408 | committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500 |
| 409 | |
| 410 | initial commit</tt></pre> |
| 411 | </div></div> |
| 412 | <p>The tree object is the tree we examined first, and this commit is |
| 413 | unusual in that it lacks any parent.</p> |
| 414 | <p>Most commits have only one parent, but it is also common for a commit |
| 415 | to have multiple parents. In that case the commit represents a |
| 416 | merge, with the parent references pointing to the heads of the merged |
| 417 | branches.</p> |
| 418 | <p>Besides blobs, trees, and commits, the only remaining type of object |
| 419 | is a "tag", which we won't discuss here; refer to <a href="git-tag.html">git-tag(1)</a> |
| 420 | for details.</p> |
| 421 | <p>So now we know how git uses the object database to represent a |
| 422 | project's history:</p> |
| 423 | <ul> |
| 424 | <li> |
| 425 | <p> |
| 426 | "commit" objects refer to "tree" objects representing the |
| 427 | snapshot of a directory tree at a particular point in the |
| 428 | history, and refer to "parent" commits to show how they're |
| 429 | connected into the project history. |
| 430 | </p> |
| 431 | </li> |
| 432 | <li> |
| 433 | <p> |
| 434 | "tree" objects represent the state of a single directory, |
| 435 | associating directory names to "blob" objects containing file |
| 436 | data and "tree" objects containing subdirectory information. |
| 437 | </p> |
| 438 | </li> |
| 439 | <li> |
| 440 | <p> |
| 441 | "blob" objects contain file data without any other structure. |
| 442 | </p> |
| 443 | </li> |
| 444 | <li> |
| 445 | <p> |
| 446 | References to commit objects at the head of each branch are |
| 447 | stored in files under .git/refs/heads/. |
| 448 | </p> |
| 449 | </li> |
| 450 | <li> |
| 451 | <p> |
| 452 | The name of the current branch is stored in .git/HEAD. |
| 453 | </p> |
| 454 | </li> |
| 455 | </ul> |
| 456 | <p>Note, by the way, that lots of commands take a tree as an argument. |
| 457 | But as we can see above, a tree can be referred to in many different |
| 458 | ways—by the SHA1 name for that tree, by the name of a commit that |
| 459 | refers to the tree, by the name of a branch whose head refers to that |
| 460 | tree, etc.—and most such commands can accept any of these names.</p> |
| 461 | <p>In command synopses, the word "tree-ish" is sometimes used to |
| 462 | designate such an argument.</p> |
| 463 | </div> |
| 464 | <h2>The index file</h2> |
| 465 | <div class="sectionbody"> |
| 466 | <p>The primary tool we've been using to create commits is "git commit |
| 467 | -a", which creates a commit including every change you've made to |
| 468 | your working tree. But what if you want to commit changes only to |
| 469 | certain files? Or only certain changes to certain files?</p> |
| 470 | <p>If we look at the way commits are created under the cover, we'll see |
| 471 | that there are more flexible ways creating commits.</p> |
| 472 | <p>Continuing with our test-project, let's modify file.txt again:</p> |
| 473 | <div class="listingblock"> |
| 474 | <div class="content"> |
| 475 | <pre><tt>$ echo "hello world, again" >>file.txt</tt></pre> |
| 476 | </div></div> |
| 477 | <p>but this time instead of immediately making the commit, let's take an |
| 478 | intermediate step, and ask for diffs along the way to keep track of |
| 479 | what's happening:</p> |
| 480 | <div class="listingblock"> |
| 481 | <div class="content"> |
| 482 | <pre><tt>$ git diff |
| 483 | --- a/file.txt |
| 484 | +++ b/file.txt |
| 485 | @@ -1 +1,2 @@ |
| 486 | hello world! |
| 487 | +hello world, again |
Junio C Hamano | 89d4e0f | 2007-02-18 00:34:59 | [diff] [blame] | 488 | $ git add file.txt |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 489 | $ git diff</tt></pre> |
| 490 | </div></div> |
| 491 | <p>The last diff is empty, but no new commits have been made, and the |
| 492 | head still doesn't contain the new line:</p> |
| 493 | <div class="listingblock"> |
| 494 | <div class="content"> |
| 495 | <pre><tt>$ git-diff HEAD |
| 496 | diff --git a/file.txt b/file.txt |
| 497 | index a042389..513feba 100644 |
| 498 | --- a/file.txt |
| 499 | +++ b/file.txt |
| 500 | @@ -1 +1,2 @@ |
| 501 | hello world! |
| 502 | +hello world, again</tt></pre> |
| 503 | </div></div> |
| 504 | <p>So "git diff" is comparing against something other than the head. |
| 505 | The thing that it's comparing against is actually the index file, |
| 506 | which is stored in .git/index in a binary format, but whose contents |
| 507 | we can examine with ls-files:</p> |
| 508 | <div class="listingblock"> |
| 509 | <div class="content"> |
| 510 | <pre><tt>$ git ls-files --stage |
| 511 | 100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt |
| 512 | $ git cat-file -t 513feba2 |
| 513 | blob |
| 514 | $ git cat-file blob 513feba2 |
Junio C Hamano | 8fd5230 | 2006-08-10 00:18:08 | [diff] [blame] | 515 | hello world! |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 516 | hello world, again</tt></pre> |
| 517 | </div></div> |
Junio C Hamano | 89d4e0f | 2007-02-18 00:34:59 | [diff] [blame] | 518 | <p>So what our "git add" did was store a new blob and then put |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 519 | a reference to it in the index file. If we modify the file again, |
| 520 | we'll see that the new modifications are reflected in the "git-diff" |
| 521 | output:</p> |
| 522 | <div class="listingblock"> |
| 523 | <div class="content"> |
| 524 | <pre><tt>$ echo 'again?' >>file.txt |
| 525 | $ git diff |
| 526 | index 513feba..ba3da7b 100644 |
| 527 | --- a/file.txt |
| 528 | +++ b/file.txt |
| 529 | @@ -1,2 +1,3 @@ |
| 530 | hello world! |
| 531 | hello world, again |
| 532 | +again?</tt></pre> |
| 533 | </div></div> |
| 534 | <p>With the right arguments, git diff can also show us the difference |
| 535 | between the working directory and the last commit, or between the |
| 536 | index and the last commit:</p> |
| 537 | <div class="listingblock"> |
| 538 | <div class="content"> |
| 539 | <pre><tt>$ git diff HEAD |
| 540 | diff --git a/file.txt b/file.txt |
| 541 | index a042389..ba3da7b 100644 |
| 542 | --- a/file.txt |
| 543 | +++ b/file.txt |
| 544 | @@ -1 +1,3 @@ |
| 545 | hello world! |
| 546 | +hello world, again |
| 547 | +again? |
| 548 | $ git diff --cached |
| 549 | diff --git a/file.txt b/file.txt |
| 550 | index a042389..513feba 100644 |
| 551 | --- a/file.txt |
| 552 | +++ b/file.txt |
| 553 | @@ -1 +1,2 @@ |
| 554 | hello world! |
| 555 | +hello world, again</tt></pre> |
| 556 | </div></div> |
| 557 | <p>At any time, we can create a new commit using "git commit" (without |
| 558 | the -a option), and verify that the state committed only includes the |
| 559 | changes stored in the index file, not the additional change that is |
| 560 | still only in our working tree:</p> |
| 561 | <div class="listingblock"> |
| 562 | <div class="content"> |
| 563 | <pre><tt>$ git commit -m "repeat" |
| 564 | $ git diff HEAD |
| 565 | diff --git a/file.txt b/file.txt |
| 566 | index 513feba..ba3da7b 100644 |
| 567 | --- a/file.txt |
| 568 | +++ b/file.txt |
| 569 | @@ -1,2 +1,3 @@ |
| 570 | hello world! |
| 571 | hello world, again |
| 572 | +again?</tt></pre> |
| 573 | </div></div> |
| 574 | <p>So by default "git commit" uses the index to create the commit, not |
| 575 | the working tree; the -a option to commit tells it to first update |
| 576 | the index with all changes in the working tree.</p> |
| 577 | <p>Finally, it's worth looking at the effect of "git add" on the index |
| 578 | file:</p> |
| 579 | <div class="listingblock"> |
| 580 | <div class="content"> |
| 581 | <pre><tt>$ echo "goodbye, world" >closing.txt |
| 582 | $ git add closing.txt</tt></pre> |
| 583 | </div></div> |
| 584 | <p>The effect of the "git add" was to add one entry to the index file:</p> |
| 585 | <div class="listingblock"> |
| 586 | <div class="content"> |
| 587 | <pre><tt>$ git ls-files --stage |
| 588 | 100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138 0 closing.txt |
| 589 | 100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt</tt></pre> |
| 590 | </div></div> |
| 591 | <p>And, as you can see with cat-file, this new entry refers to the |
| 592 | current contents of the file:</p> |
| 593 | <div class="listingblock"> |
| 594 | <div class="content"> |
Junio C Hamano | f65d928 | 2007-01-22 09:00:13 | [diff] [blame] | 595 | <pre><tt>$ git cat-file blob 8b9743b2 |
| 596 | goodbye, world</tt></pre> |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 597 | </div></div> |
| 598 | <p>The "status" command is a useful way to get a quick summary of the |
| 599 | situation:</p> |
| 600 | <div class="listingblock"> |
| 601 | <div class="content"> |
| 602 | <pre><tt>$ git status |
Junio C Hamano | 8a66a65 | 2007-02-03 08:07:35 | [diff] [blame] | 603 | # On branch master |
| 604 | # Changes to be committed: |
| 605 | # (use "git reset HEAD <file>..." to unstage) |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 606 | # |
| 607 | # new file: closing.txt |
| 608 | # |
Junio C Hamano | 8a66a65 | 2007-02-03 08:07:35 | [diff] [blame] | 609 | # Changed but not updated: |
| 610 | # (use "git add <file>..." to update what will be committed) |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 611 | # |
| 612 | # modified: file.txt |
| 613 | #</tt></pre> |
| 614 | </div></div> |
| 615 | <p>Since the current state of closing.txt is cached in the index file, |
Junio C Hamano | 8a66a65 | 2007-02-03 08:07:35 | [diff] [blame] | 616 | it is listed as "Changes to be committed". Since file.txt has |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 617 | changes in the working directory that aren't reflected in the index, |
Junio C Hamano | 8a66a65 | 2007-02-03 08:07:35 | [diff] [blame] | 618 | it is marked "changed but not updated". At this point, running "git |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 619 | commit" would create a commit that added closing.txt (with its new |
| 620 | contents), but that didn't modify file.txt.</p> |
| 621 | <p>Also, note that a bare "git diff" shows the changes to file.txt, but |
| 622 | not the addition of closing.txt, because the version of closing.txt |
| 623 | in the index file is identical to the one in the working directory.</p> |
| 624 | <p>In addition to being the staging area for new commits, the index file |
| 625 | is also populated from the object database when checking out a |
| 626 | branch, and is used to hold the trees involved in a merge operation. |
Junio C Hamano | fd73d89 | 2006-09-14 07:38:22 | [diff] [blame] | 627 | See the <a href="core-tutorial.html">core tutorial</a> and the relevant man |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 628 | pages for details.</p> |
| 629 | </div> |
| 630 | <h2>What next?</h2> |
| 631 | <div class="sectionbody"> |
| 632 | <p>At this point you should know everything necessary to read the man |
| 633 | pages for any of the git commands; one good place to start would be |
Junio C Hamano | 59929ee | 2006-05-22 22:34:54 | [diff] [blame] | 634 | with the commands mentioned in <a href="everyday.html">Everyday git</a>. You |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 635 | should be able to find any unknown jargon in the |
Junio C Hamano | bb8fb05 | 2006-05-30 07:21:12 | [diff] [blame] | 636 | <a href="glossary.html">Glossary</a>.</p> |
Junio C Hamano | ed7f4f6 | 2007-05-20 09:09:09 | [diff] [blame] | 637 | <p>The <a href="user-manual.html">Git User's Manual</a> provides a more |
| 638 | comprehensive introduction to git.</p> |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 639 | <p>The <a href="cvs-migration.html">CVS migration</a> document explains how to |
| 640 | import a CVS repository into git, and shows how to use git in a |
| 641 | CVS-like way.</p> |
| 642 | <p>For some interesting examples of git use, see the |
| 643 | <a href="howto-index.html">howtos</a>.</p> |
| 644 | <p>For git developers, the <a href="core-tutorial.html">Core tutorial</a> goes |
| 645 | into detail on the lower-level git mechanisms involved in, for |
| 646 | example, creating a new commit.</p> |
| 647 | </div> |
| 648 | <div id="footer"> |
| 649 | <div id="footer-text"> |
Junio C Hamano | 35738e8 | 2008-01-07 07:55:46 | [diff] [blame] | 650 | Last updated 07-Jan-2008 07:51:03 UTC |
Junio C Hamano | 6f8a790 | 2006-05-22 01:10:13 | [diff] [blame] | 651 | </div> |
| 652 | </div> |
| 653 | </body> |
| 654 | </html> |