Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introductory Version Control with Git

puls
April 05, 2012

Introductory Version Control with Git

puls

April 05, 2012
Tweet

More Decks by puls

Other Decks in Technology

Transcript

  1. A rough outline Background information Basic commands Intermediate commands Going

    distributed Yes, this is the only slide full of bullets
  2. Git is an open source, distributed version control system designed

    to handle everything from small to very large projects with speed and efficiency.
  3. Git is an open source, distributed version control system designed

    to handle everything from small to very large projects with speed and efficiency.
  4. Git is an open source, distributed version control system designed

    to handle everything from small to very large projects with speed and efficiency.
  5. Git is an open source, distributed version control system designed

    to handle everything from small to very large projects with speed and efficiency.
  6. Git is an open source, distributed version control system designed

    to handle everything from small to very large projects with speed and efficiency.
  7. Git is an open source, distributed version control system designed

    to handle everything from small to very large projects with speed and efficiency.
  8. fatal: protocol error: expected sha/ref, got '*********' You asked to

    pull from the remote 'origin', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line.
  9. fatal: protocol error: expected sha/ref, got '*********' You asked to

    pull from the remote 'origin', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line. path/to/file: needs update update-index --refresh: command returned error: 1
  10. fatal: protocol error: expected sha/ref, got '*********' You asked to

    pull from the remote 'origin', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line. path/to/file: needs update update-index --refresh: command returned error: 1 fatal: /usr/local/libexec/git-core/git-pull cannot be used without a working tree.
  11. Snapshot 4 pm Snapshot 5 pm Snapshot 6 pm File

    README Folder ./ Folder tests/ File README Folder ./ Folder tests/ File README Folder ./ Folder tests/
  12. Snapshot 4 pm Snapshot 5 pm Snapshot 6 pm File

    README Folder ./ Folder tests/ File README Folder ./ Folder tests/ File README Folder ./ Folder tests/
  13. Snapshot 4 pm Snapshot 5 pm Snapshot 6 pm File

    README Folder ./ Folder tests/ File README Folder ./ Folder tests/ File README Folder ./ Folder tests/
  14. Snapshot 4 pm Snapshot 5 pm Snapshot 6 pm File

    README Folder ./ Folder tests/ File README Folder ./ Folder tests/ File README Folder ./ Folder tests/
  15. Snapshot 4 pm Snapshot 5 pm Snapshot 6 pm File

    README Folder ./ Folder tests/ File README Folder ./ Folder tests/ File README Folder ./ Folder tests/ Holy crap, it’s a tree!
  16. Snapshot 4 pm Snapshot 5 pm Snapshot 6 pm File

    README Folder ./ Folder tests/ File README Folder ./ Folder tests/ File README Folder ./ Folder tests/
  17. Snapshot 4 pm Snapshot 5 pm Snapshot 4:10 pm Snapshot

    5:10 pm Snapshot 6 pm File README Folder ./ Folder tests/ File README Folder ./ Folder tests/ File README Folder ./ Folder tests/
  18. Snapshot 4 pm Snapshot 5 pm Snapshot 4:10 pm Snapshot

    5:10 pm Snapshot 6 pm File README Folder ./ Folder tests/ File README Folder ./ Folder tests/ File README Folder ./ Folder tests/ Directed Acyclic Graph
  19. Snapshot 4 pm Snapshot 5 pm Snapshot 6 pm File

    README Folder ./ Folder tests/ File README Folder ./ Folder tests/ File README Folder ./ Folder tests/
  20. Blob README Tree ./ Tree tests/ Blob README Tree ./

    Tree tests/ Blob README Tree ./ Tree tests/ Commit 07cf70 Commit 91e781 Commit 0f2908
  21. Git is nothing more than an object database storing a

    directed acyclic graph and giving you references in to it.
  22. 91e7819c14932738d02a 96d6f425c79cd9bfd2c9 full SHA1 hash 91e78 unambiguous prefix master reference

    name master^ “the parent of master” master^2 “the other parent of master” master^^^ master~3 “the parent of the parent of the parent of master” master@{yesterday} “what master was yesterday” master@{5} “what master was five changes ago”
  23. Part II: Basic Git Commands Where do we start? ~

     puls  $  git  config  -­‐-­‐global  user.name  'Jim  Puls' ~  puls  $  git  config  -­‐-­‐global  user.email  '[email protected]'
  24. There are eight basic Git commands. git  init git  status

    git  clone git  add git  commit git  diff git  log git  reset
  25. git  init code  puls  $  git  init Initialized  empty  Git

     repository  in  /Users/puls/code/.git/ code  puls  $   “Make a brand new, empty repository that I can fill with what I’m working on now.”
  26. git  status code  puls  $  git  status #  On  branch

     master # #  Initial  commit # nothing  to  commit  (create/copy  files  and  use  "git  add"  to  track) code  puls  $   “Show me what has happened since the last commit.”
  27. git  clone code  puls  $  git  clone  git://github.com/square/cube.git Cloning  into

     'cube'... remote:  Counting  objects:  510,  done. remote:  Compressing  objects:  100%  (250/250),  done. remote:  Total  510  (delta  263),  reused  433  (delta  204) Receiving  objects:  100%  (510/510),  561.75  KiB  |  507  KiB/s,  done. Resolving  deltas:  100%  (263/263),  done. code  puls  $   “Start by making me a copy of that other repository over there so I can work on it.”
  28. git  add code  puls  $  echo  "Hello  World"  >  README

    code  puls  $  git  add  README code  puls  $  git  status #  On  branch  master # #  Initial  commit # #  Changes  to  be  committed: #      (use  "git  rm  -­‐-­‐cached  <file>..."  to  unstage) # #   new  file:      README # code  puls  $   “Stage these files for the next commit that I’m about to create.”
  29. git  commit code  puls  $  echo  "Hello  World"  >  README

    code  puls  $  git  add  README code  puls  $  git  commit  -­‐m  "Initial  commit" [master  (root-­‐commit)  3b29c5f]  Initial  commit  1  file  changed,  1  insertion(+)  create  mode  100644  README code  puls  $   “Take the stuff I just staged and turn it in to a recorded snapshot. Also update the HEAD and ‘current branch’ references.”
  30. git  diff code  puls  $  echo  "Goodbye  World"  >  README

    code  puls  $  git  add  README code  puls  $  git  commit  -­‐m  "Second  commit" [master  2b8e222]  Second  commit  1  file  changed,  1  insertion(+) code  puls  $  git  diff  3b29c5f  2b8e222 diff  -­‐-­‐git  a/README  b/README index  557db03..bb638f0  100644 -­‐-­‐-­‐  a/README +++  b/README @@  -­‐1  +1,2  @@  Hello  World +Goodbye  World code  puls  $   “Show me what changed between two different references.”
  31. git  log code  puls  $  git  log  -­‐-­‐graph  -­‐-­‐pretty=oneline *

     2b8e22274b49967079bc1948af7af1cfd37f2827  Second  commit *  3b29c5fae191058bdd612ff932543caa7dbf00c6  Initial  commit code  puls  $   “Show me the list of commits.”
  32. git  reset code  puls  $  echo  "Line  three"  >>  README

    code  puls  $  git  add  README code  puls  $  git  reset  README Unstaged  changes  after  reset: M   README code  puls  $   “Undo that ‘add’ I did.”
  33. git  branch code  puls  $  git  branch  one code  puls

     $  git  branch *  master    one code  puls  $  git  branch  -­‐d  one Deleted  branch  one  (was  2b8e222). code  puls  $  git  branch  one code  puls  $  git  branch  -­‐m  one  two code  puls  $  git  branch *  master    two code  puls  $   “Create, rename, or delete a reference.”
  34. git  checkout code  puls  $  git  checkout  two Switched  to

     branch  'two' code  puls  $  git  checkout  master Switched  to  branch  'master' code  puls  $   “Make my source tree reflect a commit referenced by this name.”
  35. git  merge code  puls  $  cat  README   Line  at

     the  beginning Hello  World Goodbye  World code  puls  $  git  checkout  two  &&  cat  README   Switched  to  branch  'two' Hello  World Goodbye  World Line  at  the  end code  puls  $  git  checkout  master  &&  git  merge  two Switched  to  branch  'master' Auto-­‐merging  README Merge  made  by  the  'recursive'  strategy.  README  |        1  +  1  file  changed,  1  insertion(+) code  puls  $   “Make a new commit that reflects all of the changes in two different commit histories.”
  36. git  merge code  puls  $  cat  README   Line  at

     the  beginning Hello  World Goodbye  World code  puls  $  git  checkout  two  &&  cat  README   Switched  to  branch  'two' Hello  World Goodbye  World Line  at  the  end code  puls  $  git  checkout  master  &&  git  merge  two Switched  to  branch  'master' Auto-­‐merging  README Merge  made  by  the  'recursive'  strategy.  README  |        1  +  1  file  changed,  1  insertion(+) code  puls  $   “Make a new commit that reflects all of the changes in two different commit histories.” code  puls  $  cat  README   Line  at  the  beginning Hello  World Goodbye  World Line  at  the  end code  puls  $  
  37. git  remote code  puls  $  git  remote  add  origin  \

    >  git://github.com/puls/somecode.git code  puls  $  git  remote  -­‐v origin  git://github.com/puls/somecode.git  (fetch) origin  git://github.com/puls/somecode.git  (push) code  puls  $   “Add or remove a named shorthand for a remote copy of this repository.”
  38. git  fetch code  puls  $  git  fetch  origin From  git://github.com/puls/somecode.git

     *  [new  branch]            master          -­‐>  origin/master code  puls  $   “Copy over all of the objects and references I don’t have already from the remote database.”
  39. git  push code  puls  $  git  push  origin  master Counting

     objects:  15,  done. Delta  compression  using  up  to  8  threads. Compressing  objects:  100%  (6/6),  done. Writing  objects:  100%  (15/15),  1.18  KiB,  done. Total  15  (delta  1),  reused  0  (delta  0) Unpacking  objects:  100%  (15/15),  done. To  git://github.com/puls/somecode.git  *  [new  branch]            master  -­‐>  master code  puls  $   “Send a particular reference and all of the objects it points at over to that repository .”
  40. git  pull code  puls  $  git  pull Already  up-­‐to-­‐date. code

     puls  $   “Fetch and then merge, since that’s what I usually do anyways.”
  41. git  revert code  puls  $  git  revert  d0fcd3 [master  d16efa9]

     Revert  "Add  a  line  at  the  end"  1  file  changed,  1  deletion(-­‐) code  puls  $  git  log  -­‐-­‐pretty=oneline d16efa92e46f00a768617883f7f63e24c41e4001  Revert  "Add  a  line  at  the 24e05021800f8ddc85000ba0f29fad8de902ca42  Merge  branch  'two' d0fcd3b4abeda5de3c36a335e29e32438a7d6b8e  Add  a  line  at  the  end d6d415e2ecae33233684e2b2b94898633e0af9f6  Add  a  line  at  the   beginning code  puls  $   “Make a new commit that undoes this previous commit.”
  42. git  cherry-­‐pick code  puls  $  git  cherry-­‐pick  d0fcd3b4abeda5d [master  7413682]

     Add  a  line  at  the  end  1  file  changed,  1  insertion(+) code  puls  $  git  log  -­‐-­‐graph  -­‐-­‐all  -­‐-­‐pretty=oneline  -­‐-­‐decorate *  74136827f00e432d3e8008da70d8106c44548886  (HEAD,  master)  Add  a  li *  d6d415e2ecae33233684e2b2b94898633e0af9f6  Add  a  line  at  the  begin |  *  d0fcd3b4abeda5de3c36a335e29e32438a7d6b8e  (two)  Add  a  line  at  t |/     *  2b8e22274b49967079bc1948af7af1cfd37f2827  Second  commit code  puls  $   “Apply the change created by one commit on that branch to this branch.”
  43. git  add  -­‐p code  puls  $  git  add  -­‐p  README

    diff  -­‐-­‐git  a/README  b/README index  84fd02a..71acfe7  100644 -­‐-­‐-­‐  a/README +++  b/README @@  -­‐1,3  +1,4  @@ +Another  at  the  beginning  Line  at  the  beginning  Hello  World  Goodbye  World Stage  this  hunk  [y,n,q,a,d,/,j,J,g,e,?]? “Stage just certain parts of these files for the next commit.”
  44. git  rebase code  puls  $  git  log  -­‐-­‐all  -­‐-­‐graph  -­‐-­‐pretty=oneline

     -­‐-­‐decorate *  d0fcd3b4abeda5de3c36a335e29e32438a7d6b8e  (two)  Add  a  line  at  the   |  *  d6d415e2ecae33233684e2b2b94898633e0af9f6  (HEAD,  master)  Add  a  l |/     *  2b8e22274b49967079bc1948af7af1cfd37f2827  Second  commit code  puls  $  git  rebase  two First,  rewinding  head  to  replay  your  work  on  top  of  it... Applying:  Add  a  line  at  the  beginning code  puls  $  git  log  -­‐-­‐all  -­‐-­‐graph  -­‐-­‐pretty=oneline  -­‐-­‐decorate *  8841ea153758ca5933e79d40e8ce2651541fe094  (HEAD,  master)  Add  a  lin *  d0fcd3b4abeda5de3c36a335e29e32438a7d6b8e  (two)  Add  a  line  at  the   *  2b8e22274b49967079bc1948af7af1cfd37f2827  Second  commit “Instead of merging, apply each change I’ve made as a patch over that version.”
  45. git  rebase  -­‐i code  puls  $  git  log  -­‐-­‐all  -­‐-­‐graph

     -­‐-­‐pretty=oneline  -­‐-­‐decorate *  d0fcd3b4abeda5de3c36a335e29e32438a7d6b8e  (two)  Add  a  line  at  the   |  *  d6d415e2ecae33233684e2b2b94898633e0af9f6  (HEAD,  master)  Add  a  l |/     *  2b8e22274b49967079bc1948af7af1cfd37f2827  Second  commit code  puls  $  git  rebase  two First,  rewinding  head  to  replay  your  work  on  top  of  it... Applying:  Add  a  line  at  the  beginning code  puls  $  git  log  -­‐-­‐all  -­‐-­‐graph  -­‐-­‐pretty=oneline  -­‐-­‐decorate *  8841ea153758ca5933e79d40e8ce2651541fe094  (HEAD,  master)  Add  a  lin *  d0fcd3b4abeda5de3c36a335e29e32438a7d6b8e  (two)  Add  a  line  at  the   *  2b8e22274b49967079bc1948af7af1cfd37f2827  Second  commit “Let me edit history, rearranging, changing, and combining commits.”
  46. git  bisect code  puls  $  git  bisect  start code  puls

     $  git  bisect  bad code  puls  $  git  checkout  2b8e22274b49967079bc1948af7af1cfd37f2827 Note:  checking  out  '2b8e22274b49967079bc1948af7af1cfd37f2827'. HEAD  is  now  at  2b8e222...  Second  commit code  puls  $  git  bisect  good Bisecting:  0  revisions  left  to  test  after  this  (roughly  0  steps) [d0fcd3b4abeda5de3c36a335e29e32438a7d6b8e]  Add  a  line  at  the  end code  puls  $  git  bisect  good 8841ea153758ca5933e79d40e8ce2651541fe094  is  the  first  bad  commit code  puls  $  git  bisect  reset Switched  to  branch  'master' code  puls  $   “Something is broken. Since I know when it worked, do a binary search to figure out which change broke it.”
  47. __git_ps1 code  puls  $  export  PS1="\W  \u$(__git_ps1)  \$  " code

     puls  (master)  $ “Show the name of my ‘current branch’ in my command prompt”
  48. __git_ps1 code  puls  $  export  PS1="\W  \u$(__git_ps1)  \$  " code

     puls  (master)  $ “Show the name of my ‘current branch’ in my command prompt”
  49. code  puls  $  git  help usage:  git  [-­‐-­‐version]  [-­‐-­‐exec-­‐path[=<path>]]  [-­‐-­‐html-­‐path]

     [-­‐-­‐man-­‐ path]  [-­‐-­‐info-­‐path]  [-­‐p|-­‐-­‐paginate|-­‐-­‐no-­‐pager]  [-­‐-­‐no-­‐replace-­‐ objects]  [-­‐-­‐bare]  [-­‐-­‐git-­‐dir=<path>]  [-­‐-­‐work-­‐tree=<path>]  [-­‐-­‐ namespace=<name>]  [-­‐c  name=value]  [-­‐-­‐help]  <command>  [<args>] The  most  commonly  used  git  commands  are:      add                Add  file  contents  to  the  index      bisect          Find  by  binary  search  the  change  that  introduced  a  bu      branch          List,  create,  or  delete  branches      checkout      Checkout  a  branch  or  paths  to  the  working  tree      clone            Clone  a  repository  into  a  new  directory      commit          Record  changes  to  the  repository      diff              Show  changes  between  commits,  commit  and  working  tree      fetch            Download  objects  and  refs  from  another  repository      grep              Print  lines  matching  a  pattern      init              Create  an  empty  git  repository  or  reinitialize  an  exi      log                Show  commit  logs      merge            Join  two  or  more  development  histories  together      mv                  Move  or  rename  a  file,  a  directory,  or  a  symlink      pull              Fetch  from  and  merge  with  another  repository  or  a  loc      push              Update  remote  refs  along  with  associated  objects      rebase          Forward-­‐port  local  commits  to  the  updated  upstream  he      reset            Reset  current  HEAD  to  the  specified  state      rm                  Remove  files  from  the  working  tree  and  from  the  index
  50. The  most  commonly  used  git  commands  are:      add

                   Add  file  contents  to  the  index      bisect          Find  by  binary  search  the  change  that  introduced  a  bu      branch          List,  create,  or  delete  branches      checkout      Checkout  a  branch  or  paths  to  the  working  tree      clone            Clone  a  repository  into  a  new  directory      commit          Record  changes  to  the  repository      diff              Show  changes  between  commits,  commit  and  working  tree      fetch            Download  objects  and  refs  from  another  repository      grep              Print  lines  matching  a  pattern      init              Create  an  empty  git  repository  or  reinitialize  an  exi      log                Show  commit  logs      merge            Join  two  or  more  development  histories  together      mv                  Move  or  rename  a  file,  a  directory,  or  a  symlink      pull              Fetch  from  and  merge  with  another  repository  or  a  loc      push              Update  remote  refs  along  with  associated  objects      rebase          Forward-­‐port  local  commits  to  the  updated  upstream  he      reset            Reset  current  HEAD  to  the  specified  state      rm                  Remove  files  from  the  working  tree  and  from  the  index      show              Show  various  types  of  objects      status          Show  the  working  tree  status      tag                Create,  list,  delete  or  verify  a  tag  object  signed  wi See  'git  help  <command>'  for  more  information  on  a  specific   command.