Slide 1

Slide 1 text

Git Internals by @railsbros_andi Donnerstag, 21. Juni 12

Slide 2

Slide 2 text

History In April of 2005, Git was born Developed by Linus Torvalds for SCM of Linux “I’m an egotistical bastard, and I name all my projects after myself. First Linux, now git.” – Linus Git started out as a collection of lower level functions used in various combinations by shell and perl scripts. Donnerstag, 21. Juni 12

Slide 3

Slide 3 text

Donnerstag, 21. Juni 12

Slide 4

Slide 4 text

Git = Subversion + Magic Donnerstag, 21. Juni 12

Slide 5

Slide 5 text

Donnerstag, 21. Juni 12

Slide 6

Slide 6 text

Non-Linear Development is built to be worked on simultaneously by many people, having multiple branches developed by individual developers, being merged, branched and re- merged constantly Because of this, branching is incredibly cheap and merging is incredibly easy. Donnerstag, 21. Juni 12

Slide 7

Slide 7 text

Distributed Development Git is built to make distributed development simple. No repository is special or central in Git It works completely offline or with hundreds of remote repositories that can push to and/ or fetch from each other over several simple and standard protocols. Donnerstag, 21. Juni 12

Slide 8

Slide 8 text

Git Object Types The foundation of a Git repository Donnerstag, 21. Juni 12

Slide 9

Slide 9 text

Git Object Types Git objects are the actual data of Git There are four main object types in Git Blob Tree Commit Tag Donnerstag, 21. Juni 12

Slide 10

Slide 10 text

Git Object Types Git objects are the actual data of Git There are four main object types in Git Blob Tree Commit Tag Important for understanding Donnerstag, 21. Juni 12

Slide 11

Slide 11 text

The Blob ./ Rakefile lib/ README simplegit.rb Working Directory Git Repository Donnerstag, 21. Juni 12

Slide 12

Slide 12 text

The Blob ./ Rakefile lib/ README simplegit.rb Working Directory Git Repository blob : a874b7 blob : a906cb blob : a0a60a Donnerstag, 21. Juni 12

Slide 13

Slide 13 text

The Tree ./ Rakefile lib/ README simplegit.rb Working Directory Git Repository blob : a874b7 blob : a906cb blob : a0a60a tree : 1a738d tree : fe8971 Donnerstag, 21. Juni 12

Slide 14

Slide 14 text

The Commit ./ Rakefile lib/ README simplegit.rb Working Directory Git Repository blob : a874b7 blob : a906cb blob : a0a60a tree : 1a738d tree : fe8971 commit : a11bef Donnerstag, 21. Juni 12

Slide 15

Slide 15 text

The Commit Contents pointer to a tree author commiter message parent commits Donnerstag, 21. Juni 12

Slide 16

Slide 16 text

The Commit Contents pointer to a tree author commiter message parent commits blob commit tree parent Donnerstag, 21. Juni 12

Slide 17

Slide 17 text

References are simple pointers/references to a particular commit for example branches blob commit master tree Donnerstag, 21. Juni 12

Slide 18

Slide 18 text

The Git Data Model a directed acyclic graph blob commit tag HEAD branch remote tree Donnerstag, 21. Juni 12

Slide 19

Slide 19 text

The Directory if you run git init you will find a .git directory in your current directory the .git directory holds all objects and references the .git is the whole repository Donnerstag, 21. Juni 12

Slide 20

Slide 20 text

Working Directory the working directory is the checkout it is a working copy, so it is not important if you switch branches, your working directory content will be changed without a checkout (an empty working directory) this is called a bare git repository Donnerstag, 21. Juni 12

Slide 21

Slide 21 text

Storing of commits How commits, trees and blobs are connected Donnerstag, 21. Juni 12

Slide 22

Slide 22 text

blob commit HEAD master tree tree tree blob blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 23

Slide 23 text

blob commit HEAD master tree tree tree blob blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 24

Slide 24 text

blob commit HEAD master tree tree tree blob blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 25

Slide 25 text

blob commit HEAD master tree tree tree blob blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 26

Slide 26 text

blob commit HEAD master tree tree tree blob blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 27

Slide 27 text

blob commit HEAD master tree tree tree blob blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 28

Slide 28 text

blob commit HEAD master tree tree tree blob blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 29

Slide 29 text

blob commit HEAD master tree tree tree blob blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 30

Slide 30 text

blob commit HEAD master tree tree tree blob blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb $> modify impl.rb ..... $> git commit -a Donnerstag, 21. Juni 12

Slide 31

Slide 31 text

