power your workflow with git

211
Power Your Workflow With Git Patrick Hogan @pbhogan Credit: GitHub Octocat, http://octodex.github.com/

Upload: patrick-hogan

Post on 10-May-2015

6.628 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Power Your Workflow With Git

Power Your Workflow With GitPatrick Hogan

@pbhogan

Credit: GitHub Octocat, http://octodex.github.com/

Page 2: Power Your Workflow With Git

Scott ChaconVincent Driessen

Benjamin Sandofsky

Credits

Before I begin, I want to give special credit to these guys:Scott Chacon (Cha-kone) for much of the Git Internals content. He even sent me his slide deck.Vincent and Benjamin for their ideas on branching and workflow.

Page 3: Power Your Workflow With Git

Patrick Hogan

My name is Patrick Hogan

Page 4: Power Your Workflow With Git

I'm the founder of Gallant Games

Page 5: Power Your Workflow With Git

I have a game in the App Store called Swivel.

Page 6: Power Your Workflow With Git

Why This Talk?

I believe we are all creatives.Whether you’re a developer, designer or artist, you’re passionate about creating.To create you need tools. If your tools are frustrating they get in the way of your passion.I’m passionate about creating, so I’m fanatical about elegant tools.Elegant tools either help you or get out of your way.I believe Git is an elegant tool.

Page 7: Power Your Workflow With Git

Plus... Xcode uses Git for new projects by default, so you need to know about it.

Page 8: Power Your Workflow With Git

Approach

The first part of this talk will be a technical deep dive. I believe that to truly understand how to use Git, you have to know what Git is doing and how it thinks about your project. This demystifies a lot of the complexity and makes getting into Git a lot less scary.Once we get through the internals, we’ll examine workflows and how Git’s model can help you individually and your team as a collective.

Page 9: Power Your Workflow With Git

300?

So, this talk is classified 300 level.I tried to figure out what that meant.I asked Google, and it suggested...

Page 10: Power Your Workflow With Git

...this.So, strap in for a wild ride.

Page 11: Power Your Workflow With Git

Version Control

Workflow

For most people version control is a pain. At best it is a chore. As a result they erect a wall between their workflow and version control.Version control should inform your workflow, not hamper it.

Page 12: Power Your Workflow With Git

History

Let’s go back and see the progression.

Page 13: Power Your Workflow With Git

Local FilesystemLOCAL COMPUTER

File.m

File copy.m

File copy 2.m

So, we’ve all done this: right-click, duplicateIt starts to get unmanageable really fast.Whose ever gone back to their project and couldn’t remember which file is the correct one, or maybe what bits you wanted to save for later, or even why?Basically, we do this out of paranoia.

Page 14: Power Your Workflow With Git

LOCALVersion Control System

File.m

Version Database

Version 1

Version 2

Version 3

LOCAL COMPUTER

So, it didn’t take long for someone to come up with this.RCS, one of the first LVCS, was released in 1982 (29 years ago!)But if your computer crashes, it’s all gone.

Page 15: Power Your Workflow With Git

CENTRALIZEDVersion Control System

File.m

Mike

File.m

Jane

Version Server

Version 1

Version 2

Version 3

Version Server

Version 1

Version 2

Version 3

So, the natural progression was to store things on the server, and projects like CVS, SVN and others popped up. CVS in 1990, SVN in 2000Problem: everything is on the server. You need access to the server and you’d better not lose the server.

Page 16: Power Your Workflow With Git

DISTRIBUTEDVersion Control System

Version DB

Version 1

Version 2

Version 3

Mike

File.m

Version DB

Version 1

Version 2

Version 3

Sally

File.m

Version DB

Version 1

Version 2

Version 3

Jane

File.m

DVCS came along to solve a lot of the problems with existing VCS.Linux, for instance, switched to using BitKeeper to deal with their growth problems, and eventually switched to Git.Both Git and Mercurial popped up in 2005.

Page 17: Power Your Workflow With Git

DISTRIBUTEDVersion Control System

In many ways DVCS solves a lot of problems. It retains the best aspects of both local and centralized VCS, and has several benefits on top of that.

Page 18: Power Your Workflow With Git

Everything is Local(Almost)

One huge benefit is that almost all version control operations happen locally -- aside from sync, and then only if sync is not between two local repositories.

Page 19: Power Your Workflow With Git

No Network RequiredCreate Repo

CommitMergeBranchRebase

Tag

StatusRevisions

DiffHistory Bisect

Local Sync

None of these tasks require you to be connected to a server or any kind of network. You can be on a plane 30,000 ft up and do this stuff.

Page 20: Power Your Workflow With Git

Advantages

Everything is FastEverything is TransparentEvery Clone is a Backup

You Can Work Offline

That means...By transparent I mean you can literally inspect and see what Git is doing.

Page 21: Power Your Workflow With Git

Storage

So there are two ways VCS store their data, and if you come from Subversion this might be hard for you to get used to at first.

Page 22: Power Your Workflow With Git

Delta Storage

Page 23: Power Your Workflow With Git

Snapshot Storage(a.k.a. Direct Acyclic Graph)

Direct Acyclic Graph just means a graph where if you follow the nodes from one node you can’t get back to the that node. Doesn’t really matter... just think of it as “snapshot” storage.

Page 24: Power Your Workflow With Git

Commit 1

Commit 1

Commit 2

Commit 2

Commit 3

Commit 3

Commit 4

Commit 4

Commit 5

Commit 5

File A

File B

File A

File B

Δ1 Δ2 Δ3

Δ1 Δ2

A1 A1 A2 A3

B B1 B1 B2

DeltaStorage

DAG(Snapshot)Storage

Delta vs. Snapshot

