Git For The Android Developer

67
For the Android Developer Tony Hillerson, AnDevCon Fall 2011 #AnDevCon #effectiveui @thillerson http://www.slideshare.net/thillerson/git-for-android-developers Git

description

SL

Transcript of Git For The Android Developer

Page 1: Git For The Android Developer

For the Android DeveloperTony Hillerson, AnDevCon Fall 2011 #AnDevCon #effectiveui @thillersonhttp://www.slideshare.net/thillerson/git-for-android-developers

Git

Page 2: Git For The Android Developer

About Me

➡ Worked with Android and Git for a few years now➡ O’Reilly Screencaster: Developing Android Applications➡ http://training.oreilly.com/androidapps/➡ http://training.oreilly.com/androidapps2/➡ Tech Reviewer

Page 3: Git For The Android Developer

Conference App

➡ Get it, rate stuff

Page 4: Git For The Android Developer

Diving Right InLearning by Doing

Page 5: Git For The Android Developer

or

Simple Workflow

gitinit

changes

gitadd git commit

changes

gitadd git commit

changes

gitadd git commit

... ∞

86650c185eda50c9f9d58e2fbdf8b7113e5dee54

6facfd9f34173f4fb024196996e948a87c85eb56

b02ef5bf190e28ba24eab3ffab6133181cb5b5ef

gitclone

Page 6: Git For The Android Developer

.gitignore

➡ Can be nested deeply➡ https://github.com/github/gitignore

Page 7: Git For The Android Developer

Git Log - The Project’s History

➡ What got committed?➡ Commit messages➡ Content➡ When? Who?

Page 8: Git For The Android Developer

Remotes

➡ remote add➡ clone➡ fetch➡ pull➡ push

Page 9: Git For The Android Developer

Mac

master

Tagging

fb4f5d9 c5083fa 3f43fa3

git tag -a -m"Tagging v1.0" v1.0 c5083fa

➡ Both “-v1.0” and c5083fa will point to c5083fa

➡ Push this tag with `git push --tags`

➡ Can be cryptologically signed

Page 10: Git For The Android Developer

Recap of Simple Commands

➡ git init - Creates an empty Git repository➡ git add - Adds a file to the stage (“stages a file”)➡ git rm - Removes from version control➡ git commit - Commits the staged changes to the (local)

repository➡ git log - A view of the history➡ git tag - Names a commit➡ .gitignore - tells git to ignore certain files

Page 11: Git For The Android Developer

Why Source Control?

➡ For the solo developer?➡ Protection against mistakes➡ Freedom➡ ... to refactor➡ ... to experiment➡ For the development team?➡ All of the above, plus:➡ Parallel development➡ Merging different code branches

Page 12: Git For The Android Developer

PreliminariesGetting Git and Getting Set Up

Page 13: Git For The Android Developer

What’s a Git?

A completely ignorant, childish person with no manners.- http://urbandictionary.com

Linus Torvaldshttp://en.wikipedia.org/wiki/Linus_Torvalds

Page 14: Git For The Android Developer

What’s a Git?

Git is a free & open source, distributed version

control system designed to handle everything

from small to very large projects with speed and

efficiency. - http://git-scm.com

Page 15: Git For The Android Developer

Getting Set Up on Mac

➡ Homebrew - http://mxcl.github.com/homebrew/➡ MacPorts - http://www.macports.org/

Page 16: Git For The Android Developer

Getting Set Up on Windows

➡ msysgit -http://code.google.com/p/msysgit/

Page 17: Git For The Android Developer

Getting Set Up on Linux

➡ apt, etc - you probably know the drill

Page 18: Git For The Android Developer

Mac

WIN

Mac

MAC

Gooies!

➡ Git Tower - http://git-tower.com➡ Brother Bard’s GitX fork -

http://brotherbard.com/blog/2010/03/experimental-gitx-fork/

➡ Tortoise Git - http://code.google.com/p/tortoisegit/

Page 19: Git For The Android Developer

Eclipse Integration

➡ http://www.eclipse.org/egit/

Page 21: Git For The Android Developer

The Command LineA Short Sermon

Page 22: Git For The Android Developer

The Guts of GitThe Little Bits that Make Git Different

