understanding git internals
DESCRIPTION
This is a quick introduction to the inner workings of Git, largely based on what I learned from reading Git Internals (https://peepcode.com/products/git-internals-pdf) and experimentation. It covers Git objects (blob, tree, commit, tag), references, and branches with incrementally building diagrams to help convey the concepts.TRANSCRIPT
![Page 1: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/1.jpg)
GitJeff Kunkle
Understanding
January 6, 2010
![Page 2: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/2.jpg)
Git is a free & open source, distributed version control system designed to handle everything from small to very
large projects with speed and efficiency.
Every Git clone is a full-fledged repository with complete history and full revision tracking capabilities, not dependent
on network access or a central server. Branching and merging are fast and easy to do.
What’s Git?
![Page 3: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/3.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory
![Page 4: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/4.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory
>git init
.git
![Page 5: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/5.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory
>git init
What’s in here?.git
![Page 6: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/6.jpg)
Objects
![Page 7: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/7.jpg)
Git Objects
header
content
object_type [content size]\0content of the filecan be textcan be binarycan be whatever
Zlib::Deflate
![Page 8: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/8.jpg)
Git Objects
1 Blob 2 Tree 3 Commit 4 Tag
header
content
object_type [content size]\0content of the filecan be textcan be binarycan be whatever
Zlib::Deflate
![Page 9: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/9.jpg)
1 Blobs
![Page 10: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/10.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
![Page 11: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/11.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
>git add .>git commit -m “initial commit”
![Page 12: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/12.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
blob : 21a307
>git add .>git commit -m “initial commit”
blob : 644eda
blob : a22a24
![Page 13: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/13.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
blob : 21a307
>git add .>git commit -m “initial commit”
blob : 644eda
blob : a22a24
SHA-1 hashesof file content
![Page 14: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/14.jpg)
Blob Content
![Page 15: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/15.jpg)
Blob Content
>git cat-file -p 644eda
%w{ models }.each do |dir| path = File.join(File.dirname(__FILE__), 'lib', 'app', dir) $LOAD_PATH << path ActiveSupport::Dependencies.load_paths << path ActiveSupport::Dependencies.load_once_paths.delete(path)end
![Page 16: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/16.jpg)
2 Trees
![Page 17: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/17.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
blob : 21a307
blob : 644eda
blob : a22a24
![Page 18: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/18.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
blob : 21a307
blob : 644eda
blob : a22a24
tree : f2eb1e
tree : 523fa4
![Page 19: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/19.jpg)
Tree Content
![Page 20: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/20.jpg)
Tree Content
>git cat-file -p f2eb1e
100644 blob 21a30738954b6bb164731d822efafa6c89c7bce7! Rakefile100644 blob 644eda506db859e011ccbca5a06421ee76782ac7! init.rb040000 tree 523fa41bd27fa29a00afb0bef6a10fde75aef501! lib
![Page 21: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/21.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
blob : 21a307
blob : 644eda
blob : a22a24
tree : f2eb1e
tree : 523fa4
![Page 22: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/22.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
blob : 21a307
blob : 644eda
blob : a22a24
tree : f2eb1e
tree : 523fa4
![Page 23: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/23.jpg)
3 Commits
![Page 24: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/24.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
blob : 21a307
blob : 644eda
blob : a22a24
tree : f2eb1e
tree : 523fa4
![Page 25: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/25.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
blob : 21a307
blob : 644eda
blob : a22a24
tree : f2eb1e
tree : 523fa4
commit : 3848fa
![Page 26: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/26.jpg)
Commit Content
![Page 27: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/27.jpg)
Commit Content
>git cat-file -p 3848fa
tree f2eb1e549e7e0d9cd5b7a580e63e9ce79d5a03aeauthor Jeff Kunkle <[email protected]> 1261341813 -0500committer Jeff Kunkle <[email protected]> 1261341813 -0500
initial commit
first commit
![Page 28: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/28.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
blob : 21a307
blob : 644eda
blob : a22a24
tree : f2eb1e
tree : 523fa4
commit : 3848fa
![Page 29: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/29.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory Git Directory
blob : 21a307
blob : 644eda
blob : a22a24
tree : f2eb1e
tree : 523fa4
commit : 3848fa
![Page 30: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/30.jpg)
Commit Content
tree ac451e549e7e0d9cd5b7a580e63e9ce79d5b45a1parent 3848fa7e91490a99b77590ff1385c4b3eebb3de3author Jeff Kunkle <[email protected]> 1261351813 -0500committer Jeff Kunkle <[email protected]> 1261351813 -0500
second commit
next commit
![Page 31: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/31.jpg)
4 Tags
![Page 32: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/32.jpg)
./
Rakefile
init.rb
lib
grant.rb
blob : 21a307
blob : 644eda
blob : a22a24
tree : f2eb1e
tree : 523fa4
commit : 3848fa
![Page 33: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/33.jpg)
./
Rakefile
init.rb
lib
grant.rb
blob : 21a307
blob : 644eda
blob : a22a24
tree : f2eb1e
tree : 523fa4
commit : 3848fa
tag : e9eff3>git tag -a v1.0
![Page 34: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/34.jpg)
Tag Content
![Page 35: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/35.jpg)
Tag Content
>git cat-file -p v1.0
object 3848fa7e91490a99b77590ff1385c4b3eebb3de3type committag v1.0tagger Jeff Kunkle <[email protected]> Tue Dec 29 21:02:04 2009 -0500
version 1.0
![Page 36: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/36.jpg)
./
Rakefile
init.rb
lib
grant.rb
blob : 21a307
blob : 644eda
blob : a22a24
tree : f2eb1e
tree : 523fa4
commit : 3848fa
tag : e9eff3>git tag -a v1.0
![Page 37: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/37.jpg)
./
Rakefile
init.rb
lib
grant.rb
blob : 21a307
blob : 644eda
blob : a22a24
tree : f2eb1e
tree : 523fa4
commit : 3848fa
tag : e9eff3>git tag -a v1.0
![Page 38: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/38.jpg)
References
![Page 39: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/39.jpg)
blob
tree
commit
tag
Basic Data Model
![Page 40: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/40.jpg)
blob
tree
commit
tag
Basic Data Model
ImmutableGit Objects
![Page 41: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/41.jpg)
blob
tree
commit
tag
Basic Data Model
ImmutableGit Objects
branch
![Page 42: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/42.jpg)
blob
tree
commit
tag
Basic Data Model
ImmutableGit Objects
branch
![Page 43: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/43.jpg)
blob
tree
commit
tag
Basic Data Model
ImmutableGit Objects
branch
HEAD
![Page 44: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/44.jpg)
blob
tree
commit
tag
Basic Data Model
ImmutableGit Objects
branch
HEAD
MutableReferences
![Page 45: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/45.jpg)
./
Rakefile
init.rb
lib
grant.rb
Working Directory
![Page 46: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/46.jpg)
blobtree
commit
tagbranch
HEAD
blobtree
blob
![Page 47: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/47.jpg)
blobtree
commit
tagbranch
HEAD
blobtree
blob
tree
commit
branch
HEAD
tree
blob
>git commit -a lib/grant.rb
![Page 48: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/48.jpg)
>git commit -a Rakefile init.rb
blobtree
commit
tagbranch
HEAD
blobtree
blob
tree
commit
branch
HEAD
tree
blob
tree
commit
branch
HEAD
blob
blob
![Page 49: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/49.jpg)
Branches
![Page 50: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/50.jpg)
blob
tree
commit
branch
C1
branch
![Page 51: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/51.jpg)
C
master
C
![Page 52: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/52.jpg)
C
master
C
idea
>git checkout -b idea
![Page 53: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/53.jpg)
C
master
C
C
idea
>git commit
![Page 54: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/54.jpg)
C
master
C
C
idea
>git checkout master
![Page 55: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/55.jpg)
C
master
C
C
idea
C
>git commit
![Page 56: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/56.jpg)
C
master
C
C
idea
C
bug
>git checkout -b bug
![Page 57: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/57.jpg)
C
master
C
C
idea
C
C
bug
>git commit
![Page 58: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/58.jpg)
C
master
C
C
idea
C
C
bug
>git commit>git checkout master
![Page 59: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/59.jpg)
C
master
C
C
idea
C
C
bug
>git commit>git merge bug
![Page 60: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/60.jpg)
CC
C
idea
C
C
C
bug
master
>git commit>git merge bug>git commit
![Page 61: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/61.jpg)
CC
C
idea
C
C
C
bug
master
>git commit>git merge bug>git commit>git branch -d bug
![Page 62: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/62.jpg)
CC
C
idea
C
C
C
bug
master
>git commit>git merge bug>git commit>git branch -d bug>git checkout idea
![Page 63: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/63.jpg)
CC
C C
C
idea
C
C
bug
master
>git commit>git merge bug>git commit>git branch -d bug>git checkout idea>git commit
![Page 64: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/64.jpg)
CC
C C C
C
C
C
idea
bug
master
>git commit>git merge bug>git commit>git branch -d bug>git checkout idea>git commit>git commit
![Page 65: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/65.jpg)
CC
C C C
C
C
C
idea
bug
master
>git commit>git merge bug>git commit>git branch -d bug>git checkout idea>git commit>git commit>git checkout master
![Page 66: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/66.jpg)
CC
C C C
C
C
C
idea
bug
master
>git commit>git merge bug>git commit>git branch -d bug>git checkout idea>git commit>git commit>git checkout master>git merge idea
![Page 67: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/67.jpg)
CC
C C C
CC
C
C
idea
bug
master
>git commit>git merge bug>git commit>git branch -d bug>git checkout idea>git commit>git commit>git checkout master>git merge idea>git commit
![Page 68: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/68.jpg)
CC
C C C
CC
C
C
idea
bug
master
>git commit>git merge bug>git commit>git branch -d bug>git checkout idea>git commit>git commit>git checkout master>git merge idea>git commit>git branch -d idea
![Page 69: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/69.jpg)
Recommendations
![Page 70: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/70.jpg)
Starting new project?
![Page 71: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/71.jpg)
Starting new project?
Use Git
![Page 72: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/72.jpg)
Using SVN or CVS?
![Page 73: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/73.jpg)
Using SVN or CVS?
Switch to Git
![Page 74: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/74.jpg)
Using Team Studio?
![Page 75: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/75.jpg)
Using Team Studio?
Maybe switch to Git
![Page 76: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/76.jpg)
Using MKS?
![Page 77: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/77.jpg)
Using MKS?
Switch to Gitor CVS, or Subversion, or anything else
![Page 78: Understanding Git Internals](https://reader037.vdocuments.net/reader037/viewer/2022100220/554f5869b4c905524c8b533f/html5/thumbnails/78.jpg)
References
Good Better Best*most diagrams in this presentation are based on Git internals