So the point is that with the snapshot model, each commit takes a full snapshot of your entire working directory. That might seem weird, but has some advantages and it can be done really efficiently. We’ll see how when we get into the internals.Also, this is kind of how we think as developers. Typically you commit when your codebase reaches a certain state regardless of which files you had to mess with.

Page 25: Power Your Workflow With Git

DeltaStorage

DAG(Snapshot)Storage

Local Centralized Distributed

RCS

cp -r

rsync

Time Machine

CVS

Subversion

Perforce

darcs

Mercurial

bazaar

BitKeeper

git

Git is a distributed VCS that uses the snapshot storage model.

Page 26: Power Your Workflow With Git

About Git

Page 27: Power Your Workflow With Git

Linus Torvalds

Page 28: Power Your Workflow With Git
Page 29: Power Your Workflow With Git

http://bit.ly/linusongit

Linus on Git

This is required viewing.

Page 30: Power Your Workflow With Git

Git in a NutshellFree and Open Source

Distributed Version Control SystemDesigned to Handle Large Projects

Fast and EfficientExcellent Branching and Merging

Page 32: Power Your Workflow With Git

Under The Hood

Page 33: Power Your Workflow With Git

$ ls -lA-rw-r--r--@ 1 pbhogan staff 21508 Jul 3 15:21 .DS_Storedrwxr-xr-x 14 pbhogan staff 476 Jul 3 14:36 .git-rw-r--r--@ 1 pbhogan staff 115 Aug 11 2010 .gitignore-rw-r--r--@ 1 pbhogan staff 439 Dec 27 2010 Info.plistdrwxr-xr-x 17 pbhogan staff 578 Feb 6 10:54 Resourcesdrwxr-xr-x 7 pbhogan staff 238 Jul 18 2010 Source...

Git Directory

Page 34: Power Your Workflow With Git

Git Directory$ tree .git.git!"" HEAD!"" config!"" description!"" hooks#   !"" post-commit.sample#   $"" ...!"" info#   $"" exclude!"" objects#   !"" info#   $"" pack$"" refs !"" heads $"" tags

Page 35: Power Your Workflow With Git

.git only in root ofWorking Directory

(unlike Subversion)

Page 36: Power Your Workflow With Git

Git DirectoryConfiguration File

HooksObject Database

ReferencesIndex

Page 37: Power Your Workflow With Git

Git DirectoryConfiguration File

HooksObject Database

ReferencesIndex

Not going to discuss these here. You might never even touch these.

Page 38: Power Your Workflow With Git

Object Database

The Object Database is where a lot of the Git magic happens. It’s actually extremely simple. The approach Git takes is to have a really simple data model and then doing really smart things with.

Page 39: Power Your Workflow With Git

Object Database

≈ NSDictionary(Hash Table / Key-Value Store)

It’s really nothing more than a glorified on-disk NSDictionary -- or a hash table, if you like.

Page 40: Power Your Workflow With Git

content

Object Database

Ultimately it all comes down to storing a bit of content. We’ll talk about what that content is later, but given a piece of content...

Page 41: Power Your Workflow With Git

.git/objects/da/39a3ee5e6b4b0d3255bfef95601890afd80709

SHA1 digestda39a3ee5e6b4b0d3255bfef95601890afd80709

zlib deflate compressed1001110100111101110011110111011110110...

content

type + ' ' + size + \0

content

"loose format"

Object Database

Git appends it to a header with a type, a space, the size and a null byte...Calculates a hash (using SHA1 cryptographic hash)...Compresses the content and header...And writes it into a folder based on the hash.This is referred to as being stored in loose format.

Page 42: Power Your Workflow With Git

content

≈ NSDictionary

Pointer / Key Object / Valueda39a3ee5e6b4b0d3255bfef95601890afd80709

Object Database

Again, this is like a key-value database on disk.The hash is the key and the content is the value.What’s interesting is, because the key is a hash of the content, each bit of content in Git is kind of automatically cryptographically signed, and can be verified.

git cat-file -p da39a

Page 43: Power Your Workflow With Git

da39a3ee5e6b4b0d3255bfef95601890afd80709

da39a3ee5e6b4b0d3255...

da39a3ee5e6...

da39a...

Equivalent if common prefix is unique!

Object Database

What’s cool is Git considers any first part of the hash a valid key if it is unique so you don’t have to keep using a 40 character string.In fact, that’s more or less what I’m going to do for the rest of this talk so it all first on the slides. :)

Page 44: Power Your Workflow With Git

Garbage Collection

Object Database

Git has one more trick up it’s sleeve to keep things efficient.On certain operations, or on demand, Git will garbage collect, or really optimize the database.

Page 45: Power Your Workflow With Git

.git/objects/da/39a3...0709

.git/objects/e9/d71f...8f98

.git/objects/84/a516...dbb4

.git/objects/3c/3638...2874

.git/objects/58/e6b3...127f

Similar Objects

git gc

Object Database

So if Git knows it has certain similar objects (maybe versions of files, but can be anything really) and you run git gc...

Page 46: Power Your Workflow With Git

.git/objects/pack/0f35...183d.pack

.git/objects/pack/0f35...183d.idx

Δ1

Δ2

Δ3

Δ4

....git/objects/da/39a3...0709

.git/objects/e9/d71f...8f98

.git/objects/84/a516...dbb4

.git/objects/3c/3638...2874

.git/objects/58/e6b3...127f

"packed format"

Object Database

It’ll calculate deltas between those objects, and save them into a pack file and an index.This is referred to as being stored in packed format.

Page 47: Power Your Workflow With Git

Four Object Types

Object Database

The Object database has four data types (recall that it stores the type in the header)...

Page 48: Power Your Workflow With Git

blob tree

commit tag

Object Database

Blob... Tree... Commit... Tag...First up... blobs