Page 23: Git For The Android Developer

What’s With all the Characters?

“... to have a probability of a SHA1-hash collision rise to 1/2, you need about 10^24 objects ...”

- Scott Chacon in Pro Git (paraphrased)

86650c185eda50c9f9d58e2fbdf8b7113e5dee54

➡ SHA1 Hash

➡ Uniquely identifies a commit

➡ Secure - very unlikely that someone can tamper with content in a repository

Page 24: Git For The Android Developer

In Git There Are Only...

➡ Blobs➡ Trees➡ Commits

Page 25: Git For The Android Developer

Blobs

➡ The contents of your files are stored as binary files

in .git/objects➡ Git is efficient. It only stores the same content once.➡ Identified by a SHA-1➡ Show blob contents with e.g. git show c7fb9f5

Page 26: Git For The Android Developer

Trees

➡ Trees give structure to blobs➡ Trees are also stored in .git/objects➡ Identified by SHA-1➡ View a tree with ls-tree, e.g. `git ls-tree HEAD`

Page 27: Git For The Android Developer

Commits

➡ Identified by a SHA-1➡ Points to one tree➡ Has a required message➡ May have one (or more) parent commit(s)➡ Show the reachable commits from a commit: git rev-list

HEAD

Page 28: Git For The Android Developer

Refs

➡ Point to commits➡ .git/refs/heads - the latest commits in local branches➡ HEAD - the latest commit on the current branch

Page 29: Git For The Android Developer

Blobs Are Content

b84ed8ed

e8d5cf6579a3b1

Page 30: Git For The Android Developer

Trees Give Structure

b84ed8ed

e8d5cf6

579a3b1

9899d2c

foo.txt

bar.txt

baz.html

3ffb35b /imagestrees can point to other trees

Page 31: Git For The Android Developer

Commits Point to Trees

b84ed8ed

e8d5cf6

579a3b1

9899d2c

foo.txt

bar.txt

baz.html

3ffb35b /images

d414c3e“Updated the main activity”

Page 32: Git For The Android Developer

Commits have Parents

d414c3e

090c953

4493671

c1d1f60

“Updated the main activity”

“Fixed bug #42”

“Added RoboGuice”

“Initial commit”

Page 33: Git For The Android Developer

Refs Point to Commits

d414c3e

090c953

4493671

c1d1f60

“Updated the main activity”

“Fixed bug #42”

“Added RoboGuice”

“Initial commit”

HEAD

Page 34: Git For The Android Developer

And That’s All You Need To Know About

Git

Page 35: Git For The Android Developer

And That’s All You Need To Know About

Git(Mostly)

Page 36: Git For The Android Developer

Sweet Moves with GitSweet, Sweet Moves

Page 37: Git For The Android Developer

Interactive AddBuilding Semantic Commits

Page 38: Git For The Android Developer

Interactive Add - Building Commits

➡ `git add` simply adds to the stage➡ `git commit -a` will commit all changes to tracked files

(add and commit)➡ `git add -i` -- a command line tool to interactively add

changes➡ Individual commits shouldn’t leave things broken➡ Try to commit some useful feature or bug fix all together

Page 39: Git For The Android Developer

Amending A CommitFix the Last Commit

Page 40: Git For The Android Developer

git commit --amend

➡ Oops! I misspelled something in the commit message➡ Oops! I did `git commit -a` and forgot to `git add` a file➡ Oops! I just committed a bug➡ USE ONLY BEFORE YOU SHARE CHANGES

Page 41: Git For The Android Developer

Git RevertMore Like Git Reverse

Page 42: Git For The Android Developer

git revert

➡ Commits the reverse of a commit➡ The previous commit is still there➡ != svn revert

Page 43: Git For The Android Developer

Git StashLike a Little Repo In Your Repo

Page 44: Git For The Android Developer

git stash

➡ remember: git help stash➡ Stash away changes in a safe place

Page 45: Git For The Android Developer

BranchingHitting Save Before You Fight the Level Boss

Page 46: Git For The Android Developer

Branching

➡ checkout -b➡ merging➡ rebasing

Page 47: Git For The Android Developer

How To Think About Branching

