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

Git

 Git

Intro to Git presentation. Presented in ATLRUG in 2008.

Jesse Newland

October 02, 2011
Tweet

More Decks by Jesse Newland

Other Decks in Technology

Transcript

  1. View Slide

  2. jnewland.com/tag/git

    View Slide

  3. What is Git?
    • A popular distributed version control
    system designed to handle very large
    projects with speed and efficiency.
    • http://git.or.cz/

    View Slide

  4. quick survey
    • Who is not using source control?
    • SVN?
    • CVS? (still?)

    View Slide

  5. • Written by Linus Torvalds
    • Written to manage the Linux Kernel
    Git History

    View Slide

  6. Getting Git

    View Slide

  7. For the brave
    # install gnupg, gettext
    $ curl -O http://kernel.org/pub/software/scm/git/
    git-1.5.4.1.tar.gz
    $ tar xzf git-1.5.4.1.tar.gz
    $ ./configure
    $ make
    $ sudo make install
    $ curl http://www.kernel.org/pub/software/scm/git/git-
    manpages-1.5.4.tar.bz2
    $ sudo tar xjf git-manpages-1.5.4.tar.bz2 -C /usr/
    local/share/man

    View Slide

  8. For the weak
    sudo port install git-core +svn
    sudo apt-get install git-core
    sudo yum install git-core

    View Slide

  9. Initial Setup
    $ git config --global user.name='My Name'
    $ git config --global [email protected]
    # optional, for pretty colors
    $ git config --global color.diff=auto
    $ git config --global color.diff.new=cyan
    $ git config --global color.diff.old=magenta
    $ git config --global color.diff.frag=yellow
    $ git config --global color.diff.meta=green
    $ git config --global color.diff.commit=normal

    View Slide

  10. Why Git?
    • Distributed, not Centralized
    • Revolutionizes how you use branching
    • Extremely stupidly ridiculously fast, even
    with large projects
    • Community

    View Slide

  11. Centralized
    vs
    Distributed

    View Slide

  12. Centralized VCS
    • Repository
    • History stored in central location
    • Checkout
    • A ‘working tree’

    View Slide

  13. Distributed VCS
    • Every Git working directory is a full-fledged
    repository with full revision tracking
    capabilities, not dependent on network
    access or a central server.
    • Commits happen offline.
    • Commits can then be pushed and pulled
    between repositories with shared history.

    View Slide

  14. Wait, commits
    happen offline?

    View Slide

  15. Commits happen
    offline!!!!!!!11!!!

    View Slide

  16. on the plane

    View Slide

  17. on the train

    View Slide

  18. at that crappy coffee shop with the paid WiFi

    View Slide

  19. at an ATLRUG meeting

    View Slide

  20. Branching FTW

    View Slide

  21. “But I don’t branch”

    View Slide

  22. because you don’t use Git

    View Slide

  23. quick survey #2
    • How many of you use branching?
    • Work exclusively on trunk/master?
    • NEVER work on trunk/master?

    View Slide

  24. Why branching with
    Git is awesome
    • Instant
    $ time git checkout -b newbranch
    Switched to a new branch "newbranch"
    real 0m0.227s
    • Private
    • Merging doesn’t suck

    View Slide

  25. Forget about
    ‘breaking the build’
    • ‘Atomic’ commits are a thing of the past
    • ‘Atomic’ merges

    View Slide

  26. • Prototype well-developed changes
    • Commit early and often
    • Review and revise before you merge
    Topic Branches

    View Slide

  27. With Git, branches are
    now a part of my
    everyday workflow

    View Slide

  28. ‘Tomorrow’ branch

    View Slide

  29. ‘Drunk’ branch

    View Slide

  30. Performance

    View Slide

  31. Offline Operations
    • Performing a diff
    • Viewing file history
    • Committing changes
    • Merging branches
    • Obtaining any other revision of a file
    • Switching branches

    View Slide

  32. Online operations
    are fast too
    $ time svn co http://svn.rubyonrails.org/rails/
    trunk/
    ...
    real 0m29.537s

    View Slide

  33. Online operations
    are fast too
    $ time git clone --depth 1 git://github.com/josh/
    rails.git
    ...
    real 0m9.088s

    View Slide

  34. Online operations
    are fast too
    $ time git clone git://github.com/josh/rails.git
    ...
    real 0m37.512s

    View Slide

  35. Git for Subversion
    Users

    View Slide

  36. This looks familiar
    $ git status
    $ git log
    $ git blame
    $ git add FILE
    $ git rm FILE
    $ git mv FILE
    $ svn status
    $ svn log
    $ svn blame
    $ git add FILE
    $ git rm FILE
    $ git mv FILE

    View Slide

  37. Creating a Repo
    $ pwd
    ~/src/foo
    $ git init
    $ git add .
    $ git commit

    View Slide

  38. Checking out a Repo
    $ git clone REPO_URL

    View Slide

  39. git status
    • Untracked Files
    • Brand new file
    • Changed but not updated
    • Locally changed file not in the index
    • Changes to be committed
    • ‘The Index’

    View Slide

  40. The Index
    • Staging area for your next commit
    • Sort of like files marked A, M, D in
    svn status
    output

    View Slide

  41. One major difference
    • After making any changes to the working
    directory, and before running the commit
    command, you must use the add command
    to add any new or modified files to the
    index.

    View Slide

  42. Example
    # create bar and to the index
    echo “foo” > bar
    git add bar
    # change bar, and thus remove from the index
    echo “ “ >> bar
    # add bar to the index again
    git add bar

    View Slide

  43. Committing
    • git commit
    • commit what’s in the index
    • git commit -a
    • adds changed but not untracked
    files to the index, then commits
    • exactly like SVN

    View Slide

  44. Diffs
    # diff between working tree and the index
    $ git diff
    # diff between the index and last commit
    $ git diff --cached
    # diff between working tree and last commit
    $ git diff HEAD

    View Slide

  45. Logs
    # just like svn
    $ git log
    # find a commit changed a specific string
    $ git log -S"def stupid_method"
    # log with patches for each commit
    $ git log -p

    View Slide

  46. Reverting Changes
    $ git checkout PATH $ svn revert PATH

    View Slide

  47. git revert != svn revert
    # most similar to svn revert
    $ git checkout .
    # reverse commit and commit the result
    $ git revert

    View Slide

  48. Branching
    # create a new branch
    $ git branch NEW_BRANCH
    # switch to this branch
    $ git checkout NEW_BRANCH
    # create a new branch and check it out in one step
    $ git checkout -b NEW_BRANCH

    View Slide

  49. More Branching
    # view available branches
    $ git branch
    * new_branch
    master
    # delete a branch
    $ git branch -d ALREADY_MERGED_BRANCH
    $ git branch -D BAD_BRANCH

    View Slide

  50. Diffing and Logging
    with Branches
    # log of changes to other_branch not in master
    $ git log NEW_BRANCH..master
    # diff of those changes
    $ git diff NEW_BRANCH..master

    View Slide

  51. Merging
    # get back to the master
    $ git checkout master
    # merge in changes from your other branch
    $ git merge NEW_BRANCH
    # optionally, delete the branch
    $ git branch -d NEW_BRANCH

    View Slide

  52. Rebasing
    # store local changes not in BRANCH_NAME as
    patches, updates the local branch to BRANCH_NAME,
    then applies the patches
    $ git rebase BRANCH_NAME

    View Slide

  53. Danger, Will Robinson
    • Rebase is dangerous
    • Rewrites commit history
    • Don’t use on a branch you’re sharing

    View Slide

  54. Oh noes!
    Merge Conflicts!

    View Slide

  55. Easy Conflict
    Resolution
    $ git merge conflict_branch
    Auto-merged README
    CONFLICT (content): Merge conflict in README
    Automatic merge failed; fix conflicts and then
    commit the result.
    # fix conflict then commit
    $ vim README
    $ git add README
    $ git commit -m “fixed merge conflict”

    View Slide

  56. Collaboration with GIT

    View Slide

  57. Hosting a Git Repo
    • git-daemon
    • gitosis

    View Slide

  58. Hosting a Git Repo
    • git-daemon
    • gitosis

    View Slide

  59. github.com

    View Slide

  60. Fork your friends

    View Slide

  61. ‘MySpace for hackers’

    View Slide

  62. Track forks of your repository

    View Slide

  63. Free for public repos

    View Slide

  64. • Still in beta
    • Email me at [email protected] if you’d
    like an invite (20 left)

    View Slide

  65. Common Use Cases
    • Contributor
    • Create Patches
    • Send Patches
    • Maintainer
    • Review Patches
    • Apply Patches

    View Slide

  66. Contributor

    View Slide

  67. Fork, then clone
    • Fork repo at github
    http://github.com/jnewland/atlrug-demo/tree/master
    • Clone your copy of my repo
    $ git clone [email protected]/USERNAME/atlrug-demo.git

    View Slide

  68. Make changes
    $ git checkout -b my_branch
    $ echo “hello again” >> README
    $ git commit -a

    View Slide

  69. Track the upstream
    # add a remote
    $ git remote add jnewland git://github.com/
    jnewland/atlrug-demo.git
    # add a branch
    $ git checkout -b jnewland/master
    # update the tracking branch
    $ git pull jnewland master

    View Slide

  70. Merge it all together
    # switch back to the master
    $ git checkout master
    # update master with your changes
    $ git merge my_branch
    # update master with upstream changes
    $ git merge jnewland/master

    View Slide

  71. Push it real good
    $ git push origin master

    View Slide

  72. Sending Patches
    • Old skool:
    $ git format-patch jnewland
    • New hotness - ‘Pull Request’

    View Slide

  73. No more emailing patches

    View Slide

  74. No more emailing patches

    View Slide

  75. Maintainer

    View Slide

  76. Receive pull request

    View Slide

  77. Grab mtodd’s changes
    # add a remote
    $ git remote add mtodd git://github.com/mtodd/
    atlrug-demo.git
    # add a branch
    $ git checkout -b mtodd/master
    # pull the changes
    $ git pull mtodd master

    View Slide

  78. Merge, then push!
    # switch back to master
    $ git checkout master
    # merge
    $ git merge mtodd/master
    # push
    $ git push

    View Slide

  79. SVN Integration

    View Slide

  80. “the best part about GIT
    is that no one has to
    know you’re using it”

    View Slide

  81. basic git-svn workflow
    $ git svn clone REPO_URL
    # ... hack hack hack ...
    $ git commit -a
    # ... hack hack hack ...
    $ git commit -a
    $ git svn rebase
    $ git svn dcommit

    View Slide

  82. better workflow
    $ git svn clone REPO_URL
    $ git checkout -b new_branch
    # ... hack hack hack ...
    $ git commit -a
    $ git svn rebase
    $ git svn dcommit
    $ git checkout master
    $ git branch -d new_branch
    $ git svn rebase

    View Slide

  83. Pretty GUIs

    View Slide

  84. gitk
    • Bundled with git
    • Excellent visualization of
    branching history

    View Slide

  85. GitNub
    • http://github.com/Caged/
    gitnub/tree/master
    • RubyCocoa

    View Slide

  86. More Resources
    • http://git.or.cz/
    • http://cheat.errtheblog.com/s/git/
    • http://github.com/guides
    • #git and #github on irc.freenode.org

    View Slide

  87. Questions?
    Comments?
    Flames?

    View Slide

  88. Jesse Newland
    [email protected]
    http://jnewland.com

    View Slide

  89. flickr is awesome
    • http://flickr.com/photos/alper/528441936/
    • http://flickr.com/photos/joan-fabregat/
    1947832858/
    • http://flickr.com/photos/feria/2316579746/

    View Slide