Introduction to Git

701 views

Published on

This was a presentation I gave to the Boston PHP Meetup on October 2, 2013.

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

  • Be the first to like this

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

No notes for slide

Introduction to Git

  1. 1. Introduction to Git 10/2/2013 Rick Umali rickumali@gmail.com @rickumali http://tech.rickumali.com/ This presentation is on Google Drive at: http://sn.im/git-talk-2013 There you can read the 'speaker notes' for this presentation.
  2. 2. What is Source Control? Source code control is the most important practice a coding professional can do. A mechanism to track changes in source code. Used for version history, auditing, and recovery.
  3. 3. Revision Control Example: Wiki
  4. 4. Revision Control Example: Git You’ll learn how to make a repository like this.
  5. 5. Git Git is an open source, distributed version control system designed for speed and efficiency. Freedom No "server" required Unique architecture
  6. 6. Warning: Command Line Ahead
  7. 7. Our Example: A Basic Drupal Module
  8. 8. Creating a Repository % cd web/sites/all/modules % mkdir dumpstamp % cd dumpstamp % git init "git init" creates the entire Git repository. No server interaction required!
  9. 9. Committing Your First File To 'commit' means 'to save the state' of your work. You must first 'add' this change to the 'staging area'. % vi README.txt % git add README.txt % git commit -m "First commit. README file." Commit often!
  10. 10. Looking at the Repository History % git log % gitk
  11. 11. Adding More Files % vi dumpstamp.info dumpstamp.module % git status % git add . % git commit or % git commit -a -m “My commit message.” Learn the shortcuts by reading Git docs.
  12. 12. Examining Changes to Files % vi dumpstamp.module % git status % git diff % git add dumpstamp.module % git commit % git log Become familiar with Git status and diff output. Make your commit messages meaningful!
  13. 13. Looking at the Log Again The history can be examined different ways. % git log --format=short % git log --format=oneline % git log --oneline
  14. 14. Revisiting History You can 'revisit' any point of your history. % git checkout SHA_ID Checkout makes the working directory match the state of the specific SHA_ID. This is a time machine!
  15. 15. Returning to the Present At all times, Git retains a pointer to the ‘present’. % git checkout master
  16. 16. Branching and Merging Next, But... What we have covered so far is probably 70- 80% of what you will do with git. Adding and committing files are the heart of git (and any version control system).
  17. 17. Git encourages experimentation, by making branching very easy! Branching
  18. 18. Branching: git branch % git branch BRANCH % git checkout BRANCH This makes a branch from “where you currently are”, and then “switches” (checks out) the branch. “git branch” tells you what branch you’re on.
  19. 19. Branching: Starting State SHA 1Amaster NOTE: 'master' is a branch that's created 'by default'.
  20. 20. Branching: Make Some Changes SHA 1A SHA 2Bmaster git commit
  21. 21. Branching: Making a Branch SHA 1A SHA 2Bmaster branch1 git branch branch1 git checkout branch1 OR git checkout -b branch1
  22. 22. Branching: Changes on the Branch SHA 1A SHA 2Bmaster SHA 3C (Make changes in "branch1".) git commit branch1
  23. 23. Branching: Making a New Branch SHA 1A SHA 2Bmaster SHA 3C git checkout master git checkout -b branch2 branch1 branch2
  24. 24. Branching: Changes on another Branch SHA 1A SHA 2Bmaster branch2SHA 4D (Make changes in "branch2".) git commit branch1 SHA 3C
  25. 25. Visualizing the Branches
  26. 26. Merging Bringing two branches together. First 'checkout' the branch you want to merge into (typically master), then 'merge' in branch. % git checkout master % git merge BRANCH
  27. 27. Merging: Starting State SHA 1A SHA 2Bmaster branch2SHA 4Dbranch1 SHA 3C
  28. 28. Merging: Fast-Forward Merge git checkout master git merge branch1 SHA 1A SHA 2B branch2SHA 4Cmaster, branch1 SHA 3C
  29. 29. Merging: Resolving Conflicts git merge branch2 SHA 1A SHA 2B branch2SHA 4Cbranch1 SHA 3C SHA 5 master
  30. 30. Merging: The Hard Part Manual 'merging' may be required.
  31. 31. Visualizing the Merge
  32. 32. Whew!
  33. 33. Using Git “Remotely” You can upload your local Git repository to a public Git repository. These repositories are known as remotes. Using “remotes” is the key to collaborating.
  34. 34. Visualizing Remotes Your Repo Bob Repo GitHub Repo
  35. 35. Uploading New Code to GitHub Create a repository (on GitHub). Add a 'remote' (via git remote add). Upload your code (via git push).
  36. 36. Creating a Repository git init
  37. 37. Adding a Remote git remote adds a name for a repo at a specific URL git remote add origin git@github.com:rickumali/DumpStamp. git Your Repo GitHub Repo (origin) Dumpstamp.git This is just a naming step!
  38. 38. Push Your Repo to the Remote % git push -u origin master
  39. 39. Visualizing the Push git push uploads the repository to the remote Your Repo GitHub Repo (origin)
  40. 40. Visualizing Remotes (cloning) Your Repo Bob Repo GitHub Repo (origin) Now Bob can ‘clone’ your repository clone
  41. 41. Cloning
  42. 42. After the Clone
  43. 43. The Cycle with Remotes Your Repo Bob Repo GitHub Repo (origin) You Push, Bob “Pulls” (or Fetches/Merges) pullpush
  44. 44. You Saw and Learned A Lot About Git Typical Git Commands Add, Commit, Log, Diff, Status Branch and Merging Git Remote Repositories
  45. 45. Next Steps Install Git Commit frequently and log verbosely Experiment (branch) often
  46. 46. Introduction to Git 10/2/2013 Rick Umali rickumali@gmail.com @rickumali http://tech.rickumali.com/ This presentation is on Google Drive at: http://sn.im/git-talk-2013 There you can read the 'speaker notes' for this presentation.
  47. 47. Resources http://sethrobertson.github.io/GitBestPractices/ Great list of Git best practices. http://git-scm.org/ Both "Pro Git" book, and Git reference http://gitref.org/ A "quicker" Git reference http://www-cs-students.stanford.edu/~blynn/gitmagic/ "Friendlier" Git walk-through (git magic). http://www.mail-archive.com/dri-devel@lists. sourceforge.net/msg39091.html Linus on "clean history."
  48. 48. Resources http://www.youtube.com/watch?v=4XpnKHJAok8 Linus Torvalds (Git creator) (May '07) http://www.youtube.com/watch?v=8dhZ9BXQgc4 Randal Schwartz (Perl expert and Git old-timer) (Oct '07) http://www.youtube.com/watch?v=ZDR433b0HJY Scott Chacon (Pro Git author) (July '11)

×