Gitting the Most From Git

  • 652 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
652
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
15
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. Hello!
  • 2. Gitting the Most From Git
  • 3. Who am I?• For the people in the room: • You know me• Everyone else: • Chris Miller • Sr. Developer -- Huffington Post • Twitter: @ee99ee • Email: chris.miller@huffingtonpost.com
  • 4. Git? Git Wha?• When the office git expert has to come fix everything
  • 5. Git? Git Wha?• When the office git expert has to come fix everything
  • 6. Git Background
  • 7. Git Background• Distributed revision control system
  • 8. Git Background• Distributed revision control system• Developed by Linus Torvalds for Linux kernel development
  • 9. Git Background• Distributed revision control system• Developed by Linus Torvalds for Linux kernel development• Designed for distributed development
  • 10. Git Background• Distributed revision control system• Developed by Linus Torvalds for Linux kernel development• Designed for distributed development• Designed for large projects (large codebase)
  • 11. Git Repos
  • 12. Git Repos• What is a git repository (repo)?
  • 13. Git Repos• What is a git repository (repo)? • Directory of files managed by git
  • 14. Git Repos• What is a git repository (repo)? • Directory of files managed by git• It contains:Your files!
  • 15. Git Repos• What is a git repository (repo)? • Directory of files managed by git• It contains:Your files!• Special .git/ directory
  • 16. First Steps
  • 17. First Steps• Download and install git client (http://git-scm.com/downloads)
  • 18. First Steps• Download and install git client (http://git-scm.com/downloads)• Create the repo, add your files, and commit
  • 19. First Steps• Download and install git client (http://git-scm.com/downloads)• Create the repo, add your files, and commit • Initializeinit repo: git the
  • 20. First Steps• Download and install git client (http://git-scm.com/downloads)• Create the repo, add your files, and commit • Initializeinit repo: git the • Addgit add myfile.c your file(s):
  • 21. First Steps• Download and install git client (http://git-scm.com/downloads)• Create the repo, add your files, and commit • Initializeinit repo: git the • Addgit add myfile.c your file(s): • Commit!: git commit -m “I haz filez! yay!”
  • 22. Git ClientWhen someone says a git GUI is better than the command line?
  • 23. Git ClientWhen someone says a git GUI is better than the command line?
  • 24. The Basics
  • 25. The Basics• Git repositories are local
  • 26. The Basics• Git repositories are local • Repositories are relative to your working environment (not the server)
  • 27. The Basics• Git repositories are local • Repositories are relative to your working environment (not the server)• A commit goes to your local repository -- not some server (as in SVN)
  • 28. The Basics• Git repositories are local • Repositories are relative to your working environment (not the server)• A commit goes to your local repository -- not some server (as in SVN)• A commit is a snapshot -- A point in time
  • 29. The Basics• Git repositories are local • Repositories are relative to your working environment (not the server)• A commit goes to your local repository -- not some server (as in SVN)• A commit is a snapshot -- A point in time• Commits are metadata about the commit before and changes now
  • 30. Cloning• To get a remote repo, you need the URL • git clone [url]
  • 31. The Index• The index tracks what you want to commit• Files are in one of three states • Tracked • Ignored • Untracked
  • 32. Staging and Committing• Untracked files are “staged” prior to a commit
  • 33. Staging and Committing• Untracked files are “staged” prior to a commit [~/domains/gittalk]$ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test.c nothing added to commit but untracked files present (use "git add" to track)
  • 34. Staging and Committing• Untracked files are “staged” prior to a commit [~/domains/gittalk]$ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test.c nothing added to commit but untracked files present (use "git add" to track) [~/domains/gittalk]$ git add test.c
  • 35. Staging and Committing• Untracked files are “staged” prior to a commit [~/domains/gittalk]$ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test.c nothing added to commit but untracked files present (use "git add" to track) [~/domains/gittalk]$ git add test.c [~/domains/gittalk]$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: test.c #
  • 36. Staging and Committing• Untracked files are “staged” prior to a commit [~/domains/gittalk]$ git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # test.c nothing added to commit but untracked files present (use "git add" to track) [~/domains/gittalk]$ git add test.c [~/domains/gittalk]$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: test.c # [~/domains/gittalk]$ git commit -m “I haz moar codez” [master (root-commit) 3e44a84] I haz moar codez 1 file changed, 1 insertion(+) create mode 100644 test.c
  • 37. Removing and Moving• Removing a file from the file system is not enough.• You must tell git to remove from the repo as well (and commit that change)
  • 38. Removing and Moving• Removing a file from the file system is not enough.• You must tell git to remove from the repo as well (and commit that change) [~/domains/gittalk]$ git rm test.c rm test.c [~/domains/gittalk]$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # deleted: test.c # [~/domains/gittalk]$ git commit -m "I removed a file" [master e993a90] I removed a file 1 file changed, 1 deletion(-) delete mode 100644 test.c
  • 39. Removing and Moving• Removing a file from the file system is not enough.• You must tell git to remove from the repo as well (and commit that change) [~/domains/gittalk]$ git rm test.c rm test.c [~/domains/gittalk]$ git status # On branch master # Changes to be committed: • P. S. - Moving files works the same way # (use "git reset HEAD <file>..." to unstage) # # deleted: test.c # • Use git mv in the same sense [~/domains/gittalk]$ git commit -m "I removed a file" [master e993a90] I removed a file 1 file changed, 1 deletion(-) delete mode 100644 test.c
  • 40. Ignoring Stuff• Tell git to ignore files with .gitignore• Text file located in root of git repo• Tracked and committed like a normal file
  • 41. Ignoring Stuff /tmp/ *~ *.lock *.DS_Store• Tell git to ignore files with .gitignore *.swp *.out ~$*• Text file located in root of git repo [Tt]humbs.db /.svn/* */.svn/*• Tracked and committed like a normal file /CVS/* */CVS/* .cvsignore */.cvsignore .DS_Store Example from Huffington Post .gitignore: Icon? ._* .Spotlight-V100 .Trashes Desktop.ini *.hg *.project
  • 42. Peeking Inside• A single .git directory in root of project structure contains git meta data• Types of meta data: • Blobs • Trees • Commits • Tags
  • 43. Peeking Inside• Blobs (Binary Large Objects) are copies of the actual files• A SHA1 hash of each file is made and the contents stored as a filename of the hash. Example: • .git/objects/9da581d910c9c4ac93557ca4859e767f5caf5169
  • 44. Peeking Inside• Blobs (Binary Large Objects) are copies of the actual files• A SHA1 hash of each file is made and the contents stored as a filename of the hash. Example: • .git/objects/9da581d910c9c4ac93557ca4859e767f5caf5169• Each file stored only once• Identical files, regardless of directory, stored only once• SHA1 hash is identical on everyone’s machine • Same file always computes to the same hash
  • 45. Peeking Inside
  • 46. Peeking Inside• Tree: Mapping of directory path, blob id, and meta info (chmod, etc.)
  • 47. Peeking Inside• Tree: Mapping of directory path, blob id, and meta info (chmod, etc.)• Commit • Meta data about author, comment, and tree reference • Allows git to compile a complete snapshot of the tree at that given time
  • 48. Peeking Inside• Tree: Mapping of directory path, blob id, and meta info (chmod, etc.)• Commit • Meta data about author, comment, and tree reference • Allows git to compile a complete snapshot of the tree at that given time• Tags: Arbitrary, user-defined pointer to a given commit. Example: • Ver2.0-Alpha -> 9da581d910c9c4ac93557ca4859e767f5caf5169
  • 49. The Index and Committing • The index is a list of files and their blob references for the current state of the repo[~/domains/HuffingtonPost/huffpost-web]$ git ls-files -s100644 197687b4afab0816d33dcdfd68e462880bdac997 0www/public/twitter/public_info.php100644 0b5a713e78d33b211e334a5fe2df0d7d66eac3cb 0 www/public/twitter/quicktwitter_suggestion.php100644 d1ddd421d284e167079124cef22b9dd788952036 0 www/public/twitter/track_twitter_suggestions.php100644 89f1197c8d71064fbb4a4add6310b314c073f3cd 0 www/public/twitter/twitter_suggestions.php100644 7f57b7ac131aed04832034dc76c59ab30a92f020 0 www/public/typekey/regkey.txt100644 3a4bec59ae14a44834ef6dfeb1cc26a19a62d24d 0 www/public/uk-celebrity/includes/modules/ukCeleb_mod_Dl.inc.php100644 32e8b6a04ae8284288133764e62f7a067b63cf08 0www/public/uk-celebrity/includes/modules/ukCeleb_mod_FeaturedBlogs.inc.php100644 89e0386b2dffcf0f105280017d27367269a899d0 0 www/public/uk-celebrity/includes/modules/ukCeleb_mod_Photos.inc.php100644 91db4907100cd15728de94e5951900d2cdc91330 0 www/public/uk-celebrity/includes/modules/ukCeleb_mod_RRailModules.inc.php(....)
  • 50. The Index and Committing • The index is a list of files and their blob references for the current state of the repo[~/domains/HuffingtonPost/huffpost-web]$ git ls-files -s100644 197687b4afab0816d33dcdfd68e462880bdac997 0www/public/twitter/public_info.php100644 0b5a713e78d33b211e334a5fe2df0d7d66eac3cb 0 www/public/twitter/quicktwitter_suggestion.php100644 d1ddd421d284e167079124cef22b9dd788952036 0 www/public/twitter/track_twitter_suggestions.php100644 89f1197c8d71064fbb4a4add6310b314c073f3cd 0 www/public/twitter/twitter_suggestions.php100644 7f57b7ac131aed04832034dc76c59ab30a92f020 0 www/public/typekey/regkey.txt100644 3a4bec59ae14a44834ef6dfeb1cc26a19a62d24d 0 www/public/uk-celebrity/includes/modules/ukCeleb_mod_Dl.inc.php100644 32e8b6a04ae8284288133764e62f7a067b63cf08 0www/public/uk-celebrity/includes/modules/ukCeleb_mod_FeaturedBlogs.inc.php100644 89e0386b2dffcf0f105280017d27367269a899d0 0 www/public/uk-celebrity/includes/modules/ukCeleb_mod_Photos.inc.php100644 91db4907100cd15728de94e5951900d2cdc91330 0 www/public/uk-celebrity/includes/modules/ukCeleb_mod_RRailModules.inc.php(....) • Tip: To keep things brief, sometimes only the prefix is used. To lookup the full commit id: [~/domains/HuffingtonPost/huffpost-web]$ git rev-parse 3b18e512d 3b18e512dba79e4c8300dd08aeb37f8e728b8dad
  • 51. Committing: One more thing• Commit often• When in doubt, commit• Commit often -- no, really
  • 52. Branching• Fundamental way to create a separate line of development• Segment development of multiple features/bug fixes concurrently without one affecting the other• When in doubt, branch
  • 53. Branching• Fundamental way to create a separate line of development• Segment development of multiple features/bug fixes concurrently without one affecting the other• When in doubt, branch• Tip: I create a branch for every JIRA ticket and name it the ticket ID
  • 54. Branching
  • 55. Branching• Create a new branch: [~/domains/gittalk]$ git checkout -b MyFirstBranch Switched to a new branch MyFirstBranch
  • 56. Branching• Create a new branch: [~/domains/gittalk]$ git checkout -b MyFirstBranch Switched to a new branch MyFirstBranch• See what branch you are on: [~/domains/gittalk]$ git branch * MyFirstBranch master
  • 57. Branching• Create a new branch: [~/domains/gittalk]$ git checkout -b MyFirstBranch Switched to a new branch MyFirstBranch• See what branch you are on: [~/domains/gittalk]$ git branch * MyFirstBranch master• Switch branches [~/domains/gittalk]$ git checkout master Switched to branch master [~/domains/gittalk]$ git branch MyFirstBranch * master
  • 58. Sharing: I haz codez
  • 59. Sharing: I haz codez• Git has no central repo -- Your working directory is a repo
  • 60. Sharing: I haz codez• Git has no central repo -- Your working directory is a repo• Collaboration is done by “pushing” and “pulling” repos • A push is syncing changes in your repo to a remote repo • A pull is integrating changes to your repo from a remote repo
  • 61. Collaborating• When a coworker and I are working on the same branch:
  • 62. Collaborating• When a coworker and I are working on the same branch:
  • 63. Remote Repos• Any repo can be defined as a remote repo• When you “clone” a repo, that repo is automatically added as a remote repo called “origin”
  • 64. Remote Repos• Any repo can be defined as a remote repo• When you “clone” a repo, that repo is automatically added as a remote repo called “origin”• You can view remote repos and add more: [~/huffpost-web]$ git remote add menachem git@github.com:menachemd/huffpost-web.git [~/huffpost-web]$ git remote -v origin git@github.com:ee99ee/huffpost-web.git (fetch) origin git@github.com:ee99ee/huffpost-web.git (push) menachem git@github.com:menachemd/huffpost-web.git (fetch) menachem git@github.com:menachemd/huffpost-web.git (push) upstream git@github.com:huffingtonpost/huffpost-web.git (push) upstream git@github.com:huffingtonpost/huffpost-web.git (fetch)
  • 65. Pulling: Fetching & Merging
  • 66. Pulling: Fetching & Merging[~/huffpost-web]$ git fetch upstreamremote: Counting objects: 985, done.remote: Compressing objects: 100% (235/235), done.remote: Total 630 (delta 467), reused 539 (delta 381)Receiving objects: 100% (630/630), 89.97 KiB, done.Resolving deltas: 100% (467/467), completed with 173 local objects.From github.com:huffingtonpost/huffpost-web b6a5a33..95d0749 master -> upstream/master * [new tag] 20121107_174505 -> 20121107_174505From github.com:huffingtonpost/huffpost-web * [new tag] 20121107_165653 -> 20121107_165653 * [new tag] 20121107_171309 -> 20121107_171309
  • 67. Pulling: Fetching & Merging[~/huffpost-web]$ git fetch upstreamremote: Counting objects: 985, done.remote: Compressing objects: 100% (235/235), done.remote: Total 630 (delta 467), reused 539 (delta 381)Receiving objects: 100% (630/630), 89.97 KiB, done.Resolving deltas: 100% (467/467), completed with 173 local objects.From github.com:huffingtonpost/huffpost-web b6a5a33..95d0749 master -> upstream/master * [new tag] 20121107_174505 -> 20121107_174505From github.com:huffingtonpost/huffpost-web * [new tag] 20121107_165653 -> 20121107_165653 * [new tag] 20121107_171309 -> 20121107_171309[~/huffpost-web]$ git diff --summary upstream/master delete mode 100644 www/blogger/photofeed/script.js delete mode 100644 www/blogger/photofeed/style.css create mode 100644 www/editorial/_slideshows/helpers/5min.php delete mode 100644 www/editorial/commercial/cam_builder/images/trackpix.png delete mode 100644 www/editorial/commercial/cam_builder/js_script/modules/tracking_pixel.js (...)
  • 68. Pulling: Fetching & Merging[~/huffpost-web]$ git fetch upstreamremote: Counting objects: 985, done.remote: Compressing objects: 100% (235/235), done.remote: Total 630 (delta 467), reused 539 (delta 381)Receiving objects: 100% (630/630), 89.97 KiB, done.Resolving deltas: 100% (467/467), completed with 173 local objects.From github.com:huffingtonpost/huffpost-web b6a5a33..95d0749 master -> upstream/master * [new tag] 20121107_174505 -> 20121107_174505From github.com:huffingtonpost/huffpost-web * [new tag] 20121107_165653 -> 20121107_165653 * [new tag] 20121107_171309 -> 20121107_171309[~/huffpost-web]$ git diff --summary upstream/master delete mode 100644 www/blogger/photofeed/script.js delete mode 100644 www/blogger/photofeed/style.css create mode 100644 www/editorial/_slideshows/helpers/5min.php delete mode 100644 www/editorial/commercial/cam_builder/images/trackpix.png delete mode 100644 www/editorial/commercial/cam_builder/js_script/modules/tracking_pixel.js (...)[~/huffpost-web]$ git merge upstream/masterUpdating 41242d4..95d0749Fast-forward .gitignore | 1+ conf/apns_cert.pem | 60 +- conf/apns_key.pem | 50 +- (...)
  • 69. Pulling• The git pull command can be used to do git fetch + git merge automagically• I like to see what is going to merge before it does, so I use git fetch + git merge
  • 70. Pulling• Repos can get big... but, there is only one copy of each unique file
  • 71. Pulling• Repos can get big... but, there is only one copy of each unique file• How I feel when I fetch big repos:
  • 72. Pulling• Repos can get big... but, there is only one copy of each unique file• How I feel when I fetch big repos:
  • 73. Ye Olde Rebase
  • 74. Ye Olde Rebase• Git maintains a history of every commit
  • 75. Ye Olde Rebase• Git maintains a history of every commit• A merge creates a new commit point and adds changes to the end of the log
  • 76. Ye Olde Rebase• Git maintains a history of every commit• A merge creates a new commit point and adds changes to the end of the log• Git rebase rewrites the log -- it rewrites history Use with caution (but don’t avoid all together)
  • 77. Pushing• To send code to a remote repo, push: [~/huffpo-web]$ git push remote_name my_branch• Remember to commit before pushing
  • 78. Pushing• To send code to a remote repo, push: [~/huffpo-web]$ git push remote_name my_branch• Remember to commit before pushing When I push before committing:
  • 79. Github• If using github, you can issue “pull requests” to other users requesting they integrate your changes
  • 80. Pull Requests• The owner of the repo to whom you send the pull request can then merge your changes with theirs • This merge can be done on github.com if no conflicts exist • If conflict exists, a git fetch and git merge is necessary (by them)
  • 81. Pull Requests(Or they can ignore your pull request)
  • 82. Questions?
  • 83. One More Funny...How I feel when someone submits an IE6 bug report
  • 84. One More Funny...How I feel when someone submits an IE6 bug report