Page 49: Power Your Workflow With Git

blob

README

Info.plist

Main.m

Project

Source

Working Directory Git Directory

blob: cd98f

blob: a3f6b

blob: 04e98

Object Database

Blobs essentially correspond to files.

Page 50: Power Your Workflow With Git

blob

#import <Cocoa/Cocoa.h>

int main(int argc, const char *argv[]){ return NSApplicationMain(argc, argv);}

blob 109\0

Object Database

This is how it’s stored, SHA1 hashed and compressed.Keep in mind that the same content will always have the same hash, so multiple files or versions of files with the exact same content will only be stored once (and may even be delta packed).So Git is able to be very efficient this way.

Page 51: Power Your Workflow With Git

blob tree

commit tag

Object Database

Next... trees

Page 52: Power Your Workflow With Git

README

Info.plist

Main.m

Project

Source

Working Directory Git Directory

blob: cd98f

blob: a3f6b

blob: 04e98

tree: bfef9

tree: 9a3ee

tree

Object Database

Trees correspond to directories or folders...

Page 53: Power Your Workflow With Git

tree

100644 blob cd98f README100644 blob a3f6b Info.plist040000 tree bfef9 Source

tree 84\0

Object Database

In this case the content is a POSIX like directory list of files (blobs) along with their hashes and some posix information.Given a tree, it’s easy to find the files and other trees in it by just looking for the hashes in the object database.

Page 54: Power Your Workflow With Git

blob tree

commit tag

Object Database

Next... commits

Page 55: Power Your Workflow With Git

blob

tree

tree

tree blob

blob

blob

commit

Object Database

A commit essentially just points to a tree (which is the pretty much the root of your working directory).So here you can see the snapshot model in action. Given a snapshot you can follow it and get your entire project -- all the files and folders -- and extract them from the database just by following the hashes.

Page 56: Power Your Workflow With Git

commit

tree 9a3eeparent fb39eauthor Patrick Hogan <[email protected]> 1311810904committer Patrick Hogan <[email protected]> 1311810904

Fixed a typo in README.

commit 155\0

Object Database

Header... Type, Hash...Parent commits (0 or more) -- 0 if first, 1 for normal commit, 2 or more if mergeAuthor, Committer, DateMessage

Page 57: Power Your Workflow With Git

blob

tree

commit

Object Database

Direct Acyclic Graph

Page 58: Power Your Workflow With Git

blob tree

commit tag

Object Database

And finally... tags

Page 59: Power Your Workflow With Git

blob

tree

commit

tag

Object Database

Tags just point to a commit. It’s really just a kind of named pointer to a commit since all commits are named by their hash.

Page 60: Power Your Workflow With Git

tag

object e4d23etype committag v1.2.0tagger Patrick Hogan <[email protected]> 1311810904

Version 1.2 release -- FINALLY!

tag 121\0

Object Database

.git/objects/20/c71174453dc760692cd1461275bf0cffeb772f

.git/refs/tags/v1.2.0

Page 61: Power Your Workflow With Git

blob tree

commit tag

Immutable!

Object Database

All of these objects are immutable. They cannot be changed. Content for a given key is essentially cryptographically signed.

Page 62: Power Your Workflow With Git

Never Removes Data(Almost)

Once committed -- Git almost never removes data.At least, Git will never remove data that is reachable in your history.The only way things become unreachable is if you “rewrite history”It’s actually very hard to lose data in Git.

Page 63: Power Your Workflow With Git

"Rewriting History"Writes Alternate History

While you’ll hear this phrase a lot, it actually isn’t true. Git doesn’t rewrite history. It simply writes an alternate history and points to that.git commit --amend, git commit --squash, git rebase

Page 64: Power Your Workflow With Git

blob

tree

Object Database

So if we have a file, change it and we amend a commit...

Page 65: Power Your Workflow With Git

tree

blob blob

Object Database

It keeps the old object, writes a new one and moves a pointer.This is called an unreachable object.These can be pruned and will not push to remotes. This is really the only way Git will lose data.And even then, you have to run git prune or equivalent.

Page 66: Power Your Workflow With Git

Configuration FileHooks

Object DatabaseReferences

Index

Git Directory

Next up... references

Page 67: Power Your Workflow With Git

References

Page 68: Power Your Workflow With Git

Lightweight, Movable Pointers to Commits

References

(and other things)

Page 69: Power Your Workflow With Git

v1.0

blob

tree

commit

tree

tag refstableremote/master

HEAD

References

Every git object is immutable, so a tag cannot be changed to point elsewhere.But we need pointers that can change... so we have refs.Refs are things like branch names (heads) which point to the latest commit in a given branch.There’s also HEAD (uppercase) which points exclusively to the latest commit of your currently active (checked out) branch. This is where Git operations will do their work.

Page 70: Power Your Workflow With Git

blob

tree

committag

ref

References

So here’s the whole Git object model. Everything in Git operates within this framework.It’s really simple, but it allows many complex operations.

Page 71: Power Your Workflow With Git

Scenario

So let’s run through a scenario to see this in action.

Page 72: Power Your Workflow With Git

branch

HEAD

tree

tree

tree

blob

blob

blob

commit

Scenario

change

So here we have our first commit. It has a few directories and three files...If we change this file at the bottom and commit...All of these other objects need to change too, because it’s parent tree points to it by hash and so on up the chain.But all objects are immutable, so...

Page 73: Power Your Workflow With Git

tree

tree

tree

blob

blob

blob

commit

tag

tree

tree

blob

tree

commit

branch

HEAD

Scenario

new objects

So git makes new objects with updated pointers...It writes the new blob, then updates its parent up the chain...Notice the commit points to its parent commit, and the two unchanged files are still pointed to...The branch and head can change because they’re references...Finally, we could tag this commit. Maybe it’s a release.

