Git Your Code Under Control

35
Git Your Code Under Control MARCH 3, 2012 @mikegstowe michael stowe 2013

description

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.

Transcript of Git Your Code Under Control

Git Your Code Under Control

MARCH 3, 2012 @mikegstowe

michael stowe

2013

• 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

.com

@mikegstowe

MIKESTOWE

WHAT THE HECK IS GIT?

WHAT IS GIT

According to Princeton, git is:

Rotter; a person who is deemed dispicable or

contemptible; “only a rotter would do that”; “kill that rat”

WHAT IS GIT

But what we’re talking about is a distributed

version control and source code management

(SCM) system with an emphasis on speed.

Git was initially designed for Linux, but now

supports Mac and Windows.

WHAT IS A SCM?

Source Code Management allows for the

management of source code to avoid coding

conflicts.

DIFFERENT SCMS

There are a variety of Source Code Management Systems

available, including:

• CVS

• SVN

• Git

• Mercurial

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

GIT VS SVN

SVN requires all users to use a central repository, where

all commits go to the central repository and are

immediately reflected in all user’s code when the SVN

update command is used.

Git is a multi-layered or dis repository system, where

each user has his/ her own repository. Specific commits

or repositories can then be pulled into the “central” or

any other repository.

GIT VS SVN

Git allows multiple users to collaborate on a project

without having to setup a separate repository or branch,

and work together seamlessly. It also allows for

developers to commit their code in stages (to ensure

code backups and logging) without impacting other

environments. Commits are made to their local

repository, and then pushed into a “central” repository

when appropriate.

WHAT IS GITHUB

GitHub is a web-based control panel for managing,

modifying, and sharing code via Git.

Perhaps better phrased, it is an online code repository

designed to provide access based controls to allow

management and sharing of code repositories.

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)

SETTING UP GIT

Installing Git on your local machine is both quick and

easy, simply go to http://git-scm.com/downloads and

click “Download” for your device.

Git also has many GUI clients that allow you to use Git

without having to write commands into the terminal. You

can download a Git client at

http://git-scm.com/downloads/guis

SETTING UP GIT

When setting up Git for the first time you will also want to

setup your identity. You can do this in the terminal using

the following commands:

git config --global user.name “Your Name”

git config --global user.email [email protected]

CREATING YOUR FIRST GIT REPO

Creating a new repository in Git is extremely easy. You

can do this using a GUI, or in your terminal or using Git

Bash cd to the directory you wish to have under version

control, and type the following command:

git init

ADDING FILES IN GIT

Now that we have our repository, we can add files to it

using 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

ADDING FILES IN GIT

It’s important to remember that you will need to add files

every time you make a change prior to committing. This

helps prevent accidental or unwanted changes from being

reflected in your repository.

CHECKING STATUS IN GIT

One powerful feature in Git is that we can see the status

of the files in our local repository:

git status

This will tell us the branch, which files have been added

(but not committed), which files have been changed, and

what files have been created but are not being tracked.

COMMITTING IN GIT

Keep in mind that adding a file to be tracked DOES NOT

add 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 sure

to write a descriptive message:

git commit –m “These are the changes I made…”

COMMITTING IN GIT

This is NOT a descriptive message:

git commit –m “Stuff…”

CREATING BRANCHES IN GIT

Sometimes you may want to create “branches” in Git.

This allows you to create a new project based on the

current code base, without affecting your master “trunk,”

this allows you to switch from ticket to ticket seamlessly

and easily, allowing you to work on multiple things at

once. To checkout a previously created branch (ie

master) use the checkout command:

git checkout master

CREATING BRANCHES IN GIT

And to create a new command, add the branch flag (-b)

git checkout –b new_branch_name

MERGING BRANCHES IN GIT

You can also merge branches with each other, merging

the changes from one branch in with another

git checkout –b new

… make changes

git add *

git commit -m “Did stuff”

git checkout master

git merge new

SETTING UP A REMOTE REPOSITORY

After creating your local repository on your server you will

want to setup a remote repository which can be used to

backup your code (in case your local machine dies), and

a way to share your code with other developers.

One of the best ways to do this is to setup an account at

GitHub (http://www.github.com)

SETTING UP A REMOTE REPOSITORY

Now, in Git Bash or your terminal, run the following

command:

git remote add repo_name repo_url

For my MidwestPHP repo, I would do the following:

git remote add origin [email protected]:mikestowe/midwestphp.git

PUSHING TO A REPOSITORY

Now we can push our commits to that repository using

the following command:

git push remote_repo branch

For my MidwestPHP repo, I would do the following:

git push origin master

PULLING FROM A REPOSITORY

We can also pull updates from the repository to our

current branch using:

git pull remote_repo

For my MidwestPHP repo, I would do the following:

git pull origin

Now you have a basic concept of using Git

and setting up a remote repository for your

application. However, Git provides a lot of

great functionality to help us do more to

manage our code.

GIT CHECKOUT

If you make changes to a file that you no longer want

prior to committing, you can use the checkout command:

git checkout file.txt

After committing a file it becomes a little more difficult to

revert it. To do this we can use the HEAD reset

command to remove/ undo commits from the branch.

GIT MOVE / REMOVE

Git also allows us to move or remove files and have those

changes tracked. To move a file use:

git mv file_old.txt file_new.txt

Or to remove a file from the repository:

git rm file.txt

GIT LOG

The Git Log command allows you to see a list of commits

for the current branch, or for specific files. For the entire

branch use:

git log

Or for a specific file:

git log file.txt

GIT BLAME

Git Blame can also be used to see who committed what

to what file.

git blame file.txt

Git blame tells you who, what, and when a file was

changed.

GIT DIFF

The Git Diff command allows you to view the file differences

prior to committing, or between revisions. You can view the

entire diff, or just the diff for specific files:

git diff

git diff file.txt

git diff revsion1 revision2

MORE GIT STUFF

You can see a full list of Git Commands using the following

command in the terminal:

git --help

Or by visiting the official Git handbook (available online for

free) at http://git-scm.com/book/en

THANK YOU. @mikegstowe

visit mikestowe.com/slides for more on Security and PHP