Git Internals

Git Internals

Git Internals Keynote for @colognerb

Fc200fdbb63c5fa39f7a8c98b4915924?s=128

thyphoon

June 21, 2012
Tweet

Transcript

  1. Git Internals by @railsbros_andi Donnerstag, 21. Juni 12

  2. 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
  3. Donnerstag, 21. Juni 12

  4. Git = Subversion + Magic Donnerstag, 21. Juni 12

  5. Donnerstag, 21. Juni 12

  6. 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
  7. 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
  8. Git Object Types The foundation of a Git repository Donnerstag,

    21. Juni 12
  9. 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
  10. 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
  11. The Blob ./ Rakefile lib/ README simplegit.rb Working Directory Git

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

    Repository blob : a874b7 blob : a906cb blob : a0a60a Donnerstag, 21. Juni 12
  13. 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
  14. 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
  15. The Commit Contents pointer to a tree author commiter message

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

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

    branches blob commit master tree Donnerstag, 21. Juni 12
  18. The Git Data Model a directed acyclic graph blob commit

    tag HEAD branch remote tree Donnerstag, 21. Juni 12
  19. 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
  20. 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
  21. Storing of commits How commits, trees and blobs are connected

    Donnerstag, 21. Juni 12
  22. blob commit HEAD master tree tree tree blob blob .

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

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

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

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

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

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

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

    `-lib |-README `-example |-source | `-impl.rb `-example.rb Donnerstag, 21. Juni 12
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. . `-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
  37. . `-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
  38. 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
  39. Branching and Merging The Flow of merging branches Donnerstag, 21.

    Juni 12
  40. C1 HEAD master C0 Donnerstag, 21. Juni 12

  41. C1 HEAD master C0 $> git checkout -b feature Donnerstag,

    21. Juni 12
  42. C1 HEAD master C0 feature Donnerstag, 21. Juni 12

  43. C1 HEAD master C0 feature $> write some code ...

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

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

    21. Juni 12
  46. $> git checkout master $> fixing ... $> git commit

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

    21. Juni 12
  48. $> git checkout feature $> coding ... $> git commit

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

    Donnerstag, 21. Juni 12
  50. $> git checkout master $> git merge feature C1 HEAD

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

    C6 Donnerstag, 21. Juni 12
  52. C1 HEAD master C0 feature Donnerstag, 21. Juni 12

  53. 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
  54. C1 HEAD master C0 feature C2 C3 C4 Donnerstag, 21.

    Juni 12
  55. $> git checkout master $> git merge feature C1 HEAD

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

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

    21. Juni 12
  58. 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
  59. Donnerstag, 21. Juni 12

  60. $> git clone <some-repo> C0 master origin/master Donnerstag, 21. Juni

    12
  61. C0 master origin/master Donnerstag, 21. Juni 12

  62. C0 master origin/master $> coding ... $> git commit -a

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

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

    Juni 12
  65. C1 C0 C2 master origin/master CR1 CR3 CR4 CR2 origin/idea

    Donnerstag, 21. Juni 12
  66. 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
  67. C1 C0 C2 master origin/master CR1 CR3 CR4 CR2 origin/idea

    C3 tryidea Donnerstag, 21. Juni 12
  68. 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
  69. C1 C0 C2 master origin/master CR1 CR3 CR4 CR2 origin/idea

    C3 Donnerstag, 21. Juni 12
  70. Rebasing What is rebasing? Donnerstag, 21. Juni 12

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

  72. C1 john/master master C0 R1 $> git pull john master

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

  74. C1 john/master master C0 R1 $john> code ... commit $you>

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

    21. Juni 12
  76. C1 john/master master C0 R1 C2 R2 C3 C4 Merge

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

    Commits lets try the same scenario with rebasing Donnerstag, 21. Juni 12
  78. C1 john/master master C0 R1 Donnerstag, 21. Juni 12

  79. C1 john/master master C0 R1 $> git rebase john/master Donnerstag,

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

    12
  81. C1 john/master master C0 R1 C2 C0:C1 $john> code ...

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

    Juni 12
  83. C1 john/master master C0 R1 C3 $> git rebase john/master

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

    C3 C2:C3 Donnerstag, 21. Juni 12
  85. 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
  86. 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
  87. 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
  88. About Me Andi Bade – Galaxy Cats IT Consulting GmbH

    Twitter: @railsbros_andi Blog: thyphoon.github.com Mail: andi@galaxycats.com Donnerstag, 21. Juni 12