Page 74: Power Your Workflow With Git

tree

tree

tree

blob

blob

blob

commit

tag

tree

tree

blob

tree

commit

branch

HEAD

Scenario

change

So if we change this top blob now and make a third commit, it affects all the nodes above it...

Page 75: Power Your Workflow With Git

tree

tree

tree

blob

blob

blob

commit

tree

tree

blob

tree

commit

branch

HEAD

blob

tree

commit

tag

Scenario

Again, Git makes new objects...It writes a new blob...And a new tree...But not this tree...And a new commit and moves the branch head.

Page 76: Power Your Workflow With Git

tree

tree

tree

blob

blob

blob

commit

tree

tree

blob

tree

commit

branch

HEAD

blob

tree

commit

tag

Scenario

So that gives us this graph in our object database.And we can pull out...

Page 77: Power Your Workflow With Git

tree

tree

tree

blob

blob

blob

commit

tree

tree

blob

tree

commit

branch

HEAD

blob

tree

commit

tag

Scenario

This commit, or...

Page 78: Power Your Workflow With Git

tree

tree

tree

blob

blob

blob

commit

tree

tree

blob

tree

commit

branch

HEAD

blob

tree

commit

tag

Scenario

This commit, or...

Page 79: Power Your Workflow With Git

tree

tree

tree

blob

blob

blob

commit

tree

tree

blob

tree

commit

branch

HEAD

blob

tree

commit

tag

Scenario

This commit.And it’s all very fast because all Git needs to do is follow the links and extract the objects.

Page 80: Power Your Workflow With Git

Configuration FileHooks

Object DatabaseReferences

Index

Git Directory

Finally, the index....

Page 81: Power Your Workflow With Git

Index

Page 82: Power Your Workflow With Git

== Staging Area

Index

The index is essentially a staging area. It lets you craft your commits to be exactly what you want before you commit them.

Page 83: Power Your Workflow With Git

git checkout

git add

git commit

git status

Working Directory

Index

Repository

Index

It works like this.You change files in your working directory.Then you add files to your commit

Page 84: Power Your Workflow With Git

Index FTW

No Need To Commit All At OncePick (Stage) Logical Units to Commit

Helps You Review Your ChangesLets You Write Your History Cleanly

Page 85: Power Your Workflow With Git

Git Started

Page 86: Power Your Workflow With Git

New Project

Create and initialize the Git Directory (.git)$ git init

Page 87: Power Your Workflow With Git

Existing Project

$ git clone git://github.com/pbhogan/Archivist.git

Create and pull down remote repository.

Page 88: Power Your Workflow With Git

.gitignoreSpecify files which will be ignored by Git$ cat .gitgnore.svn.DS_Storebuild*.pbxuser*.perspective*.perspectivev3*.mode1v3*.mode2v3*.xcuserstate*.xcworkspacexcuserdata

Page 89: Power Your Workflow With Git

Staging

Stage files to the index.$ git add .

Page 90: Power Your Workflow With Git

Committing

Create a commit tagged with a message.$ git commit -m "My first commit!"

Git will force you to add a commit message.

Page 91: Power Your Workflow With Git

Branching & Merging

Page 92: Power Your Workflow With Git

Branching & Merging

$ git branch <name>

Create new branch (from current branch)

Page 93: Power Your Workflow With Git

Branching & Merging

$ git checkout <name>

Switch to branch (overwrites Working Dir!)

Page 94: Power Your Workflow With Git

$ git commit -m “First commit!”

blob

tree

commit

master

HEAD

OK, so here we have our first commit with one file.

Page 95: Power Your Workflow With Git

$ git commit -m “First commit!”

7f5ab

master

HEAD

represents commit + subtree

For simplicity, let’s just view the commit and everything beneath it like this.

Page 96: Power Your Workflow With Git

7f5ab

master

feature

HEAD

$ git branch feature$ git checkout feature

Now we create a new branch off of master.

Page 97: Power Your Workflow With Git

7f5ab

master

feature

HEAD

3d4ac

$ git add feat.c$ git commit -m “Added feat.c”

Let’s change something and commit.

Page 98: Power Your Workflow With Git

7f5ab

master

feature

HEAD

3d4ac 4da1f

$ git commit -a -m “Updated feat.c”feat.c already tracked so -a automatically stages.

And do it again. You can see we’ve left master behind, but the commits point back to where they came from.

Page 99: Power Your Workflow With Git

7f5ab

master

feature

3d4ac 4da1f

issue

HEAD

git checkout mastergit branch issuegit checkout issue

$ git checkout -b issue master

Now lets create yet another branch off of master.If you come from subversion, this many branches would probably give you an apoplexy, but it’s okay. Git is good at branches.That command at the top is a shortcut...

Page 100: Power Your Workflow With Git

master

feature

issue

HEAD

7f5ab

3d4ac 4da1f

5cb67

$ git add issue.c$ git commit -m “Added issue.c”

Page 101: Power Your Workflow With Git

master

feature

issue

HEAD

7f5ab

3d4ac 4da1f

5cb67 46fad

$ git commit -a -m “Updated issue.c”

Page 102: Power Your Workflow With Git

master

feature

issue

HEAD

7f5ab

3d4ac 4da1f

5cb67 46fad

c3d READMEf13 main.c

c3d READMEf13 main.cd4a issue.c

c3d READMEf13 main.c45e feat.c

c3d READMEf13 main.ce59 issue.c

c3d README27b main.c7e6 feat.c

changedsame

$ git log --stat

So if we run git log...We can see what Git sees... if we look at main.c, it’s the same in the 2nd commit and changed in the third.

Page 103: Power Your Workflow With Git

master

feature

issue

HEAD

7f5ab

3d4ac 4da1f

