introduction to git

48
Introduction to Git 10/2/2013 Rick Umali [email protected] @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.

Upload: rickumali

Post on 06-May-2015

489 views

Category:

Technology


5 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Introduction to Git

Introduction to Git10/2/2013

Rick [email protected]

@rickumalihttp://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.

Page 2: Introduction to Git

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.

Page 3: Introduction to Git

Revision Control Example: Wiki

Page 4: Introduction to Git

Revision Control Example: Git

You’ll learn how to make a repository like this.

Page 5: Introduction to Git

Git

Git is an open source, distributed version control system designed for speed and efficiency.

Freedom

No "server" required

Unique architecture

Page 6: Introduction to Git

Warning: Command Line Ahead

Page 7: Introduction to Git

Our Example: A Basic Drupal Module

Page 8: Introduction to Git

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!

Page 9: Introduction to Git

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!

Page 10: Introduction to Git

Looking at the Repository History

% git log% gitk

Page 11: Introduction to Git

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.

Page 12: Introduction to Git

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!

Page 13: Introduction to Git

Looking at the Log Again

The history can be examined different ways.

% git log --format=short% git log --format=oneline% git log --oneline

Page 14: Introduction to Git

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!

Page 15: Introduction to Git

Returning to the Present

At all times, Git retains a pointer to the ‘present’.

% git checkout master

Page 16: Introduction to Git

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).

Page 17: Introduction to Git

Git encourages experimentation, by making branching very easy!

Branching

Page 18: Introduction to Git

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.

Page 19: Introduction to Git

Branching: Starting State

SHA 1Amaster

NOTE: 'master' is a branch that's created 'by default'.

Page 20: Introduction to Git

Branching: Make Some Changes

SHA 1A

SHA 2Bmaster

git commit

Page 21: Introduction to Git

Branching: Making a Branch

SHA 1A

SHA 2Bmaster branch1

git branch branch1git checkout branch1

OR git checkout -b branch1

Page 22: Introduction to Git

Branching: Changes on the Branch

SHA 1A

SHA 2Bmaster

SHA 3C

(Make changes in "branch1".)git commit

branch1

Page 23: Introduction to Git

Branching: Making a New Branch

SHA 1A

SHA 2Bmaster

SHA 3C

git checkout mastergit checkout -b branch2

branch1

branch2

Page 24: Introduction to Git

Branching: Changes on another Branch

SHA 1A

SHA 2Bmaster

branch2SHA 4D

(Make changes in "branch2".)git commit

branch1 SHA 3C

Page 25: Introduction to Git

Visualizing the Branches

Page 26: Introduction to Git

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

Page 27: Introduction to Git

Merging: Starting State

SHA 1A

SHA 2Bmaster

branch2SHA 4Dbranch1 SHA 3C

Page 28: Introduction to Git

Merging: Fast-Forward Merge

git checkout mastergit merge branch1

SHA 1A

SHA 2B

branch2SHA 4Cmaster, branch1 SHA 3C

Page 29: Introduction to Git

Merging: Resolving Conflicts

git merge branch2

SHA 1A

SHA 2B

branch2SHA 4Cbranch1 SHA 3C

SHA 5 master

Page 30: Introduction to Git

Merging: The Hard Part

Manual 'merging' may be required.

Page 31: Introduction to Git

Visualizing the Merge

Page 32: Introduction to Git

Whew!

Page 33: Introduction to Git

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.

Page 34: Introduction to Git

Visualizing Remotes

Your Repo Bob Repo

GitHub Repo

Page 35: Introduction to Git

Uploading New Code to GitHub

Create a repository (on GitHub).

Add a 'remote' (via git remote add).

Upload your code (via git push).

Page 36: Introduction to Git

Creating a Repository

git init

Page 37: Introduction to Git

Adding a Remote

git remote adds a name for a repo at a specific URLgit remote add origin [email protected]:rickumali/DumpStamp.git

Your Repo GitHub Repo(origin)

Dumpstamp.git

This is just a naming step!

Page 38: Introduction to Git

Push Your Repo to the Remote

% git push -u origin master

Page 39: Introduction to Git

Visualizing the Push

git push uploads the repository to the remote

Your Repo GitHub Repo(origin)

Page 40: Introduction to Git

Visualizing Remotes (cloning)

Your Repo Bob Repo

GitHub Repo(origin)

Now Bob can ‘clone’ your repository

clone

Page 41: Introduction to Git

Cloning

Page 42: Introduction to Git

After the Clone

Page 43: Introduction to Git

The Cycle with Remotes

Your Repo Bob Repo

GitHub Repo(origin)

You Push, Bob “Pulls” (or Fetches/Merges)

pullpush

Page 44: Introduction to Git

You Saw and Learned A Lot About Git

Typical Git CommandsAdd, Commit, Log, Diff, Status

Branch and Merging

Git Remote Repositories

Page 45: Introduction to Git

Next Steps

Install Git

Commit frequently and log verbosely

Experiment (branch) often

Page 46: Introduction to Git

Introduction to Git10/2/2013

Rick [email protected]

@rickumalihttp://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.

Page 47: Introduction to Git

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/[email protected]/msg39091.html

Linus on "clean history."

Page 48: Introduction to Git

Resources

http://www.youtube.com/watch?v=4XpnKHJAok8Linus Torvalds (Git creator) (May '07)

http://www.youtube.com/watch?v=8dhZ9BXQgc4Randal Schwartz (Perl expert and Git old-timer) (Oct

'07)http://www.youtube.com/watch?v=ZDR433b0HJY

Scott Chacon (Pro Git author) (July '11)