➡ Topic Branches➡ Mainline➡ What do you want “master” to mean?➡ Branching examples

Page 48: Git For The Android Developer

Topic Branches

➡ Branching is about controlling feature sets➡ Make a new branch for a story➡ Make a new branch for a bug fix➡ Make a new branch to spike something

Page 49: Git For The Android Developer

Team Branching Strategies

➡ What do you want “master” to mean?➡ Keep master deployable?➡ one strategy for web software➡ Use “master” as an integration branch?➡ Each developer uses topic branches and integrates to

master➡ Make a branch for releases

Page 50: Git For The Android Developer

Mac

add_login_activity

Mac

master

Branching

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

git checkout -b add_login_activity

Page 51: Git For The Android Developer

Mac

add_login_activity

Mac

master

Branching: Merging

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

3f43fa3

git checkout master

9aa8827 fe594ce ccb6f5e

git merge add_login_activity

Page 52: Git For The Android Developer

Mac

add_login_activity

Mac

master

Branching: Rebasing

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

3f43fa3

Mac

add_login_activity

Mac

master

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

3f43fa3

after`git rebase master`

before

Page 53: Git For The Android Developer

Branching: Rebasing

➡ Better than merging➡ Don’t use if you’ve pushed your branch to a remote➡ Can override with `git push -force`

Page 54: Git For The Android Developer

Git Pull --rebase

➡ Also available: `git pull --rebase`➡ Helpful for avoiding merge commits➡ May cause problems if git can’t automatically merge➡ `git reset HEAD` and start over with normal `git pull`

Page 55: Git For The Android Developer

Cherry PickI’ll Take One Of Those... And One Of Those...

Page 56: Git For The Android Developer

Mac

add_login_activity

Mac

master

Cherry-pick

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

3f43fa3

git cherry-pick fe594ceA new commit with the changes from

fe594ce

Page 57: Git For The Android Developer

Interactive RebaseRewrite History

Page 58: Git For The Android Developer

Interactive Rebase - Fixing History

➡ git rebase -i [commit]➡ A list of all commits in the current order➡ Reorder➡ Fix a certain commit➡ Squash commits together➡ Delete commits➡ DON’T USE AFTER YOU’VE PUSHED

Page 59: Git For The Android Developer

Whoops!Lots Of Ways To Fix It

Page 60: Git For The Android Developer

git checkout

➡ `git checkout [filename]` = remove all unstaged changes

Page 61: Git For The Android Developer

git reset

➡ `git reset [filename]` = opposite of `git add [filename]`➡ `git reset HEAD` = same as above - acts on all changes➡ `git reset HEAD^` (also ^^, or ~1, ~42, etc.) = rollback

commits to working tree➡ All examples of “mixed” reset

Page 62: Git For The Android Developer

git reset soft

➡ git reset --soft [commit]➡ Moves HEAD to commit➡ Puts the “popped” contents on the index

Page 63: Git For The Android Developer

git reset mixed (default)

➡ git reset [commit]➡ Moves HEAD to commit➡ Puts the “popped” contents on the index➡ Moves the index to the working tree as changes

Page 64: Git For The Android Developer

git reset hard

➡ git reset --hard [commit]➡ Moves HEAD to commit➡ Puts the “popped” contents on the index➡ Moves the index to the working tree as changes➡ Makes the working tree look like the index➡ DESTRUCTIVE

Page 65: Git For The Android Developer

git reset use cases

➡ Back that last commit up (git reset HEAD^)➡ Don’t forget `commit --amend`➡ Oops, didn’t mean to commit that file➡ I meant that commit to be on a branch!

Page 66: Git For The Android Developer

The Take Home

➡ SCM Is Important➡ No matter what kind of developer you are➡ Git is fundamentally different from the others➡ It’s not a database of patches➡ It’s a history of filesystem snapshots➡ It gives you freedom to innovate, make mistakes, and

collaborate.

Page 67: Git For The Android Developer

Git

Thank you!#AnDevCon#effectiveui@thillersonhttp://github.com/thillersonhttp://slideshare.com/thillerson

For the Android Developer • Tony Hillerson • AnDevCon Fall 2011

Half Off My Git Course These Slides