5cb67 46fad

c3d READMEf13 main.c

c3d READMEf13 main.cd4a issue.c

c3d READMEf13 main.c45e feat.c

c3d READMEf13 main.ce59 issue.c

c3d README27b main.c7e6 feat.c

same

same

$ git log --stat

Page 104: Power Your Workflow With Git

master

feature

issue

HEAD

7f5ab

3d4ac 4da1f

5cb67 46fad

c3d READMEf13 main.c

c3d READMEf13 main.cd4a issue.c

c3d READMEf13 main.c45e feat.c

c3d READMEf13 main.ce59 issue.c

c3d README27b main.c7e6 feat.c changedadded

$ git log --stat

If we look at feat.c, we can see it was added in the 2nd commit and then changed in the 3rd.

Page 105: Power Your Workflow With Git

master

feature

issue

HEAD

7f5ab

3d4ac 4da1f

5cb67 46fad

c3d READMEf13 main.c

c3d READMEf13 main.cd4a issue.c

c3d READMEf13 main.c45e feat.c

c3d READMEf13 main.ce59 issue.c

c3d README27b main.c7e6 feat.c

added

changed

$ git log --stat

Page 106: Power Your Workflow With Git

master

feature

issue

HEAD

7f5ab

3d4ac 4da1f

5cb67 46fad

Page 107: Power Your Workflow With Git

master

feature

issue

7f5ab

3d4ac 4da1f

5cb67 46fad

HEAD

$ git checkout master

Page 108: Power Your Workflow With Git

7f5ab

5cb67 46fad

master

feature

issue

3d4ac 4da1f

HEAD

7f5ab

5cb67 46fad fast-forward merge

$ git merge issue

Page 109: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad

HEAD

master issue

$ git merge issue

Page 110: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad

HEAD

master issue

4da1f

46fad non-fast-forward merge7f5ab

merge base

$ git merge feature

Page 111: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad

HEAD

29fcb

masterissue

3-way merge

$ git merge feature

4da1f

46fad

7f5ab

Page 112: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad

HEAD

29fcb

masterissue

c3d READMEf13 main.c

c3d READMEf13 main.ce59 issue.c

c3d README27b main.c7e6 feat.c

$ git merge feature

???

Page 113: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad

HEAD

29fcb

masterissue

c3d READMEf13 main.c

c3d READMEf13 main.ce59 issue.c

c3d README27b main.c7e6 feat.c

c3d README

$ git merge feature

Page 114: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad

HEAD

29fcb

masterissue

c3d READMEf13 main.c

c3d READMEf13 main.ce59 issue.c

c3d README27b main.c7e6 feat.c

c3d READMEc3d README27b main.c

$ git merge feature

Page 115: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad

HEAD

29fcb

masterissue

c3d READMEf13 main.c

c3d READMEf13 main.ce59 issue.c

c3d README27b main.c7e6 feat.c

c3d README27b main.cc3d README27b main.c7e6 feat.ce59 issue.c

$ git merge feature

Page 116: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad

issue

c3d READMEf13 main.c

c3d README27b main.c7e6 feat.c

c3d READMEd29 main.ce59 issue.c

MERGE CONFLICT!

$ git merge feature

What if main.c had been changed in both branches?

Page 117: Power Your Workflow With Git

Merge Conflict

$ git merge feature

Auto-merging main.cCONFLICT (content): Merge conflict in main.cAutomatic merge failed; fix conflicts and then commit the result.

Page 118: Power Your Workflow With Git

Merge Conflict$ git merge feature

# On branch master# Unmerged paths:# (use "git add/rm <file>..." as appropriate to mark resolution)### both modified: main.c#no changes added to commit (use "git add" and/or "git commit -a")

Page 119: Power Your Workflow With Git

Merge Conflict$ cat main.c

int main (int argc, char const *argv[]){<<<<<<< HEAD# printf( "Hola World!");=======# printf("Hello World!");>>>>>>> feature# return 0;}

Page 120: Power Your Workflow With Git

Merge Conflict

$ git mergetool$ git add main.c$ git commit -m “Merged feature”

Page 121: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad 29fcb

masterissue

HEAD

$ git checkout feature

Page 122: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad 29fcb

masterissue

HEAD

1ebc5

$ git commit -am “More on feature.”

Page 123: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad 29fcb

masterissue

1ebc5

HEAD

$ git checkout master

HEAD

Page 124: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad 29fcb

masterissue

1ebc5

HEAD

4da1f 1ebc5

29fcbreintegrationmerge

merge base

$ git merge feature

Reintegration merge

Page 125: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad 29fcb

masterissue

1ebc5

HEAD

cb53e

$ git merge feature

Reintegration merge

Page 126: Power Your Workflow With Git

feature

7f5ab

3d4ac 4da1f

5cb67 46fad 29fcb

masterissue

1ebc5

HEAD

cb53e

$ git branch -d feature

Page 127: Power Your Workflow With Git

7f5ab

3d4ac 4da1f

5cb67 46fad 29fcb

masterissue

1ebc5

HEAD

cb53e

$ git branch -d issue

Page 128: Power Your Workflow With Git

$ git branch -D <name>

Delete branch with extreme prejudice.

Branching & Merging

Page 129: Power Your Workflow With Git

Isolate ExperimentsIsolate Work Units

ParallelizeLong Running Topics

Hot Fix

Branching & Merging

Page 130: Power Your Workflow With Git

Merge vs. Rebase

Page 131: Power Your Workflow With Git

Merge vs. Rebase

c1master

Page 132: Power Your Workflow With Git

c1

master c2

Merge vs. Rebase

Page 133: Power Your Workflow With Git

c1

master

c2

c3

Merge vs. Rebase

Page 134: Power Your Workflow With Git

c1

c2

c3 c5 ideamaster

Merge vs. Rebase

Page 135: Power Your Workflow With Git

c1

c2

c3

c4

c5

ideac6master

Merge vs. Rebase

Page 136: Power Your Workflow With Git

merge rebase

c1

c2

c3

c4

c5

ideac6master

c1

c2

c3

c4

c5

ideac6master

Merge vs. Rebase

Page 137: Power Your Workflow With Git

merge rebase

c1

c2

c3

c4

c5

ideac6

master

c1

c2

c3

c4

c5

ideac6master

c7

merge commit

Merge vs. Rebase

Page 138: Power Your Workflow With Git

merge rebase

c1

c2

c3

c4

c5

ideac6

master c7

c1

c2

ideamaster c4 c6

c3 c5

Merge vs. Rebase

Page 139: Power Your Workflow With Git

merge rebase

c1

c2

c3

c4

c5

ideac6

master c7

c1

c2

ideamaster

c3'

c4'

1

2

1

2

c4 c6

c3 c5

Merge vs. Rebase

Page 140: Power Your Workflow With Git

merge rebase

c1

c2

c3

c4

c5

ideac6

master c7

c1

c2

idea

master

c3'

c4'

c4 c6

c3 c5

Merge vs. Rebase

Page 141: Power Your Workflow With Git

merge rebase

c1

c2

c3

c4

c5

ideac6

master c7

c1

c2

idea

master

c3'

c4'

c4 c6

c3 c5

Merge vs. Rebase

Page 142: Power Your Workflow With Git

Remotes

Git does not have a concept of a central server.It only has the concept of nodes -- other repositories.That can be another computer, or somewhere else on your file system.

Page 143: Power Your Workflow With Git

Remotes

remote == URL

Universal Repository Locator :)

