Understanding Git Internals

78

Click here to load reader

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 of Understanding Git Internals

Page 1: Understanding Git Internals

GitJeff Kunkle

Understanding

January 6, 2010

Page 2: Understanding Git Internals

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

./

Rakefile

init.rb

lib

grant.rb

Working Directory

Page 4: Understanding Git Internals

./

Rakefile

init.rb

lib

grant.rb

Working Directory

>git init

.git

Page 5: Understanding Git Internals

./

Rakefile

init.rb

lib

grant.rb

Working Directory

>git init

What’s in here?.git

Page 6: Understanding Git Internals

Objects

Page 7: Understanding Git Internals

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

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

1 Blobs

Page 10: Understanding Git Internals

./

Rakefile

init.rb

lib

grant.rb

Working Directory Git Directory

Page 11: Understanding Git Internals

./

Rakefile

init.rb

lib

grant.rb

Working Directory Git Directory

>git add .>git commit -m “initial commit”

Page 12: Understanding Git Internals

./

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

./

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

Blob Content

Page 15: Understanding Git Internals

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

2 Trees

Page 17: Understanding Git Internals

./

Rakefile

init.rb

lib

grant.rb

Working Directory Git Directory

blob : 21a307

blob : 644eda

blob : a22a24

Page 18: Understanding Git Internals

./

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

Tree Content

Page 20: Understanding Git Internals

Tree Content

>git cat-file -p f2eb1e

100644 blob 21a30738954b6bb164731d822efafa6c89c7bce7! Rakefile100644 blob 644eda506db859e011ccbca5a06421ee76782ac7! init.rb040000 tree 523fa41bd27fa29a00afb0bef6a10fde75aef501! lib

Page 21: Understanding Git Internals

./

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

./

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

3 Commits

Page 24: Understanding Git Internals

./

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

./

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

Commit Content

Page 27: Understanding Git Internals

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

./

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

./

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

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

4 Tags

Page 32: Understanding Git Internals

./

Rakefile

init.rb

lib

grant.rb

blob : 21a307

blob : 644eda

blob : a22a24

tree : f2eb1e

tree : 523fa4

commit : 3848fa

Page 33: Understanding Git Internals

./

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

Tag Content

Page 35: Understanding Git Internals

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

./

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

./

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

References

Page 39: Understanding Git Internals

blob

tree

commit

tag

Basic Data Model

Page 40: Understanding Git Internals

blob

tree

commit

tag

Basic Data Model

ImmutableGit Objects

Page 41: Understanding Git Internals

blob

tree

commit

tag

Basic Data Model

ImmutableGit Objects

branch

Page 42: Understanding Git Internals

blob

tree

commit

tag

Basic Data Model

ImmutableGit Objects

branch

Page 43: Understanding Git Internals

blob

tree

commit

tag

Basic Data Model

ImmutableGit Objects

branch

HEAD

Page 44: Understanding Git Internals

blob

tree

commit

tag

Basic Data Model

ImmutableGit Objects

branch

HEAD

MutableReferences

Page 45: Understanding Git Internals

./

Rakefile

init.rb

lib

grant.rb

Working Directory

Page 46: Understanding Git Internals

blobtree

commit

tagbranch

HEAD

blobtree

blob

Page 47: Understanding Git Internals

blobtree

commit

tagbranch

HEAD

blobtree

blob

tree

commit

branch

HEAD

tree

blob

>git commit -a lib/grant.rb

Page 48: Understanding Git Internals

>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

Branches

Page 50: Understanding Git Internals

blob

tree

commit

branch

C1

branch

Page 51: Understanding Git Internals

C

master

C

Page 52: Understanding Git Internals

C

master

C

idea

>git checkout -b idea

Page 53: Understanding Git Internals

C

master

C

C

idea

>git commit

Page 54: Understanding Git Internals

C

master

C

C

idea

>git checkout master

Page 55: Understanding Git Internals

C

master

C

C

idea

C

>git commit

Page 56: Understanding Git Internals

C

master

C

C

idea

C

bug

>git checkout -b bug

Page 57: Understanding Git Internals

C

master

C

C

idea

C

C

bug

>git commit

Page 58: Understanding Git Internals

C

master

C

C

idea

C

C

bug

>git commit>git checkout master

Page 59: Understanding Git Internals

C

master

C

C

idea

C

C

bug

>git commit>git merge bug

Page 60: Understanding Git Internals

CC

C

idea

C

C

C

bug

master

>git commit>git merge bug>git commit

Page 61: Understanding Git Internals

CC

C

idea

C

C

C

bug

master

>git commit>git merge bug>git commit>git branch -d bug

Page 62: Understanding Git Internals

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

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

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

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

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

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

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

Recommendations

Page 70: Understanding Git Internals

Starting new project?

Page 71: Understanding Git Internals

Starting new project?

Use Git

Page 72: Understanding Git Internals

Using SVN or CVS?

Page 73: Understanding Git Internals

Using SVN or CVS?

Switch to Git

Page 74: Understanding Git Internals

Using Team Studio?

Page 75: Understanding Git Internals

Using Team Studio?

Maybe switch to Git

Page 76: Understanding Git Internals

Using MKS?

Page 77: Understanding Git Internals

Using MKS?

Switch to Gitor CVS, or Subversion, or anything else

Page 78: Understanding Git Internals

References

Good Better Best*most diagrams in this presentation are based on Git internals