Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Git再入門
Search
fhiyo
April 14, 2023
Programming
160
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Git再入門
fhiyo
April 14, 2023
More Decks by fhiyo
See All by fhiyo
Security_Engineering___Third_Edition_Chapter.20.pdf
fhiyo
0
34
Security_Engineering___Third_Edition_Chapter.21.pdf
fhiyo
0
35
効果検証入門1章
fhiyo
1
550
言語処理のための機械学習入門 1.1〜1.4
fhiyo
0
97
オプトにおける自然言語生成の応用事例
fhiyo
6
790
【論文紹介】Forecasting at Scale
fhiyo
1
730
統計的因果探索に入門してみた
fhiyo
0
560
【論文紹介】Deep Inside Convolutional Networks Visualising Image Classification Models and Saliency Maps -- Simonyan Vedaldi Zisserman 2013 in ArXiv.pdf
fhiyo
0
1.6k
Other Decks in Programming
See All in Programming
仕様書を書く前にハーネスを作る - Agent Native開発は「探索を速く、判定を固く」
gotalab555
4
1.6k
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
210
Jindong: Introducing Declarative Haptics in Compose Multiplatform
l2hyunwoo
0
110
改善しないと、タスクが回らない。 “てんこ盛りポジション” を引き継いだ情シスの、入社3ヶ月の業務改善録
krm963
0
260
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
220
FDEが実現するAI駆動経営の現在地
gonta
2
280
ビデオ通話が繋がる0.2秒で何が起きているのか
supurazako
2
180
Laravelで学ぶ Webアプリケーションチューニング入門/web_application_tuning_101
hanhan1978
4
1.7k
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
500
プロポーザルを書いてもらう
pvcresin
0
310
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
220
PostgreSQL 18で考えるUUID主キー
kazuhiro1982
0
470
Featured
See All Featured
GitHub's CSS Performance
jonrohan
1033
470k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
460
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
200
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4.2k
A Tale of Four Properties
chriscoyier
163
24k
Navigating Team Friction
lara
192
16k
The browser strikes back
jonoalderson
0
1.5k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
370
Unsuck your backbone
ammeep
672
58k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Transcript
Git再入門 @fhiyo 1
概要 - 使い方よりも概念や内部構造に焦点を当てて話す - 想定レベル: Gitでinit, add, commit, checkoutなど基本的なコマンドを使ったことが ある、もしくは使い方を知っている
2
ファイルシステムとしてのGit Git is a content-addressable filesystem. (Git Internals - Git
Objects) - content-addressable なファイルシステム - 名前やpathを自由につけるのではなく、ファイルの内容に基づいてアドレスを計算し情報を格納す る - ファイルの内容が同一なら必ず同じアドレスになる - 情報を取り出せる - コミットというオブジェクトからある時点でのディレクトリの状態を復元できる - セキュリティ的な利点がある (ファイルの中身を変えるとアドレスも変わる ) 3
用語 - ワーキングツリー (ワーキングディレクトリ): Gitの管理対象にあるディレクトリ - インデックス (ステージングエリア): 次にコミットする内容を格納したファイル -
リポジトリ: .gitディレクトリのこと - Gitオブジェクト: 管理対象のファイルやディレクトリなどのGit内部での表現 ※ 正確な定義が分からなかったのであくまで自分の理解 4
用語 5 インデックス リポジトリ ワーキングツリー Gitオブジェクト
ファイルの管理方法 - 以下のGitオブジェクトと呼ばれるオブジェクトで管理される - Blob: ファイルの内容を格納 - Tree: ディレクトリの内容を格納 -
Commit: ワーキングツリーのルートを表す Treeへの参照 - Tag: Commitへのポインタ - (他にもあるが割愛) - Gitオブジェクトはzlib形式で圧縮→ .git/objects/ 以下にシリアライズしたGitオブ ジェクトをハッシュ化 (デフォルトの関数はSHA-1) した値をパスにして格納 - ハッシュ値がアドレスに対応する - CommitだけでなくBlobやTree, Tagも全部同じ方法で一緒くたに管理している 6
デモしながら説明 7
Blob 8 - ファイルをzlib圧縮してヘッダをつけたバイナリファイル - ファイル名は無関係。内容が等しければ別ファイルでも同じ Blobオブジェクトになる - 新しいコミットを追加しても、変更のない Blobはそのまま
→ストレージの節約になる "blob"<スペース><contentsのサイズ><ヌル文字><contents> ヘッダ (他のGitオブジェクトもほぼ同じ ) ボディ 圧縮前のフォーマット
Tree - 直下にあるファイルのGitオブジェクトの情報を格納する - (パーミッション, 種別, ハッシュ値, ファイル名) の情報を各エントリに対して持つ -
自身のハッシュ値はヘッダと各エントリの値を連結した文字列をハッシュ化して計算 9 $ git rev-parse HEAD^{tree} f149947b1322f06452ae580b103f7983f0bf6351 $ git cat-file -p f149947b1322f06452ae580b103f7983f0bf6351 040000 tree 556b6843a6a6a41572ad4e7371c09c743ae07a5e .circleci 100644 blob 3c6065443b72fb277382f6b6baad9233ee3b2f86 README.md 040000 tree 6401e939617ea7007eb97a21f170a28e58d5c883 front 040000 tree f3ac12bd3fc42f062bc581b9096349ed11c90908 infra 040000 tree 00eb0847a1bcdaec39901c8f9b80e6d1192188c1 rails ... ファイルパーミッション (的なもの) オブジェクトの種類 ハッシュ値 ファイル名
変更されたファイルの検出 - 配下にあるファイルのハッシュが変更されたらTreeのハッシュも変わる - ハッシュ値に変化がないノードから下は見なくてよいので楽 10 / /dir2 /dir1/a.txt /dir2/b.txt
/dir2/c.txt 9e45451 60b70ed 69665dc ae772eb 943a16f /dir1 fe723bb /dir1/a.txt を編集 / /dir2 /dir1/a.txt /dir2/b.txt /dir2/c.txt 4cb9112 69665dc ae772eb 943a16f /dir1 323d00c 5eae772 ←skip! ※ 子のハッシュ値を使って親のハッシュ値を計算する木構造をマークル木という
Commit - ある時点におけるワーキングツリーのスナップショット - ルートツリーを指す (ルートツリーのハッシュ値を含む) - 「親 (一つ前) のコミット」を指すことで変更の履歴を作る
- バージョン管理システムとしての機能の核となる部分 - 自身のハッシュ値はルート Treeや親コミットのハッシュ値などを連結した文字列をハッシュ化して計 算 11 $ git rev-parse HEAD 4c6bf5c276d3b9e8c61a6254a7ba9fce04c57833 $ git cat-file -p 4c6bf5c276d3b9e8c61a6254a7ba9fce04c57833 tree d8e4cb183020293add52b2d3498c301f1e6d54a5 parent 4cb9112c8ad1d7588cfdd43c0c373d2ceb1e5c87 author fhiyo <
[email protected]
> 1681263933 +0900 committer fhiyo <
[email protected]
> 1681263993 +0900 add hogehoge file ルートTreeのハッシュ値 親コミットのハッシュ値 author, コミッタの情報 コミットメッセージ
バージョンの管理 12 / /dir /a.txt /dir/b.txt /dir/c.txt : Commit :
Tree : Blob ※ / はワーキングツリーのルートを表す 059e454 9e45451 60b70ed 69665dc ae772eb 943a16f main HEAD $ git add a.txt $ git add dir/{b.txt,c.txt} $ git commit -m "first commit"
バージョンの管理 13 / / /dir /a.txt /dir/b.txt /dir/c.txt /dir/c.txt :
Commit : Tree : Blob /dir ※ / はワーキングツリーのルートを表す 059e454 11db2f9 9e45451 db2f972 60b70ed 69665dc 40d9892 ae772eb 943a16f d27669a main HEAD # dir/c.txt を編集 $ git add dir/c.txt $ git commit -m "update dir/c.txt"
バージョンの管理 14 ︙ … / / /dir /a.txt /dir/b.txt /dir/c.txt
/dir/c.txt : Commit : Tree : Blob /dir ※ / はワーキングツリーのルートを表す 059e454 11db2f9 aa849b3 9e45451 db2f972 60b70ed 69665dc 40d9892 ae772eb 943a16f d27669a
git cherry-pick は差分を計算して適用している - コミットが差分だと思っていると挙動を勘違いする - コミットをコピーしたわけではなく親との差分が同じになるように調整しているだけ 15 / /
/a.txt 059e454 11db2f9 9e45451 db2f972 60b70ed /a.txt e320368 aaa aaa bbbbbb / / /a.txt a423b63 /a.txt a aaa a aaa bbbbbb … … a4b5d9a 5922f22 abb130d b75c84c 1cd6e72 git cherry-pick 11db2f9 で左右の差分が全く同じになるよう に新しいコミットができる 親が複数いるときは -m <parent-number> で対象の親 を指定できる HEAD
参考 - https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain - コミットはスナップショットであり差分ではない - GitHubブログ 16