LoginSignup
16
11

More than 5 years have passed since last update.

Gitオブジェクトにはどんなものがあるのか知りたい

Last updated at Posted at 2015-09-14

自分の研究でGitのちょっと深いところまでやる必要がでたのでやってみた.

参考にしたサイト

見えないチカラ
Gitの中身を見てみよう
Gitオブジェクト
こせきの技術日記

用語整理

用語 説明
repository コミットの集合
the index ステージングエリアの方が馴染み深い?
working tree ファイルやディレクトリが含まれているもの(全体像的な感じ)
commit ある時点でのworking treeのスナップショット

見えないチカラを参考に記述.
参考ページにはもう少し詳しくかいてある.

Git

Gitは誤解を恐れずに言うとなんでもハッシュ化して管理する.

なので参照するのは全てその文字列なわけだが,その文字列にもいくつか形式のようなものが存在するのでそれを説明していく.

blob

これはファイルを表す.tree構造の末端を表すといった方がいいのかな.

とりあえず,チュートリアル的なものをやってみる.

$ mkdir GitTest
$ cd GitTest
$ echo 'hello, world' > Hello.txt
$ cat Hello.txt
hello, world

「hello, world」と記述されたHello.txtを作成した.

ここでこのHello.txtはどのようなハッシュ値になるのかを確認.

$ git hash-object Hello.txt
4b5fa63702dd96796042e92787f464e28f09f17d

これはhello, worldという内容をhash化したものであるため.誰がやっても同じハッシュ値なるはず.

$ git init
Initialized empty Git repository in /Users/shu920921/Documents/GitTest/.git/

$ git add Hello.txt
$ git commit -m "add Hello.txt"
[master (root-commit) 10a3496] add Hello.txt
 1 file changed, 1 insertion(+)
 create mode 100644 Hello.txt

先ほどのハッシュ値を打ち込んで,そのハッシュ値はどのような形式のものなのか確かめる.git cat-file -t

$ git cat-file -t 4b5fa637
blob

blobは先ほども述べた通り,treeの末端,すなわちファイルである.

ハッシュ値からファイルの中身もみれる

$ git cat-file blob 4b5fa637
hello, world

tree

treeとはblobを管理するものだ.
blobがファイルであるなら,treeはディレクトリといったところだと思う.

とりあえず適当にディレクトリとファイルを追加.
以下のような構造にした.

GitTest
├── Hello.txt
└── testdir
    └── test.txt

masterブランチのツリーオブジェクトを確認してみる.

$ git cat-file -p master^{tree}
100644 blob 4b5fa63702dd96796042e92787f464e28f09f17d    Hello.txt
040000 tree 85a2934a71f7385034f934f9ccc8c746b73d4f44    testdir

testdirツリー(ディレクトリ)の中身を見る.

$ git cat-file -p 85a2934a7
100644 blob 16b14f5da9e2fcd6f3f38cc9e584cef2f3c90ebe    test.txt

test.txtが存在することが確認できた.

先述の通り,git cat-file blob 16b14f5da9e2 で中身を確認できる.

commit

commitオブジェクトは名の通りコミットに関する情報を保持しているオブジェクト

現在は2回commitしたはず.

とりあえずgit logで確認する.

$ git log
commit c8b175a310412696c0804a496eeac5d9ce73d88c
Author: shu- <アドレス>
Date:   Tue Sep 15 00:32:11 2015 +0900

    add test dir and file

commit 10a3496af239cfd466028f2a9132d78ee44bb63d
Author: shu- <アドレス>
Date:   Sat Sep 12 17:43:57 2015 +0900

    add Hello.txt

うん.2個コミット情報があった.

もし,コミットのハッシュ値がわかる場合は git cat-file commit ハッシュ値で確認.

$ git cat-file commit c8b175a3104
tree 62e8e0173bc99f7e0704e537b41ac0d7d645a734
parent 10a3496af239cfd466028f2a9132d78ee44bb63d
author shu- <アドレス> 1442244731 +0900
committer shu- <アドレス> 1442244731 +0900

add test dir and file

とりあえず今回はこんなところ
gitオブジェクトにはどのようなオブジェクトがあるのか少しわかった.
それとハッシュ値で全て管理されてることもわかった.

16
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
11