Git Your Code Under Control


Published on

Ever make a change only to realize the old way was better? Or did you just get done writing 400 lines of code only to have a co-worker overwrite the file by accident. Learn how to use Git and GitHub to manage your code base, including setting up a repository, checkout, commit, revert, branch, push/ pulls, utilize remote repositories, and more.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Git Your Code Under Control

  1. 1. Git Your CodeUnder Controlmichael stowe 2013 MARCH 3, 2012 @mikegstowe
  2. 2. MIKESTOWE .com• 10+ years experience hacking PHP• Author, Speaker, and Consultant, Overall Crazy Guy• Developed applications used by the medical field and law enforcement• Software Engineer at (half a million visitors every day)• Zend Certified PHP 5.3 Software Engineer@mikegstowe
  4. 4. WHAT IS GITAccording to Princeton, git is:Rotter; a person who is deemed dispicable orcontemptible; “only a rotter would do that”; “kill that rat”
  5. 5. WHAT IS GITBut what we’re talking about is a distributedversion control and source code management(SCM) system with an emphasis on speed.Git was initially designed for Linux, but nowsupports Mac and Windows.
  6. 6. WHAT IS A SCM?Source Code Management allows for themanagement of source code to avoid codingconflicts.
  7. 7. DIFFERENT SCMSThere are a variety of Source Code Management Systemsavailable, including:• CVS• SVN• Git• Mercurial
  8. 8. ADVANTAGES OF GIT• Local Repository (commit changes prior to submitting)• View changes in code prior to committing• Track Changes (via log and commit messages)• Create Branches for specific tickets/ tasks• “Blame” feature to see what code is by who• Widely used, supported by multiple systems• Integrated with Jira Bitbucket and GitHub
  9. 9. GIT VS SVNSVN requires all users to use a central repository, whereall commits go to the central repository and areimmediately reflected in all user’s code when the SVNupdate command is used.Git is a multi-layered or dis repository system, whereeach user has his/ her own repository. Specific commitsor repositories can then be pulled into the “central” orany other repository.
  10. 10. GIT VS SVNGit allows multiple users to collaborate on a projectwithout having to setup a separate repository or branch,and work together seamlessly. It also allows fordevelopers to commit their code in stages (to ensurecode backups and logging) without impacting otherenvironments. Commits are made to their localrepository, and then pushed into a “central” repositorywhen appropriate.
  11. 11. WHAT IS GITHUBGitHub is a web-based control panel for managing,modifying, and sharing code via Git.Perhaps better phrased, it is an online code repositorydesigned to provide access based controls to allowmanagement and sharing of code repositories.
  12. 12. ADVANTAGES OF GITHUB• Central Repository for Git Repositories• Easy management of the central repository• Easy management and manipulation of code• Quickly perform Pull Requests• Quickly share code with others• Built in Bug tracking system• Widely known and supported• Free (or paid for private repos)
  13. 13. SETTING UP GITInstalling Git on your local machine is both quick andeasy, simply go to andclick “Download” for your device.Git also has many GUI clients that allow you to use Gitwithout having to write commands into the terminal. Youcan download a Git client at
  14. 14. SETTING UP GITWhen setting up Git for the first time you will also want tosetup your identity. You can do this in the terminal usingthe following commands:git config --global “Your Name”git config --global
  15. 15. CREATING YOUR FIRST GIT REPOCreating a new repository in Git is extremely easy. Youcan do this using a GUI, or in your terminal or using GitBash cd to the directory you wish to have under versioncontrol, and type the following command:git init
  16. 16. ADDING FILES IN GITNow that we have our repository, we can add files to itusing the git add command.To add all files we can use:git add *Or to add specific files we can use:git add file.txt file2.txt file3.txt
  17. 17. ADDING FILES IN GITIt’s important to remember that you will need to add filesevery time you make a change prior to committing. Thishelps prevent accidental or unwanted changes from beingreflected in your repository.
  18. 18. CHECKING STATUS IN GITOne powerful feature in Git is that we can see the statusof the files in our local repository:git statusThis will tell us the branch, which files have been added(but not committed), which files have been changed, andwhat files have been created but are not being tracked.
  19. 19. COMMITTING IN GITKeep in mind that adding a file to be tracked DOES NOTadd it to the repository. Before changes become “final”(we can always revert changes) they must be committed.To commit messages, use the commit command. Be sureto write a descriptive message:git commit –m “These are the changes I made…”
  20. 20. COMMITTING IN GITThis is NOT a descriptive message:git commit –m “Stuff…”
  21. 21. CREATING BRANCHES IN GITSometimes you may want to create “branches” in Git.This allows you to create a new project based on thecurrent code base, without affecting your master “trunk,”this allows you to switch from ticket to ticket seamlesslyand easily, allowing you to work on multiple things atonce. To checkout a previously created branch (iemaster) use the checkout command:git checkout master
  22. 22. CREATING BRANCHES IN GITAnd to create a new command, add the branch flag (-b)git checkout –b new_branch_name
  23. 23. MERGING BRANCHES IN GITYou can also merge branches with each other, mergingthe changes from one branch in with anothergit checkout –b new… make changesgit add *git commit -m “Did stuff”git checkout mastergit merge new
  24. 24. SETTING UP A REMOTE REPOSITORYAfter creating your local repository on your server you willwant to setup a remote repository which can be used tobackup your code (in case your local machine dies), anda way to share your code with other developers.One of the best ways to do this is to setup an account atGitHub (
  25. 25. SETTING UP A REMOTE REPOSITORYNow, in Git Bash or your terminal, run the followingcommand:git remote add repo_name repo_urlFor my MidwestPHP repo, I would do the following:git remote add origin
  26. 26. PUSHING TO A REPOSITORYNow we can push our commits to that repository usingthe following command:git push remote_repo branchFor my MidwestPHP repo, I would do the following:git push origin master
  27. 27. PULLING FROM A REPOSITORYWe can also pull updates from the repository to ourcurrent branch using:git pull remote_repoFor my MidwestPHP repo, I would do the following:git pull origin
  28. 28. Now you have a basic concept of using Gitand setting up a remote repository for yourapplication. However, Git provides a lot ofgreat functionality to help us do more tomanage our code.
  29. 29. GIT CHECKOUTIf you make changes to a file that you no longer wantprior to committing, you can use the checkout command:git checkout file.txtAfter committing a file it becomes a little more difficult torevert it. To do this we can use the HEAD resetcommand to remove/ undo commits from the branch.
  30. 30. GIT MOVE / REMOVEGit also allows us to move or remove files and have thosechanges tracked. To move a file use:git mv file_old.txt file_new.txtOr to remove a file from the repository:git rm file.txt
  31. 31. GIT LOGThe Git Log command allows you to see a list of commitsfor the current branch, or for specific files. For the entirebranch use:git logOr for a specific file:git log file.txt
  32. 32. GIT BLAMEGit Blame can also be used to see who committed whatto what file.git blame file.txtGit blame tells you who, what, and when a file waschanged.
  33. 33. GIT DIFFThe Git Diff command allows you to view the file differencesprior to committing, or between revisions. You can view theentire diff, or just the diff for specific files:git diffgit diff file.txtgit diff revsion1 revision2
  34. 34. MORE GIT STUFFYou can see a full list of Git Commands using the followingcommand in the terminal:git --helpOr by visiting the official Git handbook (available online forfree) at
  35. 35. THANK YOU. @mikegstowevisit for more on Security and PHP