Introduction To Git Workshop

1,419 views

Published on

A workshop I ran at FSOSS 2013 introducing new users to git through a series of hands on exercises.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,419
On SlideShare
0
From Embeds
0
Number of Embeds
840
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Introduction To Git Workshop

  1. 1. Introduction To Git Workshop Tom Aratyn
  2. 2. Get Git • On Matrix • I have accounts for you • On Windows • http://msysgit.github.io • On OS X • brew install git • On Linux • sudo apt-get install git • Otherwise • http://git-scm.com
  3. 3. About Me Django and JavaScript Developer Founder @ The Boulevard Platform Engineer @ FoxyProxy Created open source projects: BitBucket Release Note Generator django-email-changer Exploit Me Suite
  4. 4. Why Git? Git is • • • • • Small Fast Distributed Free & Open Source Trusted • • • Linux Homebrew Everyone on GitHub + Gitorious +
  5. 5. Not Just For Code (but mainly for code) http://government.github.com
  6. 6. About Today What we will cover • Creating Repos • Checking Out • Committing • Basic Branching • Basic Merging • Pushing & Pulling What we won't • git reset • Changing history • Rebasing • Adding/Removing Remotes • Partial Staging • Fetch • Tags
  7. 7. Making A Git Repository Normal Repository Bare Repository Cloned Repository
  8. 8. A git repository with a working directory Normal Repository
  9. 9. Normal Repository Exercise $ git init workshop.normal.git
  10. 10. A git repo without a working directory (this is what you want on the repo) Bare Repository
  11. 11. Bare Repository Exercise $ git init --bare workshop.bare.git
  12. 12. Cloned Repository A normal repository is a copy of a remote repository and setup to work with it.
  13. 13. Cloned Repository Exercise $ git clone workshop.bare.git workshop.git $ cd workshop.git
  14. 14. Image By: Scott Chacon, Pro Git
  15. 15. Staging Files Before a file can be added it must be staged
  16. 16. Staging File Exercise $ mvim index.html $ git add index.html
  17. 17. What’s The Status Of My Files Right Now? $ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: index.html #
  18. 18. Committing Files Committing means that we want in the version control system. All staged files will be committed.
  19. 19. Commit File Exercise $ git commit –m "My initial commit"
  20. 20. Commit File Exercise Result Committer: Tom Aratyn <mystic@nelson.local> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email you@example.com After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 foo
  21. 21. The Log Git keeps a log of everything you commit, who wrote authored it, and who committed it.
  22. 22. View The Git Log Exercise $ git log $ git log --graph $ gitk
  23. 23. (Don’t worry there are better GUIs, more on that later)
  24. 24. Why isn't your name there?
  25. 25. The Parts Of A Log List of commits and branch pointers A commit is made up of: • • • • • SHA1 id Message Files Author Committer
  26. 26. Configuring git git is configured in two places: ~/.gitconfig youreRepo/.git/config
  27. 27. Configuring Git Exercise $ git config --global user.name "Your Name" $ git config --global user.email you@example.com
  28. 28. Configuring Git Exercise Result $ cat ~/.gitconfig [user] name = Tom Aratyn email = "tom@aratyn.name"
  29. 29. Changing History There are many ways to change history in git. We're only going to look at one way: Amend the last commit.
  30. 30. Change The Last Commit Exercise $ git commit --amend -m "initial commit with an html file" $ # has the author changed? $ gitk
  31. 31. Author Vs. Committer Git is made from from the ground up for multiple developer projects (Linux). Large projects often distinguish between the author (who wrote the patch/code) and the committer (who let it into the blessed repository)
  32. 32. Update the Author Exercise $ git commit --amend --resetauthor # why did the screen change ? # type in ":wq" to leave vim.
  33. 33. How To Remove A File? What if we committed a file we no longer need can we get rid of it? Yes & No
  34. 34. From the current (and future) versions. Yes, You Can Remove It
  35. 35. No, It'll Be In Past Versions The whole point of version control is you can always recover old files.
  36. 36. Remove A File Exercise $ git rm index.html $ ls $ #Notice how the file is now gone $ git status $ #Notice how the file is staged $ git commit -m "Removed index.html"
  37. 37. Create another index.html and commit it Practice: Adding a file
  38. 38. Branching Fast and easy branching is git's killer feature. Branches let development progress on multiple fronts separately and simultaneously.
  39. 39. Check Your Branch Exercise $ git branch $ git branch –a $ # What's the difference between the two commands?
  40. 40. Create A Branch Exercise $ git branch workshop-example $ git branch $ # what branch are you on?
  41. 41. Switch Branch Exercise $ git checkout workshop-example $ git branch $ # now what branch are you on?
  42. 42. Switch To A New Branch Immediately Exercise $ git checkout -b fix-bug-123 $ git branch $ gitk
  43. 43. Making A Change On A Branch Exercise $ # edit index.html $ git add index.html $ git commit -m "Added some initial html"
  44. 44. Merging Merging is really git's killer feature Because branching without merging is pretty useless See CVS
  45. 45. Merging Process 1. Go to the branch you want to merge into • Often the branch you branched off of. • Usually "master" or "develop" 2. Do the merge
  46. 46. Two Three types of merges 1. Fast Forward Merge 2. Basic Merge a. Conflicted Merge
  47. 47. Only available when the branch can be cleanly applied onto your current branch Fast Forward Merge
  48. 48. Fast Forward Merge Exercise $ # (assuming you have a change on fix-bug-123 - use gitk to check) $ git checkout master $ git merge fix-bug-123 $ gitk
  49. 49. Basic Merge Exercise Prep Add add a div on the master branch Change the title on the fixbug-123 branch Recall git checkout git add git commit
  50. 50. Basic Merge Exercise $ git checkout master $ git merge fix-bug-123 $ git log --graph --decorate --all
  51. 51. Conflicted Merge Exercise Prep Change the same line on both branches (change the class on the same div) Recall git checkout git add git commit
  52. 52. Conflicted Merge Exercise $ $ $ $ $ $ $ git checkout master git merge fix-bug-123 git status # edit index.html git add index.html git commit git log --graph --decorate --all
  53. 53. Sharing Is Caring So far everything we've done is on the same repo but projects need to be shared. Git lets you push your changes to others and pull the changes others made.
  54. 54. Pushing Exercise $ # Recall that we cloned our bare repo $ git push origin master $ cd ../workshop.bare.git $ git log
  55. 55. Pulling Exercise Prep 1. Clone the bare repo again • Call it workshop.2.git 2. Commit a change to workshop.git 3. Push the change Recall git clone git add git commit git push
  56. 56. Pulling Exercise $ $ $ $ cd ../workshop.2.git git branch git pull git log
  57. 57. How does pulling work?
  58. 58. Tracking Branches A tracking branch is a local branch which knows that updates to a remote branch should be applied to it.
  59. 59. Tracking Branch Exercise $ git checkout –t remotes/origin/fix-bug-123
  60. 60. About Today What we covered • Creating Repos • Checking Out • Committing • Basic Branching • Basic Merging • Pushing & Pulling What we didn't • git reset • Changing history • Rebasing • Adding/Removing Remotes • Partial Staging
  61. 61. Where to next? Learn more at from "Pro Git" http://git-scm.com/book Start Your Project: Free Open Source Repos http://github.com Free Private Repos http://bitbucket.org GUI: http://SourceTreeApp.com
  62. 62. Questions?
  63. 63. A link to this presentation will be on http://blog.tom.aratyn.name @themystic tom@aratyn.name (I can email it to you) Thank You!

×