55
For the Android Developer AnDevCon 2011 : Tony Hillerson #AnDevCon #eectiveui http://www.slideshare.net/thillerson/scm-for-android-developers-using-git Git

SCM for Android Developers Using Git

Embed Size (px)

DESCRIPTION

Delivered March 8 2011 at AnDevCon

Citation preview

Page 1: SCM for Android Developers Using Git

For the Android DeveloperAnDevCon 2011 : Tony Hillerson#AnDevCon #effectiveuihttp://www.slideshare.net/thillerson/scm-for-android-developers-using-git

Git

Page 2: SCM for Android Developers Using Git

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: SCM for Android Developers Using Git

PreliminariesGetting Git and Getting Set Up

Page 4: SCM for Android Developers Using Git

What’s a Git?

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

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

Page 5: SCM for Android Developers Using Git

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 6: SCM for Android Developers Using Git

Getting Set Up on Mac• Homebrew - http://mxcl.github.com/homebrew/

• MacPorts - http://www.macports.org/

Page 7: SCM for Android Developers Using Git

Getting Set Up on Windows• msysgit -http://code.google.com/p/msysgit/

• Cygwin - http://www.cygwin.com/

Page 8: SCM for Android Developers Using Git

Getting Set Up on Linux• apt, etc - you probably know the drill

Page 10: SCM for Android Developers Using Git

Eclipse Integration• http://www.eclipse.org/egit/

Page 12: SCM for Android Developers Using Git

Note• Code may be in backticks: `git <command>`

• Don’t input the backticks

Page 13: SCM for Android Developers Using Git

The Command LineA Short Sermon

Page 14: SCM for Android Developers Using Git

Seven Use CasesWhere Git will Make your Life Easier

Page 15: SCM for Android Developers Using Git

Project HistoryGit will Make your Life Easier

Page 16: SCM for Android Developers Using Git

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 17: SCM for Android Developers Using Git

Simple Commands• `git init`- Creates an empty Git repository

• .gitignore - tells git to ignore certain files

• `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`

• `git add -i` Interactive Add

Page 18: SCM for Android Developers Using Git

or

Simple Workflow

gitinit

changes

gitadd git commit

changes

gitadd git commit

changes

gitadd git commit

... ∞

86650c185eda50c9f9d58e2fbdf8b7113e5dee54

6facfd9f34173f4fb024196996e948a87c85eb56

b02ef5bf190e28ba24eab3ffab6133181cb5b5ef

gitclone

Page 19: SCM for Android Developers Using Git

.gitignore• Can be nested deeply

• https://github.com/github/gitignore

• Use it! Contribute!

Page 20: SCM for Android Developers Using Git

Git Log - The Project’s History• What got committed?

• Commit messages

• Content

• When? Who?

Page 21: SCM for Android Developers Using Git

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 22: SCM for Android Developers Using Git

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 23: SCM for Android Developers Using Git

Getting Out of TroubleGit will Make your Life Easier

Page 24: SCM for Android Developers Using Git

Advanced Commands• blame

• checkout

• commit -a

• reset

• stash

• commit --amend

• revert

Page 25: SCM for Android Developers Using Git

git blame

• remember: git help blame

• Who broke the build??? etc...

Page 26: SCM for Android Developers Using Git

git checkout, commit -a, reset

• remember: git help reset

• `git commit -a` = commit bypassing `git add`

• `git checkout <filename>` = remove all unstaged changes

• `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

• `git reset --soft` = rollback commit to stage

Page 27: SCM for Android Developers Using Git

git stash

• remember: git help stash

• Stash away changes in a safe place

Page 28: SCM for Android Developers Using Git

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 29: SCM for Android Developers Using Git

git revert

• Commits the reverse of a commit

• The previous commit is still there

• != svn revert

Page 30: SCM for Android Developers Using Git

CollaborationGit will Make your Life Easier

Page 31: SCM for Android Developers Using Git

Remotes• remote add

• push

• clone

• pull

• fetch

Page 32: SCM for Android Developers Using Git

Strategies• Star

• Peer to Peer

• Blessed Repository

• Hierarchical

Page 33: SCM for Android Developers Using Git

Star

“The Server”Committer

Committer

Committer

Committer

Committer

Committer

Page 34: SCM for Android Developers Using Git

• ssh://user@computer:path/to/repo.git

• http

• Gitosis

• Gitolite

Peer to N Peers

Peer Peer

Peer

Page 35: SCM for Android Developers Using Git

Hierarchical (Linux model)

Developer Developer

Lieutenant

Developer Developer

Lieutenant

Dictator ... etc. Maintains a “blessed repository”

Page 36: SCM for Android Developers Using Git

Topical DevelopmentGit will Make your Life Easier

Page 37: SCM for Android Developers Using Git

Branching• checkout -b

• merging

• Rebasing

• cherry-pick

Page 38: SCM for Android Developers Using Git

How To Think About Branching• Topic Branches

• Mainline

• What do you want “master” to mean?

• Branching examples

Page 39: SCM for Android Developers Using Git

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 40: SCM for Android Developers Using Git

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 41: SCM for Android Developers Using Git

Mac

add_login_activity

Mac

master

Branching

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

git checkout -b add_login_activity

Page 42: SCM for Android Developers Using Git

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 43: SCM for Android Developers Using Git

Mac

add_login_activity

Mac

master

Branching: Rebasing

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

3f43fa3

Mac

add_login_activit

Mac

master

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

3f43fa3

after`git rebase master`

before

Page 44: SCM for Android Developers Using Git

Branching: Rebasing• Better than merging

• Don’t use if you’ve pushed your branch to a remote

• Git will complain about refs

• Can override with `git push -force`

• Also available on `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 45: SCM for Android Developers Using Git

Mac

add_login_activity

Mac

master

Cherry-pick

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

3f43fa3

git cherry-pick fe594ce

A new commit with the changes

from fe594ce

Page 46: SCM for Android Developers Using Git

Cherry Picking• Doesn’t add a reference to the old commit

• Only applies the changes

• Future merges should apply cleanly

Page 47: SCM for Android Developers Using Git

Release ManagementGit will Make your Life Easier

Page 48: SCM for Android Developers Using Git

Tagging• tag

• push --tags

Page 49: SCM for Android Developers Using Git

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 50: SCM for Android Developers Using Git

Bug FixingGit will Make your Life Easier

Page 51: SCM for Android Developers Using Git

Bug Fixing• blame

• bisect

Page 52: SCM for Android Developers Using Git

Bisectgit bisect start

Macfb4f5d9 c5083fa 3f43fa3 9aa8827 fe594ce ccb6f5e 2e531cb

git bisect bad

Macfb4f5d9 c5083fa 3f43fa3 9aa8827 fe594ce ccb6f5e 2e531cb

git bisect good fb4f5d9

Macfb4f5d9 c5083fa 3f43fa3 9aa8827 fe594ce ccb6f5e 2e531cb

git bisect goodgit bisect goodgit bisect bad

Git tells you this was the first bad commit

HEAD

HEAD

HEAD

Page 53: SCM for Android Developers Using Git

Open Source CodeGit will Make your Life Easier

Page 54: SCM for Android Developers Using Git

Github.com• Search it!

• Forking

• Pull Requests

Page 55: SCM for Android Developers Using Git

Thank you!

For the Android DeveloperAnDevCon 2011 : Tony Hillerson

Git