table of contents
other versions
- jessie 1:2.1.4-2.1+deb8u6
- jessie-backports 1:2.11.0-3~bpo8+1
- stretch 1:2.11.0-3+deb9u4
- testing 1:2.20.1-2
- stretch-backports 1:2.20.1-1~bpo9+1
- unstable 1:2.20.1-2
- experimental 1:2.21.0+next.20190320-1
GITTUTORIAL-2(7) | Git Manual | GITTUTORIAL-2(7) |
NAME¶
gittutorial-2 - A tutorial introduction to Git: part twoSYNOPSIS¶
git *
DESCRIPTION¶
You should work through gittutorial(7) before reading this tutorial. The goal of this tutorial is to introduce two fundamental pieces of Git’s architecture—the object database and the index file—and to provide the reader with everything necessary to understand the rest of the Git documentation.THE GIT OBJECT DATABASE¶
Let’s start a new project and create a small amount of history:$ mkdir test-project $ cd test-project $ git init Initialized empty Git repository in .git/ $ echo 'hello world' > file.txt $ git add . $ git commit -a -m "initial commit" [master (root-commit) 54196cc] initial commit 1 file changed, 1 insertion(+) create mode 100644 file.txt $ echo 'hello world!' >file.txt $ git commit -a -m "add emphasis" [master c4d59f3] add emphasis 1 file changed, 1 insertion(+), 1 deletion(-)
$ git cat-file -t 54196cc2 commit $ git cat-file commit 54196cc2 tree 92b8b694ffb1675e5975148e1121810081dbdffe author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500 committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500 initial commit
$ git ls-tree 92b8b694 100644 blob 3b18e512dba79e4c8300dd08aeb37f8e728b8dad file.txt
$ git cat-file -t 3b18e512 blob
$ git cat-file blob 3b18e512 hello world
$ find .git/objects/ .git/objects/ .git/objects/pack .git/objects/info .git/objects/3b .git/objects/3b/18e512dba79e4c8300dd08aeb37f8e728b8dad .git/objects/92 .git/objects/92/b8b694ffb1675e5975148e1121810081dbdffe .git/objects/54 .git/objects/54/196cc2703dc165cbd373a65a4dcf22d50ae7f7 .git/objects/a0 .git/objects/a0/423896973644771497bdc03eb99d5281615b51 .git/objects/d0 .git/objects/d0/492b368b66bdabf2ac1fd8c92b39d3db916e59 .git/objects/c4 .git/objects/c4/d59f390b9cfd4318117afde11d601c1085f241
$ cat .git/HEAD ref: refs/heads/master
$ cat .git/refs/heads/master c4d59f390b9cfd4318117afde11d601c1085f241 $ git cat-file -t c4d59f39 commit $ git cat-file commit c4d59f39 tree d0492b368b66bdabf2ac1fd8c92b39d3db916e59 parent 54196cc2703dc165cbd373a65a4dcf22d50ae7f7 author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500 committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143418702 -0500 add emphasis
$ git ls-tree d0492b36 100644 blob a0423896973644771497bdc03eb99d5281615b51 file.txt $ git cat-file blob a0423896 hello world!
$ git cat-file commit 54196cc2 tree 92b8b694ffb1675e5975148e1121810081dbdffe author J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500 committer J. Bruce Fields <bfields@puzzle.fieldses.org> 1143414668 -0500 initial commit
•"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 they’re connected into the project history.
•"tree" objects represent the state of a
single directory, associating directory names to "blob" objects
containing file data and "tree" objects containing subdirectory
information.
•"blob" objects contain file data
without any other structure.
•References to commit objects at the head of each
branch are stored in files under .git/refs/heads/.
•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 ways—by 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 INDEX FILE¶
The primary tool we’ve been using to create commits is git-commit -a, which creates a commit including every change you’ve 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, we’ll see that there are more flexible ways creating commits. Continuing with our test-project, let’s modify file.txt again:$ echo "hello world, again" >>file.txt
$ git diff --- a/file.txt +++ b/file.txt @@ -1 +1,2 @@ hello world! +hello world, again $ git add file.txt $ git diff
$ 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
$ 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
$ 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?
$ 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
$ 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?
$ echo "goodbye, world" >closing.txt $ git add closing.txt
$ git ls-files --stage 100644 8b9743b20d4b15be3955fc8d5cd2b09cd2336138 0 closing.txt 100644 513feba2e53ebbd2532419ded848ba19de88ba00 0 file.txt
$ git cat-file blob 8b9743b2 goodbye, world
$ 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) # # modified: file.txt #
WHAT NEXT?¶
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 Everyday Git[1]. You should be able to find any unknown jargon in gitglossary(7). The Git User’s Manual[2] 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 howtos[3]. For Git developers, gitcore-tutorial(7) goes into detail on the lower-level Git mechanisms involved in, for example, creating a new commit.SEE ALSO¶
gittutorial(7), gitcvs-migration(7), gitcore-tutorial(7), gitglossary(7), git-help(1), Everyday Git[1], The Git User’s Manual[2]GIT¶
Part of the git(1) suite.NOTES¶
- 1.
- Everyday Git
- 2.
- Git User’s Manual
- 3.
- howtos
05/28/2018 | Git 2.1.4 |