blob commit HEAD master tree tree tree blob blob commit tree tree tree blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 32

Slide 32 text

blob commit HEAD master tree tree tree blob blob commit tree tree tree blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 33

Slide 33 text

blob commit HEAD master tree tree tree blob blob commit tree tree tree blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 34

Slide 34 text

blob commit HEAD master tree tree tree blob blob commit tree tree tree blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12

Slide 35

Slide 35 text

blob commit HEAD master tree tree tree blob blob commit tree tree tree blob . `-lib |-README `-example |-source | `-impl.rb `-example.rb $> modify README ... $> git commit -a Donnerstag, 21. Juni 12

Slide 36

Slide 36 text

. `-lib |-README `-example |-source | `-impl.rb `-example.rb blob commit tree tree tree blob blob commit tree tree tree blob HEAD master tree blob commit Donnerstag, 21. Juni 12

Slide 37

Slide 37 text

. `-lib |-README `-example |-source | `-impl.rb `-example.rb blob commit tree tree tree blob blob commit tree tree tree blob HEAD master tree blob commit Donnerstag, 21. Juni 12

Slide 38

Slide 38 text

Branching creating a new branch is simply writing a file in the .git/ refs/heads with the SHA-1 of the last commit for that branch ➡ Creating a branch is nothing more than just writing 40 characters to a file. DEMO Donnerstag, 21. Juni 12

Slide 39

Slide 39 text

Branching and Merging The Flow of merging branches Donnerstag, 21. Juni 12

Slide 40

Slide 40 text

C1 HEAD master C0 Donnerstag, 21. Juni 12

Slide 41

Slide 41 text

C1 HEAD master C0 $> git checkout -b feature Donnerstag, 21. Juni 12

Slide 42

Slide 42 text

C1 HEAD master C0 feature Donnerstag, 21. Juni 12

Slide 43

Slide 43 text

C1 HEAD master C0 feature $> write some code ... $> git commit -a $> write more code ... $> git commit -a Donnerstag, 21. Juni 12

Slide 44

Slide 44 text

C1 HEAD master C0 feature C2 C3 Donnerstag, 21. Juni 12

Slide 45

Slide 45 text

C1 HEAD master C0 feature C2 C3 HOTFIX ALERT Donnerstag, 21. Juni 12

Slide 46

Slide 46 text

$> git checkout master $> fixing ... $> git commit -a $> git tag t1 C1 HEAD master C0 feature C2 C3 HOTFIX ALERT Donnerstag, 21. Juni 12

Slide 47

Slide 47 text

C1 HEAD master C0 feature C2 C3 C4 t1 Donnerstag, 21. Juni 12

Slide 48

Slide 48 text

$> git checkout feature $> coding ... $> git commit -a C1 HEAD master C0 feature C2 C3 C4 t1 Donnerstag, 21. Juni 12

Slide 49

Slide 49 text

C1 HEAD master C0 feature C2 C3 C4 t1 C5 Donnerstag, 21. Juni 12

Slide 50

Slide 50 text

$> git checkout master $> git merge feature C1 HEAD master C0 feature C2 C3 C4 t1 C5 Donnerstag, 21. Juni 12

Slide 51

Slide 51 text

C1 HEAD master C0 feature C2 C3 C4 t1 C5 C6 Donnerstag, 21. Juni 12

Slide 52

Slide 52 text

C1 HEAD master C0 feature Donnerstag, 21. Juni 12

Slide 53

Slide 53 text

C1 HEAD master C0 feature $> write some code ... $> git commit -a $> write more code ... $> git commit -a $> write more code ... $> git commit -a Donnerstag, 21. Juni 12

Slide 54

Slide 54 text

C1 HEAD master C0 feature C2 C3 C4 Donnerstag, 21. Juni 12

Slide 55

Slide 55 text

$> git checkout master $> git merge feature C1 HEAD master C0 feature C2 C3 C4 Donnerstag, 21. Juni 12

Slide 56

Slide 56 text

C1 HEAD master C0 feature C2 C3 C4 Donnerstag, 21. Juni 12

Slide 57

Slide 57 text

C1 HEAD master C0 feature C2 C3 C4 fast-forward Donnerstag, 21. Juni 12

Slide 58

Slide 58 text

Remotes Remotes are pointers to branches in other peoples copies of the same repository often just one, named origin the master from the origin ist referenced by origin/master Donnerstag, 21. Juni 12

Slide 59

Slide 59 text

Donnerstag, 21. Juni 12

Slide 60

Slide 60 text

$> git clone C0 master origin/master Donnerstag, 21. Juni 12

