Introduction to Git for developers


Published on

This is introduction to Git, distributed version control system. You will learn about git history, reasons behind its invention, design considerations, internal structure and see how to use git for your projects.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction to Git for developers

  1. 1. git --intro lenin.gil lenin.gif lenin.budet.git
  2. 2. History <ul><li>History must be written of, by and for the survivors. </li></ul>
  3. 3. Brief history <ul><li>Local only </li></ul><ul><ul><li>Open-source: SCCS (1972) · RCS (1982) </li></ul></ul><ul><ul><li>Proprietary: PVCS (1985) </li></ul></ul><ul><li>Client-server </li></ul><ul><ul><li>Open-source: CVS (1990) · CVSNT (1998) · Subversion (2000) </li></ul></ul><ul><ul><li>Proprietary: Software Change Manager (1970s) · ClearCase (1992) · Visual SourceSafe (1994) · Perforce (1995) · Team Foundation Server (2005) </li></ul></ul><ul><li>Distributed </li></ul><ul><ul><li>Open-source: GNU arch (2001) · Darcs (2002) · DCVS (2002) · SVK (2003) · Monotone (2003) · Codeville (2005) · Git (2005) · Mercurial (2005) · Bazaar (2005) · Fossil (2007) </li></ul></ul><ul><ul><li>Proprietary: TeamWare (1990s?) · Code Co-op (1997) · BitKeeper (1998) · Plastic SCM (2006) </li></ul></ul>
  4. 4. Предпосылки <ul><li>Бо́льшую часть существования ядра Linux (1991-2002) изменения вносились в код путем приёма патчей и архивирования версий. В 2002 году проект перешёл на проприетарную BitKeeper </li></ul><ul><li>В 2005 отношения между сообществом разработчиков ядра Linux и владельцем BitKeeper испортились, и право бесплатного пользования продуктом было отменено </li></ul><ul><li>Guess who started git? Linus Torvalds </li></ul>
  5. 5. Problems <ul><li>Linus’ April 7, 2005 email: </li></ul><ul><ul><li>“ SCMs I've looked at make this hard. One of the things (the main thing, in fact) I've been working at is to make that process really efficient .” </li></ul></ul><ul><ul><li>“ If it takes half a minute to apply a patch […] a series of 250 emails takes two hours” </li></ul></ul><ul><ul><li>“ When I say I hate CVS with a passion , I have to also say that if there are any SVN (Subversion) users in the audience, you might want to leave […]  I see Subversion as being the most pointless project ever started ” </li></ul></ul><ul><ul><li>“ The slogan of Subversion for a while was &quot;CVS done right&quot;, or something like that, and if you start with that kind of slogan, there's nowhere you can go. There is no way to do CVS right ” </li></ul></ul>
  6. 6. “ git ” ? <ul><li>“ I’m an egotistical bastard, and I name all my projects after myself. First Linux, now git .” – Linus </li></ul><ul><li>git – (British) a foolish or worthless person </li></ul><ul><ul><li>Examples of GIT </li></ul></ul><ul><ul><ul><li>That git of a brother of yours has ruined everything! </li></ul></ul></ul><ul><ul><ul><li><oh, don't be such a silly git , of course your mates want you around> </li></ul></ul></ul>
  7. 7. PRINCIPLES <ul><li>Less is more. </li></ul>
  8. 8. Design criteria <ul><li>Take CVS as an example of what not to do ; if in doubt, make the exact opposite decision. </li></ul><ul><li>Support a distributed , BitKeeper-like workflow. </li></ul><ul><li>Very strong safeguards against corruption , either accidental or malicious </li></ul><ul><li>Very high performance </li></ul>
  9. 9. Characteristics <ul><li>Non-linear development </li></ul><ul><ul><li>rapid branching and merging </li></ul></ul><ul><ul><li>specific tools for visualizing and navigating a non-linear development history </li></ul></ul><ul><li>Distributed development </li></ul><ul><ul><li>Like Darcs, BitKeeper, Mercurial, SVK, Bazaar and Monotone </li></ul></ul><ul><li>Compatibility with existing systems/protocols </li></ul><ul><ul><li>Repositories can be published via  HTTP ,  FTP ,  rsync , or a Git protocol over either a plain socket or  ssh </li></ul></ul><ul><ul><li>CVS server emulation </li></ul></ul><ul><ul><li>Subversion and svk repositories can be used directly with git-svn </li></ul></ul>
  10. 10. Characteristics <ul><li>Efficiency </li></ul><ul><ul><li>order of magnitude faster than some revision control systems </li></ul></ul><ul><ul><li>fetching revision history from a locally stored repository can be two orders of magnitude faster than fetching it from the remote server </li></ul></ul><ul><ul><li>Git does not get slower as the project history grows larger </li></ul></ul><ul><li>Toolkit-based design </li></ul><ul><ul><li>set of programs written in C </li></ul></ul><ul><ul><li>shell scripts that provide wrappers around those programs </li></ul></ul>
  11. 11. INTERNALS <ul><li>No man should marry until he has studied anatomy and dissected at least one woman. - Honore de Balzac </li></ul>
  12. 12. Storage model <ul><li>Subversion, CVS, Perforce, Mercurial are Delta Storage  systems </li></ul><ul><ul><li>store the differences between one commit and the next </li></ul></ul><ul><ul><li>yes, mercurial is a delta-storage system </li></ul></ul><ul><li>Git is different </li></ul><ul><ul><li>stores a snapshot of what all the files in your project look like in this tree structure each time you commit. </li></ul></ul>
  13. 13. Everything has hash <ul><li>All the information needed to represent the history of a project is stored in files referenced by a 40-digit &quot;object name&quot; that looks something like this: </li></ul><ul><ul><li>6ff87c4664981e4397625791c8ea3bbb5f2279a3 </li></ul></ul><ul><li>SHA1 hash of the contents of the object. Advantages: </li></ul><ul><ul><li>Git can quickly determine whether two objects are identical or not, just by comparing names. </li></ul></ul><ul><ul><li>Since object names are computed the same way in every repository, the same content stored in two repositories will always be stored under the same name . </li></ul></ul><ul><ul><li>Git can detect errors when it reads an object, by checking that the object's name is still the SHA1 hash of its contents. </li></ul></ul>
  14. 14. Objects <ul><li>Every object consists of three things - a type , a size and content </li></ul><ul><li>There are four different types of objects: &quot;blob&quot;, &quot;tree&quot;, &quot;commit&quot;, and &quot;tag&quot;. </li></ul><ul><ul><li>A &quot; blob &quot; is used to store file data - it is generally a file </li></ul></ul><ul><ul><li>A &quot; tree &quot; is basically like a directory </li></ul></ul><ul><ul><li>A &quot; commit &quot; points to a single tree, marking it as what the project looked like at a certain point in time </li></ul></ul><ul><ul><li>A &quot; tag &quot; is a way to mark a specific </li></ul></ul>
  15. 15. Blob Object <ul><li>Chunk of binary data </li></ul><ul><li>Files with same content (anywhere in repo) share same blob </li></ul>
  16. 16. Tree Object <ul><li>Simple object with pointers to blobs and other trees – like directory. </li></ul><ul><li>Two trees have the same hash name if and only if their contents (including, recursively, the contents of all subdirectories) are identical </li></ul>
  17. 17. Commit Object <ul><li>Links a physical state of a tree with a description of how we got there and why. </li></ul>
  18. 18. Commit Object <ul><li>Commit is defined by </li></ul><ul><ul><li>a tree : The SHA1 name of a tree object, representing the contents of a directory at a certain point in time. </li></ul></ul><ul><ul><li>parent(s ): The SHA1 name of some number of commits which represent the immediately previous step(s) in the history of the project. A commit with no parents is called a &quot;root&quot; commit, and represents the initial revision of a project. </li></ul></ul><ul><ul><li>an author : The name of the person responsible for this change, together with its date. </li></ul></ul><ul><ul><li>a committer : The name of the person who actually created the commit, with the date it was done. </li></ul></ul><ul><ul><li>a comment describing this commit. </li></ul></ul>
  19. 19. The Object Model
  20. 20. REVISION HISTORY <ul><li>You can either have software quality or you can have pointer arithmetic, but you cannot have both at the same time. -- Bertrand Meyer </li></ul>
  21. 21. History is a DAG <ul><li>In computer science speak, the Git object data is a directed acyclic graph . </li></ul><ul><li>That is, starting at any commit you can traverse its parents in one direction and there is no chain that begins and ends with the same object </li></ul>
  22. 22. History is a DAG To keep all the information and history on the three versions of this tree, Git stores 16 immutable , signed , compressed objects.
  23. 23. BRANCHES <ul><li>There are two major products that come out of Berkeley: LSD and UNIX.  We don’t believe this to be a coincidence. -- Jeremy S. Anderson </li></ul>
  24. 24. Objects vs References <ul><li>Git objects are immutable </li></ul><ul><li>Beside objects, there are references </li></ul><ul><ul><li>Unlike the objects, references can change </li></ul></ul><ul><ul><li>References are simple pointers to a particular commit </li></ul></ul>
  25. 25. Branches <ul><li>Examples of references are branches and remotes </li></ul><ul><ul><li>A branch in Git is just a file that contains the SHA-1 of the most recent commit of that branch </li></ul></ul><ul><ul><li>Creating a branch is nothing more than just writing 40 characters to a file . </li></ul></ul><ul><ul><li>As you continue to commit, one of the branches will keep changing to point to the new commit SHA-1s, while the other one can stay where it was. </li></ul></ul>
  26. 26. The Model
  27. 27. Local branching
  28. 28. Local branching
  29. 29. Local branching <ul><li>Suppose we need a hot fix to production </li></ul>
  30. 30. Local branching <ul><li>After experiment merged to master </li></ul>
  31. 31. What to do with fast branches <ul><li>New branch each time you begin to work on a story or feature </li></ul><ul><li>If you get blocked and need to put it on hold, it doesn’t effect anything else . </li></ul><ul><li>Often you merge the branch back into development and delete it the same day that you created it </li></ul><ul><li>If you get a huge project or idea (refactoring, etc), you create a long-term branch , continuously rebase it to keep it in line with other development, and once everything is tested and ready, merge it in with your master. </li></ul>
  32. 32. Real workflow example
  33. 33. PRACTICE : LOCAL REPOSITORY <ul><li>The function of good software is to make the complex appear to be simple. -- Grady Booch </li></ul>
  34. 34. git init c:> mkdir test c:> cd test c:test> git init Initialized empty Git repository in c:/test/.git/
  35. 35. git add c:test> dir /b cities.cpp cities.h c:test> git add cities.h c:test> git status # On branch master # # Initial commit # # Changes to be committed: # (use &quot;git rm --cached <file>...&quot; to unstage) # # new file: cities.h # # Untracked files: # (use &quot;git add <file>...&quot; to include in what will be committed) # # cities.cpp
  36. 36. git commit c:test> git commit -m &quot;first commit&quot; [master (root-commit) 207b79d] first commit 1 files changed, 44 insertions(+), 0 deletions(-) create mode 100644 cities.h c:test> git status # On branch master # Untracked files: # (use &quot;git add <file>...&quot; to include in what will be committed) # # cities.cpp nothing added to commit but untracked files present (use &quot;git add&quot; to track)
  37. 37. git commit -a c:test> git status # On branch master nothing to commit (working directory clean) c:test> echo &quot;aaa&quot; > cities.cpp c:test> git commit -m &quot;test&quot; # On branch master # Changed but not updated: # (use &quot;git add <file>...&quot; to update what will be committed) # (use &quot;git checkout -- <file>...&quot; to discard changes in working directory) # # modified: cities.cpp # no changes added to commit (use &quot;git add&quot; and/or &quot;git commit -a&quot;) c:test> git commit -m &quot;test&quot; -a [master 6eaf41e] test 1 files changed, 1 insertions(+), 210 deletions(-) rewrite cities.cpp (100%)
  38. 38. change-> add -> commit
  39. 39. git log c:test> git commit Aborting commit due to empty commit message. c:test> git log commit 57e762203d0b522fa3a47afcc907af313b5d6d78 Author: Dmitry Guyvoronsky <> Date: Fri Feb 25 16:18:15 2011 +0200 second commit commit 207b79dd89469a75c9e92a38c4b3eac904bea603 Author: Dmitry Guyvoronsky <> Date: Fri Feb 25 16:15:17 2011 +0200 first commit c:test> git log --pretty=oneline 57e762203d0b522fa3a47afcc907af313b5d6d78 second commit 207b79dd89469a75c9e92a38c4b3eac904bea603 first commit
  40. 40. git branch c:test> git status # On branch master nothing to commit (working directory clean) c:test> git branch * master c:test> git branch mytest c:test> git branch * master mytest c:test> git checkout mytest Switched to branch 'mytest' c:test> git branch master * mytest
  41. 41. git checkout -b c:test> git branch master * mytest c:test> git checkout -b another Switched to a new branch 'another' c:test> git branch * another master mytest
  42. 42. DISTRIBUTED WORKFLOW <ul><li>Nothing is more fairly distributed than common sense: no one thinks he needs more of it than he already has </li></ul>
  43. 43. Cloning <ul><li>To clone repo = to create a copy </li></ul><ul><li>Git can clone a repository over several transports , including local, HTTP, HTTPS, SSH, its own git protocol, and rsync. </li></ul>
  44. 44. Remote branches <ul><li>Remotes are pointers to branches in other peoples copies of the same repository </li></ul><ul><li>If you got your repository by cloning it, you should have a remote branch of where you copied it from automatically added as origin by default. </li></ul>
  45. 45. Remote branches <ul><li>A fetch pulls all the refs and objects that you don’t already have from the remote repository you specify. </li></ul>
  46. 46. Remote branches <ul><li>We look at the origin/idea branch and like it, but we also want the changes they’ve made on their origin/master branch </li></ul><ul><li>So we do a 3-way merge of their two branches and our master . </li></ul><ul><li>We don’t know how well this is going to work, so we make a tryidea branch first and then do the merge there. </li></ul>
  47. 47. Just for your information <ul><li>The current record for number of commit parents in the Linux kernel is 12 branches merged in a single commit </li></ul>
  48. 48. git clone c:test> git clone Initialized empty Git repository in c:/test/test/.git/ remote: Counting objects: 10, done. remote: Compressing objects: 100% (10/10), done. remote: Total 10 (delta 1), reused 0 (delta 0) Receiving objects: 100% (10/10), 5.69 KiB, done. Resolving deltas: 100% (1/1), done.
  49. 49. Local branches are yours only c:testtest> git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/master c:testtest> git checkout -b working Switched to a new branch 'working' c:testtest> git branch -a master * working remotes/origin/HEAD -> origin/master remotes/origin/master
  50. 50. git fetch ; git merge c:testtest> git st # On branch master nothing to commit (working directory clean) c:testtest> git fetch remote: Counting objects: 4, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From 3ade0ca..6309355 master -> origin/master c:testtest> git st # On branch master # Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded. # nothing to commit (working directory clean) c:testtest> git merge origin/master Updating 3ade0ca..6309355 Fast-forward new.cpp | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 new.cpp
  51. 51. git pull = git fetch ; git merge c:testtest> git pull remote: Counting objects: 5, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 1), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. From 6309355..a4212c7 master -> origin/master Updating 6309355..a4212c7 Fast-forward new.cpp | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
  52. 52. GUIs <ul><li>Simplicity is not a matter of dumbing things down. Simplicity is when someone takes care of the details. </li></ul>
  53. 53. Graphical interfaces <ul><li>gitk </li></ul><ul><li>TortoiseSvn </li></ul><ul><li>SmartGit </li></ul><ul><li>mysysgit </li></ul><ul><li>… at least 25 more </li></ul><ul><li>Interfaces, frontends, and tools </li></ul>
  54. 54. Graphical interfaces - gitk
  55. 55. Graphical interfaces - TortoiseGit
  56. 56. Graphical interfaces - SmartGit
  57. 57. COMPARISON <ul><li>There ain't no such thing as a free lunch? </li></ul>
  58. 58. <ul><li>git </li></ul><ul><li>History is DAG </li></ul><ul><li>Manipulate history – rebase, reset, commit amend, etc </li></ul><ul><li>Branch is just a reference (head) </li></ul><ul><li>Faster on Linux systems </li></ul><ul><li>C </li></ul><ul><li>Linux, Rails, Perl, Android, Wine, Fedora, Gnome etc. </li></ul><ul><li> (619,333 users, 1,783,177 repos) </li></ul><ul><li>That’s it </li></ul><ul><li>Mercurial </li></ul><ul><li>History is DAG, but tries to be linear, causing negative effects in some places (same rev number over different repos) </li></ul><ul><li>No tools to manipulate history by default </li></ul><ul><li>Confusion working with branches – named/unnamed, etc. </li></ul><ul><li>Python </li></ul><ul><li>Mozilla, OpenJDK, OpenSolaris, Xen, Symbian, Go etc. </li></ul><ul><li> (100,000+ users, 49,334 repos) </li></ul><ul><li>That’s it </li></ul>
  59. 59. … investigate it yourself <ul><li>Rebase </li></ul><ul><li>Git stash </li></ul><ul><li>Git bisect (binary search) </li></ul><ul><li>Tagging (with/without message, +signed tags possible) </li></ul><ul><li>… </li></ul><ul><li>Profit! </li></ul>
  60. 60. Q & A <ul><li>Start here : / </li></ul><ul><li>For those who know SVN - http:// </li></ul><ul><li>Git for Windows - </li></ul>
  61. 61. [email_address]