0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

HEAD, ブランチ, index, blobとは?.git/の中を覗く

0
Last updated at Posted at 2026-08-24

要約

Git で実体を持つのは objects/ の blob だけ。HEAD・ブランチ・commit・tree・index はすべてそこを指すポインタ。

目次

HEAD・ブランチ・インデックスはポインタ

image.png

上から HEAD → ブランチ → commit → tree → blob、そこに index が横から blob を掴んでいます。

データ本体を持つのは objects/ の blob だけです。HEAD もブランチも index も、そこを指すポインタにすぎません。


HEAD とブランチはポインタ

image.png

  • HEAD はブランチを指すテキスト
  • ブランチは commit のハッシュ

commit は tree を介して blob を指す

image.png

  • b (blob) = ファイルの中身。ファイル名は持たない
  • t (tree) = ディレクトリ。「名前 → blob/tree」の対応表
  • c (commit) = tree 1つ + 親 + メタ情報

commit が直接持っているのは tree ひとつだけで、ファイルの中身はその先の blob にあります。
commit はスナップショット、つまり差分ではなく全体の状態を指しますが、その状態を記述するのは tree であって、commit がファイルのコピーを抱えているわけではありません。

gitオブジェクトの名前は内容から決まります。内容が同じなら同じ名前、つまり同じ実体を指します。


index も blob を指しているだけ

image.png

index が持つのは「パス → blob ハッシュ」の対応表です。

index はすでに objects/ の中の blob を指しています。

git add     → blob を objects/ に作り、index に登録
git commit  → index から tree を組み立て、commit を作り、ブランチの指し先を進める

図の右下、tree からの矢印が無く index からだけ矢印が来ている blob が、add 済み・未コミットの状態です。


まとめ

  • HEAD / ブランチ = ポインタ。データは持たない
  • commit = tree 1つを指すだけ。ファイルの中身は tree の先の blob
  • blob = 唯一データを持つ実体。内容が同じなら共有される
  • index = パスと blob の一覧。tree を介さず blob を直接指す
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?