Page 144: Power Your Workflow With Git

Protocolsssh://

http[s]://git://file://

rsync://ftp://

Page 145: Power Your Workflow With Git

ssh://http[s]://

git://file://

rsync://ftp://

push

push

pullpullpullpull

Protocols

Page 146: Power Your Workflow With Git

c1 c2

master

c1 c2

master

Ann's Computer

Bob's Computer

Company Server

Page 147: Power Your Workflow With Git

c1 c2

master

c1 c2

master

git clone [email protected]:project.git

c1 c2

masterorigin/master

Ann's Computer

Bob's Computer

Company Server

Page 148: Power Your Workflow With Git

c1 c2

master

c1 c2

master

c1 c2

master

origin/master

origin/master

git clone [email protected]:project.git

Ann's Computer

Bob's Computer

Company Server

Page 149: Power Your Workflow With Git

c1 c2

master

c1 c2

master

c1 c2

master

git commit -a -m "Ann's new feature"

origin/master

origin/master

a1 a2

Ann's Computer

Bob's Computer

Company Server

Page 150: Power Your Workflow With Git

c1 c2

master

c1 c2

master

c1 c2

master

origin/master

origin/master

a1 a2

git commit -a -m "Bob's new feature"

b1 b2

Ann's Computer

Bob's Computer

Company Server

Page 151: Power Your Workflow With Git

Ann's Computer

Bob's Computer

Company Server

c1 c2

master

c1 c2

master

c1 c2

master

origin/master

origin/master

a1 a2

b1 b2

git push origin master

b1 b2

Page 152: Power Your Workflow With Git

Ann's Computer

Bob's Computer

Company Server

c1 c2

master

c1 c2

master

c1 c2

origin/master

a1 a2

b1 b2

git push origin master

b1 b2

master

origin/master

Page 153: Power Your Workflow With Git

Ann's Computer

Bob's Computer

Company Server

c1 c2

master

c1 c2

master

c1 c2

origin/master

a1 a2

b1 b2

git push origin master

b1 b2

master

origin/master

Page 154: Power Your Workflow With Git

Ann's Computer

Bob's Computer

Company Server

c1 c2

master

c1 c2

master

c1 c2

origin/master

a1 a2

b1 b2

git fetch

b1 b2b1 b2

master

origin/master

Page 155: Power Your Workflow With Git

Ann's Computer

Bob's Computer

Company Server

c1 c2

master

c1 c2

master

c1 c2

a1 a2

b1 b2

b1 b2

git fetch

b1 b2

origin/master

master

origin/master

Page 156: Power Your Workflow With Git

Ann's Computer

Bob's Computer

Company Server

c1 c2

master

c1 c2

master

c1 c2

a1 a2

b1 b2

b1 b2

b1 b2

origin/master

git merge origin/master

a3

master

origin/master

Page 157: Power Your Workflow With Git

Ann's Computer

Bob's Computer

Company Server

c1 c2

c1 c2

master

c1 c2

a1 a2

b1 b2

b1 b2

b1 b2

origin/master

git push origin master

a3

master

a1 a2 a3

master

origin/master

Page 158: Power Your Workflow With Git

Ann's Computer

Bob's Computer

Company Server

c1 c2

c1 c2

master

c1 c2

a1 a2

b1 b2

b1 b2

b1 b2

git push origin master

a3

master

a1 a2 a3

master

origin/master

origin/master

Page 159: Power Your Workflow With Git

Ann's Computer

Bob's Computer

Company Server

c1 c2

c1 c2

master

c1 c2

a1 a2

b1 b2

b1 b2

b1 b2

a3

master

a1 a2 a3

master

origin/master

git checkout -b c2 bug37git commitgit commit

b3 b4

bug37

origin/master

Page 160: Power Your Workflow With Git

Ann's Computer

Bob's Computer

Company Server

c1 c2

c1 c2

master

c1 c2

a1 a2

b1 b2

b1 b2

b1 b2

a3

master

a1 a2 a3

master