Slide 61

Slide 61 text

C0 master origin/master Donnerstag, 21. Juni 12

Slide 62

Slide 62 text

C0 master origin/master $> coding ... $> git commit -a $> coding ... $> git commit -a Donnerstag, 21. Juni 12

Slide 63

Slide 63 text

C1 C0 C2 master origin/master Donnerstag, 21. Juni 12

Slide 64

Slide 64 text

C1 C0 C2 master origin/master $> git fetch Donnerstag, 21. Juni 12

Slide 65

Slide 65 text

C1 C0 C2 master origin/master CR1 CR3 CR4 CR2 origin/idea Donnerstag, 21. Juni 12

Slide 66

Slide 66 text

C1 C0 C2 master origin/master $> git checkout -b tryidea $> git merge origin/master --no-commit $> git merge origin/idea --no-commit $> git commit -a CR1 CR3 CR4 CR2 origin/idea Donnerstag, 21. Juni 12

Slide 67

Slide 67 text

C1 C0 C2 master origin/master CR1 CR3 CR4 CR2 origin/idea C3 tryidea Donnerstag, 21. Juni 12

Slide 68

Slide 68 text

C1 C0 C2 master origin/master CR1 CR3 CR4 CR2 origin/idea C3 tryidea $> git checkout master $> git merge tryidea $> git push orgin master Donnerstag, 21. Juni 12

Slide 69

Slide 69 text

C1 C0 C2 master origin/master CR1 CR3 CR4 CR2 origin/idea C3 Donnerstag, 21. Juni 12

Slide 70

Slide 70 text

Rebasing What is rebasing? Donnerstag, 21. Juni 12

Slide 71

Slide 71 text

C1 john/master master C0 R1 Donnerstag, 21. Juni 12

Slide 72

Slide 72 text

C1 john/master master C0 R1 $> git pull john master Donnerstag, 21. Juni 12

Slide 73

Slide 73 text

C1 john/master master C0 R1 C2 Donnerstag, 21. Juni 12

Slide 74

Slide 74 text

C1 john/master master C0 R1 $john> code ... commit $you> code ... commit $you> git pull john master C2 Donnerstag, 21. Juni 12

Slide 75

Slide 75 text

C1 john/master master C0 R1 C2 R2 C3 C4 Donnerstag, 21. Juni 12

Slide 76

Slide 76 text

C1 john/master master C0 R1 C2 R2 C3 C4 Merge Commits Donnerstag, 21. Juni 12

Slide 77

Slide 77 text

C1 john/master master C0 R1 C2 R2 C3 C4 Merge Commits lets try the same scenario with rebasing Donnerstag, 21. Juni 12

Slide 78

Slide 78 text

C1 john/master master C0 R1 Donnerstag, 21. Juni 12

Slide 79

Slide 79 text

C1 john/master master C0 R1 $> git rebase john/master Donnerstag, 21. Juni 12

Slide 80

Slide 80 text

C1 john/master master C0 R1 C2 C0:C1 Donnerstag, 21. Juni 12

Slide 81

Slide 81 text

C1 john/master master C0 R1 C2 C0:C1 $john> code ... commit $you> code ... commit Donnerstag, 21. Juni 12

Slide 82

Slide 82 text

C1 john/master master C0 R1 C3 R2 C2 Donnerstag, 21. Juni 12

Slide 83

Slide 83 text

C1 john/master master C0 R1 C3 $> git rebase john/master R2 C2 Donnerstag, 21. Juni 12

Slide 84

Slide 84 text

C1 john/master C0 R1 R1:C2 R2 C2 master C5 C4 C3 C2:C3 Donnerstag, 21. Juni 12

Slide 85

Slide 85 text

C1 john/master master C0 R1 C4 R2 C2 C1 john/master master C0 R1 C2 R2 C3 C4 fetch and merge rebase C5 C3 Donnerstag, 21. Juni 12

Slide 86

Slide 86 text

rebase instead of merge normally a pull is a fetch and a merge you can change it to be a fetch and a rebase git config branch.autosetuprebase always/never/local/ remote or you can call git pull --rebase Donnerstag, 21. Juni 12

Slide 87

Slide 87 text

Credits heavily based on Git Internals PDF by Scott Chacon from PeepCode (https:// peepcode.com/products/git-internals-pdf) and some own experience Donnerstag, 21. Juni 12

Slide 88

Slide 88 text

About Me Andi Bade – Galaxy Cats IT Consulting GmbH Twitter: @railsbros_andi Blog: thyphoon.github.com Mail: [email protected] Donnerstag, 21. Juni 12