Git

89

Transcript of Git

Page 1: Git
Page 2: Git

jnewland.com/tag/git

Page 3: Git

What is Git?

• A popular distributed version control system designed to handle very large projects with speed and efficiency.

• http://git.or.cz/

Page 4: Git

quick survey

• Who is not using source control?

• SVN?

• CVS? (still?)

Page 5: Git

• Written by Linus Torvalds

• Written to manage the Linux Kernel

Git History

Page 6: Git

Getting Git

Page 8: Git

For the weak

sudo port install git-core +svn

sudo apt-get install git-core

sudo yum install git-core

Page 9: Git

Initial Setup$ git config --global user.name='My Name'

$ git config --global [email protected]

# optional, for pretty colors

$ git config --global color.diff=auto

$ git config --global color.diff.new=cyan

$ git config --global color.diff.old=magenta

$ git config --global color.diff.frag=yellow

$ git config --global color.diff.meta=green

$ git config --global color.diff.commit=normal

Page 10: Git

Why Git?

• Distributed, not Centralized

• Revolutionizes how you use branching

• Extremely stupidly ridiculously fast, even with large projects

• Community

Page 11: Git

Centralizedvs

Distributed

Page 12: Git

Centralized VCS

• Repository

• History stored in central location

• Checkout

• A ‘working tree’

Page 13: Git

Distributed VCS

• Every Git working directory is a full-fledged repository with full revision tracking capabilities, not dependent on network access or a central server.

• Commits happen offline.

• Commits can then be pushed and pulled between repositories with shared history.

Page 14: Git

Wait, commitshappen offline?

Page 15: Git

Commits happen offline!!!!!!!11!!!

Page 16: Git

on the plane

Page 17: Git

on the train

Page 18: Git

at that crappy coffee shop with the paid WiFi

Page 19: Git

at an ATLRUG meeting

Page 20: Git

Branching FTW

Page 21: Git

“But I don’t branch”

Page 22: Git

because you don’t use Git

Page 23: Git

quick survey #2

• How many of you use branching?

• Work exclusively on trunk/master?

• NEVER work on trunk/master?

Page 24: Git

Why branching with Git is awesome

• Instant$ time git checkout -b newbranch

Switched to a new branch "newbranch"

real 0m0.227s

• Private

• Merging doesn’t suck

Page 25: Git

Forget about ‘breaking the build’

• ‘Atomic’ commits are a thing of the past

• ‘Atomic’ merges

Page 26: Git

• Prototype well-developed changes

• Commit early and often

• Review and revise before you merge

Topic Branches

Page 27: Git

With Git, branches are now a part of my

everyday workflow

Page 28: Git

‘Tomorrow’ branch

Page 29: Git

‘Drunk’ branch

Page 30: Git

Performance

Page 31: Git

Offline Operations

• Performing a diff

• Viewing file history

• Committing changes

• Merging branches

• Obtaining any other revision of a file

• Switching branches

Page 32: Git

Online operationsare fast too

$ time svn co http://svn.rubyonrails.org/rails/trunk/

...

real 0m29.537s

Page 33: Git

Online operationsare fast too

$ time git clone --depth 1 git://github.com/josh/rails.git

...

real 0m9.088s

Page 34: Git

Online operationsare fast too

$ time git clone git://github.com/josh/rails.git

...

real 0m37.512s

Page 35: Git

Git for Subversion Users

Page 36: Git

This looks familiar

$ git status

$ git log

$ git blame

$ git add FILE

$ git rm FILE

$ git mv FILE

$ svn status

$ svn log

$ svn blame

$ git add FILE

$ git rm FILE

$ git mv FILE

Page 37: Git

Creating a Repo

$ pwd

~/src/foo

$ git init

$ git add .

$ git commit

Page 38: Git

Checking out a Repo

$ git clone REPO_URL

Page 39: Git

git status

• Untracked Files

• Brand new file

• Changed but not updated

• Locally changed file not in the index

• Changes to be committed

• ‘The Index’

Page 40: Git

The Index

• Staging area for your next commit

• Sort of like files marked A, M, D in svn status output

Page 41: Git

One major difference

• After making any changes to the working directory, and before running the commit command, you must use the add command to add any new or modified files to the index.

Page 42: Git

Example# create bar and to the index

echo “foo” > bar

git add bar

# change bar, and thus remove from the index

echo “ “ >> bar

# add bar to the index again

git add bar

Page 43: Git

Committing

• git commit

• commit what’s in the index

• git commit -a

• adds changed but not untracked files to the index, then commits

• exactly like SVN

Page 44: Git

Diffs# diff between working tree and the index

$ git diff

# diff between the index and last commit

$ git diff --cached

# diff between working tree and last commit

$ git diff HEAD

Page 45: Git

Logs# just like svn

$ git log

# find a commit changed a specific string

$ git log -S"def stupid_method"

# log with patches for each commit

$ git log -p

Page 46: Git

Reverting Changes