origin/master

git push origin bug37b3 b4

bug37

origin/master b3 b4

bug37

Page 161: Power Your Workflow With Git

Ann's Computer

Bob's Computer

Company Server

c1 c2

c1 c2

master

c1 c2

a1 a2

b1 b2

b1 b2

b1 b2

a3

master

a1 a2 a3

master

origin/master

b3 b4

bug37

origin/master

b3 b4

origin/bug37

b3 b4

bug37

git fetch

Page 162: Power Your Workflow With Git

git pull==

fetch + merge

Can sometimes explode on you... safer to fetch then merge manually.

Page 163: Power Your Workflow With Git

Collaboration

Page 164: Power Your Workflow With Git

Centralized Model

SharedRepository

Developer

Developer

Developer

Developer

Developer

Developer

Developer

Developer

Page 165: Power Your Workflow With Git

Dictator ModelDictator Authoritative

Repository

Developer Developer Developer Developer

Lieutenant Lieutenant

Page 166: Power Your Workflow With Git

Peer to Peer Model

DeveloperPrivate

DeveloperPrivate

Developer Public

DeveloperPublic

Page 167: Power Your Workflow With Git

Integration Model

DeveloperPrivate

DeveloperPrivate

Developer Public

DeveloperPublic

IntegrationManager

AuthoritativeRepository

Page 168: Power Your Workflow With Git

Bottom Line

Many good collaboration modelsCustomize for your project / teamDistributed VCS gives you options

Page 169: Power Your Workflow With Git

Workflow

Page 170: Power Your Workflow With Git

Non WorkflowMake ChangesMore Changes

Break CodebaseRAGE!!!

Fix CodebaseRepeat

Page 171: Power Your Workflow With Git

Bad Workflow

Create ChangesCommit Changes

Page 172: Power Your Workflow With Git

Basic WorkflowCreate ChangesStage Changes

Review ChangesCommit Changes

Page 173: Power Your Workflow With Git

Workflow Pro Tip

Commit EarlyCommit OftenCommit Units

Page 174: Power Your Workflow With Git

v1.1

v2.0

v2.1

v1.2

v1.3

v1.0

Credit: Vincent Driessen, http://nvie.com/posts/a-successful-git-branching-model

Good Workflow

Page 175: Power Your Workflow With Git

DON’T PANIC!

Page 176: Power Your Workflow With Git

Master BranchAlways reflects PRODUCTION-READY state.

Always exists.

Develop BranchLatest DEVELOPMENT state for next release.Base your continuous integration on this.

Ultimately ends up in MASTER.Always exists.

Primary Branches

Page 177: Power Your Workflow With Git

Primary BranchesDEVELOP MASTER

v1.0

v2.0

v3.0

Start Developing

First Release (Tagged)

Ready for Release

Next Release

Next Release

Ready For Release

Ready for Release

But DEVELOP does not really reflect a STABLE state yet.

Page 178: Power Your Workflow With Git

Feature BranchesFine-grained work-in-progress for future release.

Branches off latest DEVELOP.Merges back into DEVELOP then discard.

Or just discard (failed experiments).Short or long running.

Typically in developer repositories only.Naming convention: feature / cool-new-feature

Secondary Branches

Page 179: Power Your Workflow With Git

Feature BranchesFEATURE DEVELOP

Branch Per Feature(a.k.a. Topic Branches)

Long Running Feature

Start Developing

Merge Feature

Always Branch From Latest

Merge Feature

Merge Feature

git merge --no-ff

Page 180: Power Your Workflow With Git

Release BranchesLatest RELEASE CANDIDATE state.

Preparatory work for release.Last minute QA, testing & bug fixes happens here.

Sits between DEVELOP and MASTER.Branch from DEVELOP.

Merge back into both MASTER and DEVELOP.Discard after merging.

Secondary Branches

Page 181: Power Your Workflow With Git

DEVELOP RELEASE MASTER

v1.0

v2.0

Release Branches

Branch From Develop

Release (Tagged)

Next Release

Final QA Fixes

Release Notes

Start Developing

Ready For Release

Reintegrate Changes

Reintegrate Release

Ready For Release

Reintegrate Release

Reintegrate Changes

Page 182: Power Your Workflow With Git

HotFix BranchesLike RELEASE, preparing for new release.

Resolve emergency problems with existing production release.Branch from MASTER.

Merge back into both MASTER and DEVELOP.Discard after merging.

Naming convention: hotfix / bug-157

Secondary Branches

Page 183: Power Your Workflow With Git

HotFix BranchesDEVELOP HOTFIX MASTER

v1.1

v1.2

v1.0 Existing Release

Branch From Release

HotFix Release (Tagged)

HotFix Release

Commit Fixes

Keep p0wning teh bugz!

Ongoing Development

Merge Bug Fix

Merge Bug Fix

Page 184: Power Your Workflow With Git

Support BranchesSimilar to MASTER + HOTFIX for legacy releases.

Branches off from earlier tagged MASTER.Does not merge back into anything.

Always exists once created.Continuing parallel master branch for a version series.

Naming convention: support / version-1

Secondary Branches

Page 185: Power Your Workflow With Git

HOTFIXMASTER SUPPORT

v2.0

v2.1

v1.0

v1.2

v1.3

v1.1

Support Branches1. First Release

Support Release (Tagged)

Support Release

3. Bug Discovered!

2. Second Release

Branch From Release

Could be cherry picked into DEVELOP if relevant.

Branch From Support

Page 186: Power Your Workflow With Git

All Together NowFEATURE DEVELOP RELEASE HOTFIX HOTFIXMASTER SUPPORT

v1.1

v2.0

v2.1

v1.2

v1.3

v1.0

Page 187: Power Your Workflow With Git

Public BranchesAuthoritative history of the project.

