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

Git Educated About Git - 20 Essential Commands

Git Educated About Git - 20 Essential Commands

Git is a free, distributed version control system that is fast, easy to learn, and has great features like cheap local branching and convenient staging areas. It has also taken the open source world by storm, especially with the help of online services like GitHub. Learn 20 essential commands that will help you work with your next project, as well as common conventions and workflows.

EDIT: Updated 2/3/2014 for Sunshine PHP Conference.
EDIT: Updated on 9/15/2013 for Web & PHP Con. See the recording of "You're Doin' Git!" performance on YouTube: http://www.youtube.com/watch?v=gRWVIXRb0_Q

Jeremy Lindblom

April 20, 2013
Tweet

More Decks by Jeremy Lindblom

Other Decks in Programming

Transcript

  1. git educated about git
    by Jeremy Lindblom
    ( @jeremeamia )

    View Slide

  2. git
    what? • why? • how?

    View Slide

  3. git

    View Slide

  4. hello! i'm jeremy.

    View Slide

  5. hello! i'm jeremy.
    PHP Software Engineer at
    Co-author of the AWS SDK for PHP
    Co-organizer of the Seattle PHP User Group
    Zend Education Advisory Board for 5.5 exam
    Keys/Vocals for Gigawattband.com
    @jeremeamia on and | webjeremy.com

    View Slide

  6. raise your hand if…
    1.  You've used git.
    a.  …but only via a GUI.
    2.  You've used SVN, CVS, P4, etc.
    3.  You've never used a VCS.
    4.  You have a GitHub account.
    a.  …and actually have repos you've created.
    5.  You came to learn more about git.
    6.  You just came to hear the song.

    View Slide

  7. git
    what is (g)it?

    View Slide

  8. what is git?
    1. It's a version control system.
    2. It's really popular.
    3. It's distributed.
    4. It's fast.
    5. It's FOSS.

    View Slide

  9. what is git?
    1. It's a version control system.
    2. It's really popular.
    3. It's distributed.
    4. It's fast.
    5. It's FOSS.

    View Slide

  10. version control
    •  Also source control or revision control
    •  “any practice that tracks and provides
    control over changes to source code”[1]
    •  Version Control System ( VCS )
    – Git
    – SVN
    – CVS
    – Mercurial
    – Perforce
    – ClearCase
    – SourceSafe
    – Bazaar
    – TFS

    View Slide

  11. vocabulary – part 1
    •  Repository | Repo – Set of current &
    past data about the files
    •  Revision | Version | Commit – A tracked
    change to the repo
    •  Working Copy – Local copy of the repo
    at a specific revision
    •  Head | Tip – The most recent revision

    View Slide

  12. vocabulary – part 2
    •  Trunk | Mainline | Master – The main
    development path of the repo
    •  Branch – A divergent development path
    of the repo for features, fixes, etc.
    •  Merge – Integrate revisions from a
    branch back into trunk or other branch
    •  Tag – A name or label given to a revision

    View Slide

  13. snapshots, not deltas
    [2]

    View Slide

  14. what is git?
    1. It's a version control system.
    2. It's really popular.
    3. It's distributed.
    4. It's fast.
    5. It's FOSS.

    View Slide

  15. who is using git?

    View Slide

  16. who is using git?
    https://github.com/aws

    View Slide

  17. who is using git?
    Zend Framework • Symfony • Laravel
    Doctrine • Guzzle • CodeIgniter • Monolog
    WordPress • CakePHP
    AWS SDK for PHP • Yii
    Composer • Magento
    PHPUnit • hhvm • Slim
    Drupal • FuelPHP • Kohana • Joomla
    Silex • PHPParser • Aura • Assetic • phpBB
    Phalcon • phpDocumentor • PyroCMS

    View Slide

  18. what is git?
    1. It's a version control system.
    2. It's really popular.
    3. It's distributed.
    4. It's fast.
    5. It's FOSS.

    View Slide

  19. centralized version control
    [3]

    View Slide

  20. distributed version control
    [3]

    View Slide

  21. what is git?
    1. It's a version control system.
    2. It's really popular.
    3. It's distributed.
    4. It's fast.
    5. It's FOSS.

    View Slide

  22. what is git?
    1. It's a version control system.
    2. It's really popular.
    3. It's distributed.
    4. It's fast.
    5. It's FOSS.

    View Slide

  23. git
    why should you use (g)it?

    View Slide

  24. so you can…
    …continue being employable.
    …contribute to open source projects.
    …put code on your GitHub profile.
    …deploy code to PaaS solutions.
    …use Composer effectively.
    …ditch SVN, CVS, P4, etc.

    View Slide

  25. git
    how do you use (g)it?

    View Slide

  26. 20 essential commands
    1.    git  help  
    2.    git  config  
    3.    git  init  
    4.    git  status  
    5.    git  add  
    6.    git  rm  -­‐-­‐cached  
    7.    git  commit  
    8.    git  diff  
    9.    git  reset  
    10.  git  log  
     
     
    11.  git  remote  
    12.  git  push  
    13.  git  clone
    14.  git  pull  
    15.  git  fetch  
    16.  git  branch  
    17.  git  checkout  
    18.  git  merge  
    19.  git  tag  
    20.  git  push  -­‐-­‐tags  

    View Slide

  27. #1. git help
    $  git  help  
    usage:  git  [-­‐-­‐version]  [-­‐-­‐exec-­‐path[=]]  [-­‐-­‐html-­‐pat  
                         [-­‐p|-­‐-­‐paginate|-­‐-­‐no-­‐pager]  [-­‐-­‐no-­‐replace-­‐objec  
                         [-­‐-­‐git-­‐dir=]  [-­‐-­‐work-­‐tree=]  [-­‐-­‐nam  
                         [-­‐c  name=value]  [-­‐-­‐help]  
                           []  
     
    $  git  help  init                                                                    
    NAME  
                 git-­‐config  -­‐  Get  and  set  repository  or  global  opti  
    SYNOPSIS  
                   git  config  []  [type]  [-­‐z|-­‐-­‐null]  nam  

    View Slide

  28. #2. git config
    $  git  config  -­‐-­‐global  user.name  "Jeremy  Lindblom"  
    $  git  config  -­‐-­‐global  user.email  [email protected]  
     
    $  git  config  user.name  
    Jeremy  Lindblom  
    $  git  config  user.email  
    [email protected]  

    View Slide

  29. #3. git init
    $  mkdir  git-­‐test  
    $  cd  git-­‐test  
    $  ls  -­‐a  
    .        ..  
    $  git  init  
    Initialized  empty  Git  repository  in  ~/git-­‐test/.git/  
    $  ls  -­‐a  
    .        ..      .git  
    $  ls  -­‐a  .git  
    .                      HEAD                config            hooks              objects  
    ..                    branches        description  info                refs  

    View Slide

  30. #4. git status
    $  git  status  
    #  On  branch  master  
    #  
    #  Initial  commit  
    #  
    nothing  to  commit  (create/copy  files  and  use  "git  add"  to  
    track)  

    View Slide

  31. making changes

    View Slide

  32. making changes
    echo  'Hello,  world!';  
    $  git  status  
    #  On  branch  master  
    #  Initial  commit  
    #  Untracked  files:  
    #      (use  "git  add  ..."  to  include  in  commit)  
    #  README.md  
    #  index.php  
    nothing  added  to  commit  but  untracked  files  present  (use  
    "git  add"  to  track)  
    #  Git  Test  
     
    index.php README.md

    View Slide

  33. #5. git add
    $  git  add  index.php  
    $  git  add  README.md  
     
    $  git  status  
    #  On  branch  master  
    #  Initial  commit  
    #  Changes  to  be  committed:  
    #      (use  "git  rm  -­‐-­‐cached  ..."  to  unstage)  
    #  
    #  new  file:      index.php  
    #  new  file:      README.md  
    #  

    View Slide

  34. #6. git rm --cached
    $  git  rm  -­‐-­‐cached  README.md  
    $  git  status  
    #  On  branch  master  
    #  Initial  commit  
    #  Changes  to  be  committed:  
    #      (use  "git  rm  -­‐-­‐cached  ..."  to  unstage)  
    #  new  file:      index.php  
    #  
    #  Untracked  files:  
    #      (use  "git  add  ..."  to  include  in  commit)  
    #  README.md  

    View Slide

  35. #7. git commit
    $  git  add  .  
    $  git  commit  -­‐m  "Added  index  and  README"  
    [master  (root-­‐commit)  3ea3f8f]  Added  index  and  README  
     2  files  changed,  8  insertions(+),  0  deletions(-­‐)  
     create  mode  100644  README.md  
     create  mode  100644  index.php  
    $  git  status  
    #  On  branch  master  
    nothing  to  commit  (working  directory  clean)  

    View Slide

  36. #8. git diff
    $  git  diff  
    diff  -­‐-­‐git  a/README.md  b/README.md  
    index  fdd135c..5152172  100644  
    -­‐-­‐-­‐  a/README.md  
    +++  b/README.md  
    @@  -­‐1,4  +1,4  @@  
     #  Git  Test  
       
    -­‐Testing  out  git.  
    +Testing  out  git.  It's  not  too  hard.  

    View Slide

  37. #9. git reset
    $  git  status  
    #  On  branch  master  
    #  Changes  not  staged  for  commit:  
    #      (use  "git  add  ..."  to  update  for  commit)  
    #      (use  "git  checkout  -­‐-­‐  ..."  to  discard  changes)  
    #  
    #  modified:      README.md  
    #  
    no  changes  added  to  commit  (use  "git  add)    
    $  git  reset  -­‐-­‐hard  
    HEAD  is  now  at  fdd135c  Added  index  and  README  

    View Slide

  38. #10. git log
    $  git  log  
    fdd135cd6e09efd9c0b1b30387c2d555  Added  index  and  README  
    0190a93b810f5084fa3a85afc54cf0d6  Initial  commit  

    View Slide

  39. we're still local!
    $  git  log  
    fdd135cd6e09efd9c0b1b30387c2d555  Added  index  and  README  
    0190a93b810f5084fa3a85afc54cf0d6  Initial  commit  

    View Slide

  40. 20 essential commands
    1.    git  help  
    2.    git  config  
    3.    git  init  
    4.    git  status  
    5.    git  add  
    6.    git  rm  -­‐-­‐cached  
    7.    git  commit  
    8.    git  diff  
    9.    git  reset  
    10.  git  log  
     
     
    11.  git  remote  
    12.  git  push  
    13.  git  clone
    14.  git  pull  
    15.  git  fetch  
    16.  git  branch  
    17.  git  checkout  
    18.  git  merge  
    19.  git  tag  
    20.  git  push  -­‐-­‐tags  

    View Slide

  41. #11. git remote
    $  git  remote  add  origin  [email protected]:vendor/project.git  
    $  git  remote  -­‐-­‐verbose  
    origin
     [email protected]:vendor/project.git  (fetch)  
    origin
     [email protected]:vendor/project.git  (push)  

    View Slide

  42. workflow for changes
    Local

    View Slide

  43. #12. git push
    $  git  push  origin  master  
    Counting  objects:  6,  done.  
    Delta  compression  using  up  to  4  threads.  
    Compressing  objects:  100%  (2/2),  done.  
    Writing  objects:  100%  (4/4),  432  bytes,  done.  
    Total  4  (delta  0),  reused  0  (delta  0)  
    To  [email protected]:vendor/project.git  
         0190a93..fdd135c    master  -­‐>  master  

    View Slide

  44. #13. git clone
    $  cd  ../  
    $  rm  -­‐rf  git-­‐test/  
    $  git  clone  [email protected]:jeremeamia/git-­‐test.git  
    Cloning  into  'git-­‐test'...  
    remote:  Counting  objects:  9,  done.  
    remote:  Compressing  objects:  100%  (6/6),  done.  
    remote:  Total  9  (delta  1),  reused  4  (delta  0)  
    Receiving  objects:  100%  (9/9),  done.  
    Resolving  deltas:  100%  (1/1),  done.  
    $  cd  git-­‐test  

    View Slide

  45. #14. git pull
    $  git  pull  origin  master  
    From  github.com:jeremeamia/git-­‐test  
     *  branch                        master          -­‐>  FETCH_HEAD  
    Current  branch  master  is  up  to  date.  

    View Slide

  46. #15. git fetch
    $  git  fetch  origin    
    git  pull vs. git  fetch  

    View Slide

  47. #16. git branch
    $  git  branch  improve-­‐readme  
    $  git  branch  
       improve-­‐readme  
    *  master  

    View Slide

  48. #17. git checkout
    $  git  checkout  improve-­‐readme  
    Switched  to  branch  'improve-­‐readme'  
    $  git  branch  
    *  improve-­‐readme  
       master  

    View Slide

  49. #18. git merge
    $  vim  README.md    
    $  git  add  .  
    $  git  commit  -­‐m  "Improved  the  README"  
    [improve-­‐readme  749305e]  Improved  the  README  
     1  files  changed,  2  insertions(+),  0  deletions(-­‐)  
    $  git  checkout  master  
    Switched  to  branch  'master'  
    $  git  merge  improve-­‐readme  
    Updating  fdd135c..749305e  
    Fast-­‐forward  
     README.md  |        2  ++  
     1  files  changed,  2  insertions(+),  0  deletions(-­‐)  

    View Slide

  50. #19. git tag
    $  git  tag  v1.0.0  
    $  git  tag  
    v1.0.0  
    http://semver.org/  

    View Slide

  51. #20. git push --tags
    $  git  push  -­‐-­‐tags  origin  master  
    Counting  objects:  5,  done.  
    Delta  compression  using  up  to  4  threads.  
    Compressing  objects:  100%  (3/3),  done.  
    Writing  objects:  100%  (3/3),  328  bytes,  done.  
    Total  3  (delta  1),  reused  0  (delta  0)  
    To  [email protected]:jeremeamia/git-­‐test.git  
         fdd135c..749305e    master  -­‐>  master  
     *  [new  tag]                  1.0.0  -­‐>  1.0.0  

    View Slide

  52. congratulations!
    1.    git  help  
    2.    git  config  
    3.    git  init  
    4.    git  status  
    5.    git  add  
    6.    git  rm  -­‐-­‐cached  
    7.    git  commit  
    8.    git  diff  
    9.    git  reset  
    10.  git  log  
     
     
    11.  git  remote  
    12.  git  push  
    13.  git  clone
    14.  git  pull  
    15.  git  fetch  
    16.  git  branch  
    17.  git  checkout  
    18.  git  merge  
    19.  git  tag  
    20.  git  push  -­‐-­‐tags  

    View Slide

  53. commands summary
    Starting:
    Changing:
    Status:
    Syncing:
    Branching:
    Releasing:
    init,  clone,  config  
    add,  commit,  status,  reset,  rm  
    push,  pull,  fetch,  remote  
    branch,  merge,  checkout  
    tag,  push  -­‐-­‐tags  
    status,  diff,  log  

    View Slide

  54. “You’re Doin’ Git!”
    By Jeremy Lindblom

    ♬  


    ♬  
    ♬  

    View Slide

  55. git  help,  config,  and  init.    
    remote,  fetch,  pull,  let's  edit.  
    diff,  add,  status,  commit.  you're  doin'  git.  
     
    log,  branch,  checkout,  that's  it!  
    checkout,  merge,  rebase,  don't  quit.  
    clean,  reset,  tag,  push  those  bits!  you're  doin'  git!  
     
    fork  and  clone  all  the  repos  you  know,  
    patches  and  pull  requests,  you'll  steal  the  git  show.  
    you're  doin'  git.  you're  doing  it!  
    check  git  blame  and  see  your  username.  
    hope  you  ran  the  tests  before  your  commit,  
    or  other  devs  will  think  you're  an  idiot.  
    you're  doin'  git.  (x4)  
     
    You're  Doin'  Git!  
    ©2013  Jeremy  Lindblom  

    View Slide

  56. git
    — tools —

    View Slide

  57. git hosting
    Third-party hosting
    •  GitHub
    •  Bitbucket
    •  SourceForge
    •  Gitorious
    •  Google Code
    •  CodePlex
    Self-hosting
    •  Git
    •  Gitolite
    •  Gitosis

    View Slide

  58. git guis
    •  git-gui + gitk
    •  GitHub (web / Mac / Windows)
    •  Tower
    •  GitBox
    •  SourceTree
    •  TortoiseGit
    •  Your IDE

    View Slide

  59. git
    — workflows —

    View Slide

  60. popular workflows
    •  git-flow –
    http://nvie.com/posts/a-successful-git-
    branching-model/
    •  Github Flow –
    http://scottchacon.com/2011/08/31/
    github-flow.html

    View Slide

  61. git-flow
    [4]

    View Slide

  62. multiple remotes
    AWS  SDK  for  PHP  
    AWS SDK
    for PHP

    View Slide

  63. custom workflows
    AWS SDK for PHP

    View Slide

  64. [5]

    View Slide

  65. github features
    •  Awesome web interface
    •  Encourages collaboration
    •  Issue and milestone trackers
    •  Wikis, project pages, organizations
    •  Comments and code reviews
    •  Forking and Pull Requests (PRs)

    View Slide

  66. git
    what? • why? • how?

    View Slide

  67. git
    why?
    how?
    what? A fast, distributed, fun,
    popular, & free VCS.
    To be employable & a
    good OSS contributor.
    Learn the CLI & tools.
    Create lots of branches.

    View Slide

  68. git educated about git
    by Jeremy Lindblom
    ( @jeremeamia )
    Thanks! Questions?
    Rate on Joind.in: https://joind.in/10519

    View Slide

  69. git tutorials
    •  http://try.github.io/
    •  http://book.git-scm.com/
    •  http://progit.org/book/
    •  http://gitimmersion.com/
    •  http://githowto.com/
    •  http://gitref.org/
    •  http://gitready.com/

    View Slide

  70. references
    1.  http://en.wikipedia.org/wiki/
    Revision_control
    2.  http://git-scm.com/book/ch1-3.html
    3.  http://git-scm.com/book/en/Getting-
    Started-About-Version-Control
    4.  http://nvie.com/posts/a-successful-git-
    branching-model/
    5.  https://github.com/aws/aws-sdk-php

    View Slide