$ git checkout PATH $ svn revert PATH

Page 47: Git

git revert != svn revert

# most similar to svn revert

$ git checkout .

# reverse commit <rev> and commit the result

$ git revert <rev>

Page 48: Git

Branching# create a new branch

$ git branch NEW_BRANCH

# switch to this branch

$ git checkout NEW_BRANCH

# create a new branch and check it out in one step

$ git checkout -b NEW_BRANCH

Page 49: Git

More Branching# view available branches

$ git branch

* new_branch

master

# delete a branch

$ git branch -d ALREADY_MERGED_BRANCH

$ git branch -D BAD_BRANCH

Page 50: Git

Diffing and Logging with Branches

# log of changes to other_branch not in master

$ git log NEW_BRANCH..master

# diff of those changes

$ git diff NEW_BRANCH..master

Page 51: Git

Merging# get back to the master

$ git checkout master

# merge in changes from your other branch

$ git merge NEW_BRANCH

# optionally, delete the branch

$ git branch -d NEW_BRANCH

Page 52: Git

Rebasing

# store local changes not in BRANCH_NAME as patches, updates the local branch to BRANCH_NAME, then applies the patches

$ git rebase BRANCH_NAME

Page 53: Git

Danger, Will Robinson

• Rebase is dangerous

• Rewrites commit history

• Don’t use on a branch you’re sharing

Page 54: Git

Oh noes! Merge Conflicts!

Page 55: Git

Easy Conflict Resolution

$ git merge conflict_branchAuto-merged READMECONFLICT (content): Merge conflict in READMEAutomatic merge failed; fix conflicts and then commit the result.

# fix conflict then commit

$ vim README

$ git add README

$ git commit -m “fixed merge conflict”

Page 56: Git

Collaboration with GIT

Page 57: Git

Hosting a Git Repo

• git-daemon

• gitosis

Page 58: Git

Hosting a Git Repo

• git-daemon

• gitosis

Page 59: Git

github.com

Page 60: Git

Fork your friends

Page 61: Git

‘MySpace for hackers’

Page 62: Git

Track forks of your repository

Page 63: Git

Free for public repos

Page 64: Git

• Still in beta

• Email me at [email protected] if you’d like an invite (20 left)

Page 65: Git

Common Use Cases

• Contributor

• Create Patches

• Send Patches

• Maintainer

• Review Patches

• Apply Patches

Page 66: Git

Contributor

Page 67: Git

Fork, then clone

• Fork repo at githubhttp://github.com/jnewland/atlrug-demo/tree/master

• Clone your copy of my repo$ git clone [email protected]/USERNAME/atlrug-demo.git

Page 68: Git

Make changes

$ git checkout -b my_branch

$ echo “hello again” >> README

$ git commit -a

Page 69: Git

Track the upstream# add a remote

$ git remote add jnewland git://github.com/jnewland/atlrug-demo.git

# add a branch

$ git checkout -b jnewland/master

# update the tracking branch

$ git pull jnewland master

Page 70: Git

Merge it all together# switch back to the master

$ git checkout master

# update master with your changes

$ git merge my_branch

# update master with upstream changes

$ git merge jnewland/master

Page 71: Git

Push it real good

$ git push origin master

Page 72: Git

Sending Patches

• Old skool:$ git format-patch jnewland

• New hotness - ‘Pull Request’

Page 73: Git

No more emailing patches

Page 74: Git

No more emailing patches

Page 75: Git

Maintainer

Page 76: Git

Receive pull request

Page 77: Git

Grab mtodd’s changes# add a remote

$ git remote add mtodd git://github.com/mtodd/atlrug-demo.git

# add a branch

$ git checkout -b mtodd/master

# pull the changes

$ git pull mtodd master

Page 78: Git

Merge, then push!# switch back to master

$ git checkout master

# merge

$ git merge mtodd/master

# push

$ git push

Page 79: Git

SVN Integration

Page 80: Git

“the best part about GIT is that no one has to know you’re using it”

Page 81: Git

basic git-svn workflow

$ git svn clone REPO_URL

# ... hack hack hack ...

$ git commit -a

# ... hack hack hack ...

$ git commit -a

$ git svn rebase

$ git svn dcommit

Page 82: Git

better workflow$ git svn clone REPO_URL

$ git checkout -b new_branch

# ... hack hack hack ...

$ git commit -a

$ git svn rebase

$ git svn dcommit

$ git checkout master

$ git branch -d new_branch

$ git svn rebase

Page 83: Git

Pretty GUIs

Page 84: Git

gitk

• Bundled with git

• Excellent visualization of branching history

Page 85: Git

GitNub

• http://github.com/Caged/gitnub/tree/master

• RubyCocoa

Page 86: Git

More Resources

• http://git.or.cz/

• http://cheat.errtheblog.com/s/git/

• http://github.com/guides

• #git and #github on irc.freenode.org

Page 87: Git

Questions?Comments?

Flames?