Commits are succinct and well-documented. As linear as possible.

Immutable.

Credit: Benjamin Sandofsky, http://sandofsky.com/blog/git-workflow.html

Public-Private Workflow

Page 188: Power Your Workflow With Git

Private BranchesDisposable and malleable.Kept in local repositories.

Never merge directly into public.First clean up (reset, rebase, squash, and amend)Then merge a pristine, single commit into public.

Public-Private Workflow

Credit: Benjamin Sandofsky, http://sandofsky.com/blog/git-workflow.html

Page 189: Power Your Workflow With Git

1. Create a private branch off a public branch.2. Regularly commit your work to this private branch.3. Once your code is perfect, clean up its history.4. Merge the cleaned-up branch back into the public branch.

Public-Private Workflow

Credit: Benjamin Sandofsky, http://sandofsky.com/blog/git-workflow.html

Page 190: Power Your Workflow With Git

Bottom Line

Every project is different. Custom design your workflow.

Branches are your LEGO blocks.

Page 191: Power Your Workflow With Git

Further Reading

http://nvie.com/posts/a-successful-git-branching-modelhttps://github.com/nvie/gitflow

http://sandofsky.com/blog/git-workflow.html

Page 192: Power Your Workflow With Git

Resources & Tools

Stuff I recommend.

Page 193: Power Your Workflow With Git

Choosing Git

http://whygitisbetterthanx.comby Scott Chacon

Page 194: Power Your Workflow With Git

Getting Git

http://git-scm.com

Page 195: Power Your Workflow With Git

Getting Git

Install Homebrewhttp://mxcl.github.com/homebrew/

$ brew install git

Not really Git related, but a great move overall and a fantastic way to keep Git up to date.Actually uses Git itself to keep itself up to date.

Page 196: Power Your Workflow With Git

Commanding Git

git config --global user.name "Patrick Hogan"git config --global user.email [email protected]

Edit ~/.profile

git config --global core.autocrlf inputgit config --global core.safecrlf true

git config --global color.ui true

Page 197: Power Your Workflow With Git

Commanding Git

source /usr/local/etc/bash_completion.d/git-completion.bash

RED="\[\033[0;31m\]"YELLOW="\[\033[0;33m\]"GREEN="\[\033[0;32m\]"WHITE="\[\033[1;37m\]"RESET="\[\033[1;0m\]"GIT='$(__git_ps1 "[%s]")'PS1="\n$WHITE\u$RESET: \W$YELLOW$GIT $GREEN\$$RESET "

Edit ~/.profile

pbhogan: Swivel[master] $

Page 198: Power Your Workflow With Git

Learning Git

http://progit.org/book/by Scott Chacon

It’s free, and available in e-book form (but buy it to support this Scott!)

Page 199: Power Your Workflow With Git

Learning Git

http://gitref.orgby the GitHub team

Page 200: Power Your Workflow With Git

Learning Git

http://gitimmersion.comby the EdgeCase team

Learn by doing approach.

Page 201: Power Your Workflow With Git

Hosting Git

http://github.comFree public repositories.

Free for public repositories — it’s where I host my open source stuff.$7 for 5 private repositories.

Page 202: Power Your Workflow With Git

GitHub Promo Code

http://github.comFree public repositories.

360iDEVph

Free for public repositories — it’s where I host my open source stuff.$7 for 5 private repositories.

Page 203: Power Your Workflow With Git

Hosting Git

https://github.com/sitaramc/gitoliteDIY team repositories hosting.

Gitolite allows you to setup git hosting on a central server, with very fine-grained access control.

Page 204: Power Your Workflow With Git

Hosting Git

http://dropbox.comDIY single user hosting :-)

Yes! This is how I host my private projects currently.

Page 205: Power Your Workflow With Git

Hosting Git

http://dropbox.comDIY single user hosting :-)

$ mkdir -p /Users/pbhogan/Dropbox/Repos/Swivel.git

$ cd /Users/pbhogan/Dropbox/Repos/Swivel.git

$ git init --bare

$ cd /Users/pbhogan/Projects/Swivel

$ git remote add dropbox file:///Users/pbhogan/Dropbox/Repos/Swivel.git

Here’s how. Basically just setting up a file:// remote to a location in your Dropbox.Dropbox takes care of the rest.SINGLE USER ONLY!!! Bad things will happen if you try this in a shared folder.

Page 206: Power Your Workflow With Git

Using Git

http://www.git-tower.comThe single best Git client — bar none.

This client is awesome and super polished. I use it every day at work.Has GitHub and Beanstalk integration.

Page 207: Power Your Workflow With Git

Tower Promo Code

http://www.git-tower.comThe single best Git client — bar none.

IDEV2011

This client is awesome and super polished. I use it every day at work.Has GitHub and Beanstalk integration.

Page 208: Power Your Workflow With Git

Using Git

http://mac.github.comFree. Works with GitHub only.

Nice looking client if you’re exclusively a GitHub user. I prefer Git Tower myself.

Page 209: Power Your Workflow With Git

Diffing Git

http://kaleidoscopeapp.comCompare files. Integrates with Tower.

A very sexy app for file comparison. Even compares images. Integrates with lots of other tools including Git Tower and command line.

Page 210: Power Your Workflow With Git

Merging Git

http://sourcegear.com/diffmergeDecent, free file merging tool.

A bit clunky and sluggish, but quite effective tool for helping you merge files. I hope Kaleidoscope will add merge conflict resolving one day so I can stop using this. Free.

Page 211: Power Your Workflow With Git

Patrick Hogan@pbhogan

[email protected]

Please rate this talk at: http://spkr8.com/t/7606

Tower Promo: IDEV2011 GitHub Promo: 360iDEVph10% discount 1 month free micro account