Skip to content
Naoya Tech
Go back

How Git Works Internally: Blobs, Trees, Commits and Refs

13:53 Programming & CS

Overview

This introductory video explains what happens behind everyday commands such as git add and git commit. Rather than just a version control system, it describes Git as a huge key-value store for tracking content. The key is a 40-character SHA-1 hash computed from the content itself: the same content always gives the same key, and changing one character gives a completely different one, as running git hash-object on “hello” shows.

Key Points

Chapters

概要

この入門動画では、git addやgit commitといった毎日使うコマンドの裏側で何が起きているのかを解説します。動画ではGitを単なるバージョン管理システムではなく、コンテンツを追跡するための巨大なキーバリューストアとして説明します。キーは中身から計算される40文字のSHA-1ハッシュで、同じ中身からは必ず同じキーが生まれ、1文字でも変わればまったく別のキーになることを、「hello」をgit hash-objectに渡して確かめます。

ポイント

  • Gitが保存するオブジェクトは3種類です。ブロブはファイルの中身だけを持ち、名前すら持ちません。ツリーは名前、権限、種類とハッシュを対応づけ、別のツリーを指すこともできます。コミットはプロジェクト全体のスナップショットである1つのツリーを指し、作者、日時、メッセージ、親コミットを持ちます。歴史とは、このコミットの鎖のことです。
  • .git/objectsでは、1つのフォルダーが大きくなりすぎないよう、ハッシュの最初の2文字がフォルダー名、残りの38文字がファイル名になっています。
  • git addはファイルをワーキングディレクトリからステージングエリア(インデックス)へ移し、git commitはそれをスナップショットとしてリポジトリに保存します。
  • ブランチは、コミットのハッシュを1つ書いただけのrefs/heads以下の小さなファイルなので、一瞬で作れます。HEADはふつうコミットではなくブランチを指し、タグは動かない参照です。
  • ブロブが名前を持たないので、中身が同じファイルは1つのブロブを共有し、変更のないファイルは既存のブロブを指し続けます。これがGitの効率の良さの源です。

チャプター

Watch on YouTube

Share this post on:

Previous Video
Automating an OSS Vulnerability Feed with Notion Workers
Next Video
How Memory Works: The Stack, the Heap and Pointers