Documentation / gittutorial-2.txton commit completion: Use consistent if [...] convention, not "test" (ad244d2)
   1gittutorial-2(7)
   2================
   3
   4NAME
   5----
   6gittutorial-2 - A tutorial introduction to git: part two
   7
   8SYNOPSIS
   9--------
  10git *
  11
  12DESCRIPTION
  13-----------
  14
  15You should work through linkgit:gittutorial[7] before reading this tutorial.
  16
  17The goal of this tutorial is to introduce two fundamental pieces of
  18git's architecture--the object database and the index file--and to
  19provide the reader with everything necessary to understand the rest
  20of the git documentation.
  21
  22The git object database
  23-----------------------
  24
  25Let's start a new project and create a small amount of history:
  26
  27------------------------------------------------
  28$ mkdir test-project
  29$ cd test-project
  30$ git init
  31Initialized empty Git repository in .git/
  32$ echo 'hello world' > file.txt
  33$ git add .
  34$ git commit -a -m "initial commit"
  35[master (root-commit) 54196cc] initial commit
  36 1 files changed, 1 insertions(+), 0 deletions(-)
  37 create mode 100644 file.txt
  38$ echo 'hello world!' >file.txt
  39$ git commit -a -m "add emphasis"
  40[master c4d59f3] add emphasis
  41 1 files changed, 1 insertions(+), 1 deletions(-)
  42------------------------------------------------
  43
  44What are the 7 digits of hex that git responded to the commit with?
  45
  46We saw in part one of the tutorial that commits have names like this.
  47It turns out that every object in the git history is stored under
  48a 40-digit hex name.  That name is the SHA1 hash of the object's
  49contents; among other things, this ensures that git will never store
  50the same data twice (since identical data is given an identical SHA1
  51name), and that the contents of a git object will never change (since
  52that would change the object's name as well). The 7 char hex strings
  53here are simply the abbreviation of such 40 character long strings.
  54Abbreviations can be used everywhere where the 40 character strings
  55can be used, so long as they are unambiguous.
  56
  57It is expected that the content of the commit object you created while
  58following the example above generates a different SHA1 hash than
  59the one shown above because the commit object records the time when
  60it was created and the name of the person performing the commit.
  61
  62We can ask git about this particular object with the `cat-file`
  63command. Don't copy the 40 hex digits from this example but use those
  64from your own version. Note that you can shorten it to only a few
  65characters to save yourself typing all 40 hex digits:
  66
  67------------------------------------------------
  68$ git cat-file -t 54196cc2
  69commit
  70$ git cat-file commit 54196cc2
  71tree 92b8b694ffb1675e5975148e1121810081dbdffe
  72author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
  73committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
  74
  75initial commit
  76------------------------------------------------
  77
  78A tree can refer to one or more "blob" objects, each corresponding to
  79a file.  In addition, a tree can also refer to other tree objects,
  80thus creating a directory hierarchy.  You can examine the contents of
  81any tree using ls-tree (remember that a long enough initial portion
  82of the SHA1 will also work):
  83
  84------------------------------------------------
  85$ git ls-tree 92b8b694
  86100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad    file.txt
  87------------------------------------------------
  88
  89Thus we see that this tree has one file in it.  The SHA1 hash is a
  90reference to that file's data:
  91
  92------------------------------------------------
  93$ git cat-file -t 3b18e512
  94blob
  95------------------------------------------------
  96
  97A "blob" is just file data, which we can also examine with cat-file:
  98
  99------------------------------------------------
 100$ git cat-file blob 3b18e512
 101hello world
 102------------------------------------------------
 103
 104Note that this is the old file data; so the object that git named in
 105its response to the initial tree was a tree with a snapshot of the
 106directory state that was recorded by the first commit.
 107
 108All of these objects are stored under their SHA1 names inside the git
 109directory:
 110
 111------------------------------------------------
 112$ find .git/objects/
 113.git/objects/
 114.git/objects/pack
 115.git/objects/info
 116.git/objects/3b
 117.git/objects/3b/18e512dba79e4c8300dd08aeb37f8e728b8dad
 118.git/objects/92
 119.git/objects/92/b8b694ffb1675e5975148e1121810081dbdffe
 120.git/objects/54
 121.git/objects/54/196cc2703dc165cbd373a65a4dcf22d50ae7f7
 122.git/objects/a0
 123.git/objects/a0/423896973644771497bdc03eb99d5281615b51
 124.git/objects/d0
 125.git/objects/d0/492b368b66bdabf2ac1fd8c92b39d3db916e59
 126.git/objects/c4
 127.git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241
 128------------------------------------------------
 129
 130and the contents of these files is just the compressed data plus a
 131header identifying their length and their type.  The type is either a
 132blob, a tree, a commit, or a tag.
 133
 134The simplest commit to find is the HEAD commit, which we can find
 135from .git/HEAD:
 136
 137------------------------------------------------
 138$ cat .git/HEAD
 139ref: refs/heads/master
 140------------------------------------------------
 141
 142As you can see, this tells us which branch we're currently on, and it
 143tells us this by naming a file under the .git directory, which itself
 144contains a SHA1 name referring to a commit object, which we can
 145examine with cat-file:
 146
 147------------------------------------------------
 148$ cat .git/refs/heads/master
 149c4d59f390b9cfd4318117afde11d601c1085f241
 150$ git cat-file -t c4d59f39
 151commit
 152$ git cat-file commit c4d59f39
 153tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59
 154parent 54196cc2703dc165cbd373a65a4dcf22d50ae7f7
 155author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500
 156committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500
 157
 158add emphasis
 159------------------------------------------------
 160
 161The "tree" object here refers to the new state of the tree:
 162
 163------------------------------------------------
 164$ git ls-tree d0492b36
 165100644 blob a0423896973644771497bdc03eb99d5281615b51    file.txt
 166$ git cat-file blob a0423896
 167hello world!
 168------------------------------------------------
 169
 170and the "parent" object refers to the previous commit:
 171
 172------------------------------------------------
 173$ git cat-file commit 54196cc2
 174tree 92b8b694ffb1675e5975148e1121810081dbdffe
 175author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
 176committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500
 177
 178initial commit
 179------------------------------------------------
 180
 181The tree object is the tree we examined first, and this commit is
 182unusual in that it lacks any parent.
 183
 184Most commits have only one parent, but it is also common for a commit
 185to have multiple parents.   In that case the commit represents a
 186merge, with the parent references pointing to the heads of the merged
 187branches.
 188
 189Besides blobs, trees, and commits, the only remaining type of object
 190is a "tag", which we won't discuss here; refer to linkgit:git-tag[1]
 191for details.
 192
 193So now we know how git uses the object database to represent a
 194project's history:
 195
 196  * "commit" objects refer to "tree" objects representing the
 197    snapshot of a directory tree at a particular point in the
 198    history, and refer to "parent" commits to show how they're
 199    connected into the project history.
 200  * "tree" objects represent the state of a single directory,
 201    associating directory names to "blob" objects containing file
 202    data and "tree" objects containing subdirectory information.
 203  * "blob" objects contain file data without any other structure.
 204  * References to commit objects at the head of each branch are
 205    stored in files under .git/refs/heads/.
 206  * The name of the current branch is stored in .git/HEAD.
 207
 208Note, by the way, that lots of commands take a tree as an argument.
 209But as we can see above, a tree can be referred to in many different
 210ways--by the SHA1 name for that tree, by the name of a commit that
 211refers to the tree, by the name of a branch whose head refers to that
 212tree, etc.--and most such commands can accept any of these names.
 213
 214In command synopses, the word "tree-ish" is sometimes used to
 215designate such an argument.
 216
 217The index file
 218--------------
 219
 220The primary tool we've been using to create commits is `git-commit
 221-a`, which creates a commit including every change you've made to
 222your working tree.  But what if you want to commit changes only to
 223certain files?  Or only certain changes to certain files?
 224
 225If we look at the way commits are created under the cover, we'll see
 226that there are more flexible ways creating commits.
 227
 228Continuing with our test-project, let's modify file.txt again:
 229
 230------------------------------------------------
 231$ echo "hello world, again" >>file.txt
 232------------------------------------------------
 233
 234but this time instead of immediately making the commit, let's take an
 235intermediate step, and ask for diffs along the way to keep track of
 236what's happening:
 237
 238------------------------------------------------
 239$ git diff
 240--- a/file.txt
 241+++ b/file.txt
 242@@ -1 +1,2 @@
 243 hello world!
 244+hello world, again
 245$ git add file.txt
 246$ git diff
 247------------------------------------------------
 248
 249The last diff is empty, but no new commits have been made, and the
 250head still doesn't contain the new line:
 251
 252------------------------------------------------
 253$ git diff HEAD
 254diff --git a/file.txt b/file.txt
 255index a042389..513feba 100644
 256--- a/file.txt
 257+++ b/file.txt
 258@@ -1 +1,2 @@
 259 hello world!
 260+hello world, again
 261------------------------------------------------
 262
 263So 'git-diff' is comparing against something other than the head.
 264The thing that it's comparing against is actually the index file,
 265which is stored in .git/index in a binary format, but whose contents
 266we can examine with ls-files:
 267
 268------------------------------------------------
 269$ git ls-files --stage
 270100644 513feba2e53ebbd2532419ded848ba19de88ba00 0       file.txt
 271$ git cat-file -t 513feba2
 272blob
 273$ git cat-file blob 513feba2
 274hello world!
 275hello world, again
 276------------------------------------------------
 277
 278So what our 'git-add' did was store a new blob and then put
 279a reference to it in the index file.  If we modify the file again,
 280we'll see that the new modifications are reflected in the 'git-diff'
 281output:
 282
 283------------------------------------------------
 284$ echo 'again?' >>file.txt
 285$ git diff
 286index 513feba..ba3da7b 100644
 287--- a/file.txt
 288+++ b/file.txt
 289@@ -1,2 +1,3 @@
 290 hello world!
 291 hello world, again
 292+again?
 293------------------------------------------------
 294
 295With the right arguments, 'git-diff' can also show us the difference
 296between the working directory and the last commit, or between the
 297index and the last commit:
 298
 299------------------------------------------------
 300$ git diff HEAD
 301diff --git a/file.txt b/file.txt
 302index a042389..ba3da7b 100644
 303--- a/file.txt
 304+++ b/file.txt
 305@@ -1 +1,3 @@
 306 hello world!
 307+hello world, again
 308+again?
 309$ git diff --cached
 310diff --git a/file.txt b/file.txt
 311index a042389..513feba 100644
 312--- a/file.txt
 313+++ b/file.txt
 314@@ -1 +1,2 @@
 315 hello world!
 316+hello world, again
 317------------------------------------------------
 318
 319At any time, we can create a new commit using 'git-commit' (without
 320the "-a" option), and verify that the state committed only includes the
 321changes stored in the index file, not the additional change that is
 322still only in our working tree:
 323
 324------------------------------------------------
 325$ git commit -m "repeat"
 326$ git diff HEAD
 327diff --git a/file.txt b/file.txt
 328index 513feba..ba3da7b 100644
 329--- a/file.txt
 330+++ b/file.txt
 331@@ -1,2 +1,3 @@
 332 hello world!
 333 hello world, again
 334+again?
 335------------------------------------------------
 336
 337So by default 'git-commit' uses the index to create the commit, not
 338the working tree; the "-a" option to commit tells it to first update
 339the index with all changes in the working tree.
 340
 341Finally, it's worth looking at the effect of 'git-add' on the index
 342file:
 343
 344------------------------------------------------
 345$ echo "goodbye, world" >closing.txt
 346$ git add closing.txt
 347------------------------------------------------
 348
 349The effect of the 'git-add' was to add one entry to the index file:
 350
 351------------------------------------------------
 352$ git ls-files --stage
 353100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138 0       closing.txt
 354100644 513feba2e53ebbd2532419ded848ba19de88ba00 0       file.txt
 355------------------------------------------------
 356
 357And, as you can see with cat-file, this new entry refers to the
 358current contents of the file:
 359
 360------------------------------------------------
 361$ git cat-file blob 8b9743b2
 362goodbye, world
 363------------------------------------------------
 364
 365The "status" command is a useful way to get a quick summary of the
 366situation:
 367
 368------------------------------------------------
 369$ git status
 370# On branch master
 371# Changes to be committed:
 372#   (use "git reset HEAD <file>..." to unstage)
 373#
 374#       new file: closing.txt
 375#
 376# Changed but not updated:
 377#   (use "git add <file>..." to update what will be committed)
 378#
 379#       modified: file.txt
 380#
 381------------------------------------------------
 382
 383Since the current state of closing.txt is cached in the index file,
 384it is listed as "Changes to be committed".  Since file.txt has
 385changes in the working directory that aren't reflected in the index,
 386it is marked "changed but not updated".  At this point, running "git
 387commit" would create a commit that added closing.txt (with its new
 388contents), but that didn't modify file.txt.
 389
 390Also, note that a bare `git diff` shows the changes to file.txt, but
 391not the addition of closing.txt, because the version of closing.txt
 392in the index file is identical to the one in the working directory.
 393
 394In addition to being the staging area for new commits, the index file
 395is also populated from the object database when checking out a
 396branch, and is used to hold the trees involved in a merge operation.
 397See linkgit:gitcore-tutorial[7] and the relevant man
 398pages for details.
 399
 400What next?
 401----------
 402
 403At this point you should know everything necessary to read the man
 404pages for any of the git commands; one good place to start would be
 405with the commands mentioned in link:everyday.html[Everyday git].  You
 406should be able to find any unknown jargon in linkgit:gitglossary[7].
 407
 408The link:user-manual.html[Git User's Manual] provides a more
 409comprehensive introduction to git.
 410
 411linkgit:gitcvs-migration[7] explains how to
 412import a CVS repository into git, and shows how to use git in a
 413CVS-like way.
 414
 415For some interesting examples of git use, see the
 416link:howto-index.html[howtos].
 417
 418For git developers, linkgit:gitcore-tutorial[7] goes
 419into detail on the lower-level git mechanisms involved in, for
 420example, creating a new commit.
 421
 422SEE ALSO
 423--------
 424linkgit:gittutorial[7],
 425linkgit:gitcvs-migration[7],
 426linkgit:gitcore-tutorial[7],
 427linkgit:gitglossary[7],
 428linkgit:git-help[1],
 429link:everyday.html[Everyday git],
 430link:user-manual.html[The Git User's Manual]
 431
 432GIT
 433---
 434Part of the linkgit:git[1] suite.