|o||"commit" objects refer to "tree" objects representing the snapshot of a directory tree at a particular point in the history, and refer to "parent" commits to show how theyre connected into the project history.|
o "tree" objects represent the state of a single directory, associating directory names to "blob" objects containing file data and "tree" objects containing subdirectory information.
o "blob" objects contain file data without any other structure.
o References to commit objects at the head of each branch are stored in files under .git/refs/heads/.
o The name of the current branch is stored in .git/HEAD.
Note, by the way, that lots of commands take a tree as an argument. But as we can see above, a tree can be referred to in many different waysby the SHA-1 name for that tree, by the name of a commit that refers to the tree, by the name of a branch whose head refers to that tree, etc.--and most such commands can accept any of these names.
In command synopses, the word "tree-ish" is sometimes used to designate such an argument.
The primary tool weve been using to create commits is git-commit -a, which creates a commit including every change youve made to your working tree. But what if you want to commit changes only to certain files? Or only certain changes to certain files?
If we look at the way commits are created under the cover, well see that there are more flexible ways creating commits.
Continuing with our test-project, lets modify file.txt again:
$ echo "hello world, again" >>file.txt
but this time instead of immediately making the commit, lets take an intermediate step, and ask for diffs along the way to keep track of whats happening:
$ git diff --- a/file.txt +++ b/file.txt @@ -1 +1,2 @@ hello world! +hello world, again $ git add file.txt $ git diff
The last diff is empty, but no new commits have been made, and the head still doesnt contain the new line:
$ git diff HEAD diff --git a/file.txt b/file.txt index a042389..513feba 100644 --- a/file.txt +++ b/file.txt @@ -1 +1,2 @@ hello world! +hello world, again
So git diff is comparing against something other than the head. The thing that its comparing against is actually the index file, which is stored in .git/index in a binary format, but whose contents we can examine with ls-files:
$ git ls-files --stage 100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt $ git cat-file -t 513feba2 blob $ git cat-file blob 513feba2 hello world! hello world, again
So what our git add did was store a new blob and then put a reference to it in the index file. If we modify the file again, well see that the new modifications are reflected in the git diff output:
$ echo again? >>file.txt $ git diff index 513feba..ba3da7b 100644 --- a/file.txt +++ b/file.txt @@ -1,2 +1,3 @@ hello world! hello world, again +again?
With the right arguments, git diff can also show us the difference between the working directory and the last commit, or between the index and the last commit:
$ git diff HEAD diff --git a/file.txt b/file.txt index a042389..ba3da7b 100644 --- a/file.txt +++ b/file.txt @@ -1 +1,3 @@ hello world! +hello world, again +again? $ git diff --cached diff --git a/file.txt b/file.txt index a042389..513feba 100644 --- a/file.txt +++ b/file.txt @@ -1 +1,2 @@ hello world! +hello world, again
At any time, we can create a new commit using git commit (without the "-a" option), and verify that the state committed only includes the changes stored in the index file, not the additional change that is still only in our working tree:
$ git commit -m "repeat" $ git diff HEAD diff --git a/file.txt b/file.txt index 513feba..ba3da7b 100644 --- a/file.txt +++ b/file.txt @@ -1,2 +1,3 @@ hello world! hello world, again +again?
So by default git commit uses the index to create the commit, not the working tree; the "-a" option to commit tells it to first update the index with all changes in the working tree.
Finally, its worth looking at the effect of git add on the index file:
$ echo "goodbye, world" >closing.txt $ git add closing.txt
The effect of the git add was to add one entry to the index file:
$ git ls-files --stage 100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138 0 closing.txt 100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt
And, as you can see with cat-file, this new entry refers to the current contents of the file:
$ git cat-file blob 8b9743b2 goodbye, world
The "status" command is a useful way to get a quick summary of the situation:
$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage)
new file: closing.txt
Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory)
Since the current state of closing.txt is cached in the index file, it is listed as "Changes to be committed". Since file.txt has changes in the working directory that arent reflected in the index, it is marked "changed but not updated". At this point, running "git commit" would create a commit that added closing.txt (with its new contents), but that didnt modify file.txt.
Also, note that a bare git diff shows the changes to file.txt, but not the addition of closing.txt, because the version of closing.txt in the index file is identical to the one in the working directory.
In addition to being the staging area for new commits, the index file is also populated from the object database when checking out a branch, and is used to hold the trees involved in a merge operation. See gitcore-tutorial(7) and the relevant man pages for details.
At this point you should know everything necessary to read the man pages for any of the git commands; one good place to start would be with the commands mentioned in giteveryday(7). You should be able to find any unknown jargon in gitglossary(7).
The [blue]Git Users Manual provides a more comprehensive introduction to Git.
gitcvs-migration(7) explains how to import a CVS repository into Git, and shows how to use Git in a CVS-like way.
For some interesting examples of Git use, see the [blue]howtos.
For Git developers, gitcore-tutorial(7) goes into detail on the lower-level Git mechanisms involved in, for example, creating a new commit.
gittutorial(7), gitcvs-migration(7), gitcore-tutorial(7), gitglossary(7), git-help(1), giteveryday(7), [blue]The Git Users Manual
Part of the git(1) suite.
1. Git Users Manual git-htmldocs/user-manual.html 2. howtos git-htmldocs/howto-index.html
|Git 2&.7&.4||GITTUTORIAL-2 (7)||03/17/2016|