Your SlideShare is downloading. ×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Git Your Code Under Control

908

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 …

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
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
908
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
56
Comments
0
Likes
4
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

Transcript

  • 1. Git Your CodeUnder Controlmichael stowe 2013 MARCH 3, 2012 @mikegstowe
  • 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 CaringBridge.org (half a million visitors every day)• Zend Certified PHP 5.3 Software Engineer@mikegstowe
  • 3. WHAT THEHECK IS GIT?
  • 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. 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. WHAT IS A SCM?Source Code Management allows for themanagement of source code to avoid codingconflicts.
  • 7. DIFFERENT SCMSThere are a variety of Source Code Management Systemsavailable, including:• CVS• SVN• Git• Mercurial
  • 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. 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. 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. 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. 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. SETTING UP GITInstalling Git on your local machine is both quick andeasy, simply go to http://git-scm.com/downloads 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 athttp://git-scm.com/downloads/guis
  • 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 user.name “Your Name”git config --global user.email youremail@domain.com
  • 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. 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. 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. 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. 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. COMMITTING IN GITThis is NOT a descriptive message:git commit –m “Stuff…”
  • 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. CREATING BRANCHES IN GITAnd to create a new command, add the branch flag (-b)git checkout –b new_branch_name
  • 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. 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 (http://www.github.com)
  • 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 git@github.com:mikestowe/midwestphp.git
  • 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. 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. 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. 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. 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. 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. 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. 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. 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 http://git-scm.com/book/en
  • 35. THANK YOU. @mikegstowevisit mikestowe.com/slides for more on Security and PHP

×