Git for Android Developers

Preview:

DESCRIPTION

Slides for a presentation I gave at AnDevCon

Citation preview

GitFor the Android Developer

Tony Hillerson, AnDevCon Fall 2013

#AnDevCon @tackmobile @thillerson

Presentation tackmobile.comPresentation tackmobile.com

About Me

• @thillerson, +thillerson

• Developer at Tack Mobile(tackmobile.com), @tackmobile

• Android, iOS, and Mobile Web

• Rails, Node, maybe Elixir (one day)

Diving Right InLearning by Doing

Image © Dennis Barneshttp://www.flickr.com/photos/dennisbarnes/2817664242

Presentation tackmobile.com

git init

changes

git add

git commit

changes

git add

git commit

changes

git add

git commit

... ∞

86650c185

6facfd9f3

b02ef5bf1

git cloneor

Presentation tackmobile.com

.gitignore

• Can be nested deeply

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

Presentation tackmobile.com

Git Log - The Project’s History

• What got committed?

• Commit messages

• Content

• When? Who?

Presentation tackmobile.com

Remotes

• remote add

• clone

• fetch

• pull

• push

Presentation tackmobile.com

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

Presentation tackmobile.com

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

Presentation tackmobile.com

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

PreliminariesGetting Git and Getting Set Up

Presentation tackmobile.com

What’s a Git?

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

Linus Torvalds http://en.wikipedia.org/wiki/Linus_Torvalds

Presentation tackmobile.com

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

Presentation tackmobile.com

Getting Set Up on Mac

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

• brew install git

• MacPortshttp://www.macports.org/

Presentation tackmobile.com

Getting Set Up on Windows

• msysgithttp://code.google.com/p/msysgit/

Presentation tackmobile.com

Getting Set Up on Linux

• apt, etc - you probably know the drill

Presentation tackmobile.com

Gooies!

• SourceTree (Mac and Windows)http://sourcetreeapp.com/

• TortoiseGit (Windows)http://code.google.com/p/tortoisegit/

Presentation tackmobile.com

IDE Integration

• Android StudioYou’re All Set

• Eclipsehttp://www.eclipse.org/egit/

Presentation tackmobile.com

Reference

• Githttp://git-scm.com/

• ProGithttp://progit.org/book/

• Insider Guide to Github http://www.pragprog.com/screencasts/v-scgithub/insider-guide-to-github

Presentation tackmobile.com

The Command LineA Short Sermon

The Guts of GitThe Little Bits that Make Git Different

Presentation tackmobile.com

What’s With all the Characters?

• SHA1 Hash e.g.86650c185eda50c9f9d58e2fbdf8b7113e5dee54

• Uniquely identifies a commit

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

Presentation tackmobile.com

SHA-1 Hash Keys

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

Presentation tackmobile.com

In Git There Are Only...

• Blobs

• Trees

• Commits

Presentation tackmobile.com

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`

Presentation tackmobile.com

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`

Presentation tackmobile.com

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`

Presentation tackmobile.com

Refs

• Point to commits

• .git/refs/heads - the latest commits in local branches

• HEAD - the latest commit on the current branch

Presentation tackmobile.com

Blobs Are Content

b84ed8ed

e8d5cf6579a3b1

Presentation tackmobile.com

Trees Give Structure

b84ed8ed

e8d5cf6

579a3b1

9899d2c

MainActivity.java

BarView.java

FooFragment.java

3ffb35b /anotherpackage

trees can point to other trees

com/yourcompany/androidapp

Presentation tackmobile.com

Commits Point to Trees

b84ed8ed

e8d5cf6

579a3b1

9899d2c

MainActivity.java

BarView.java

FooFragment.java

3ffb35b /anotherpackage

com/yourcompany/androidappd414c3e

“Fixed bug # 42”

Presentation tackmobile.com

Commits Have Parents

d414c3e

090c953

4493671

c1d1f60

“Updated the main activity”

“Fixed bug #42”

“Typed awesome code”

“Initial commit”

1

2

3

4

Presentation tackmobile.com

Refs Point to Commits

d414c3e

090c953

4493671

c1d1f60

“Updated the main activity”

“Fixed bug #42”

“Typed awesome code”

“Initial commit”

1

2

3

4HEAD

Presentation tackmobile.com

And That’s All You Need To Know About Git

Presentation tackmobile.com

And That’s All You Need To Know About Git

(Mostly)

Day to Day Git“What would you say you *do* here?”

Presentation tackmobile.com

Semantic CommitsMake Commits Mean Something

Presentation tackmobile.com

Semantic Commits

• Git’s not just a big truck

• Commits should each mean something

Presentation tackmobile.com

Interactive Add - Building Semantic 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

Presentation tackmobile.com

Interactive Add - SourceTree

Presentation tackmobile.com

BranchingLike Hitting Save Before You Fight the Level Boss

Presentation tackmobile.com

Branching

• New branch: git checkout -b <name>

• A branch is a named ref

• merging

• rebasing

Presentation tackmobile.com

How To Think About Branching

• Mainline

• What do you want “master” to mean?

• Topic Branches

• Branching examples

Presentation tackmobile.com

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

Presentation tackmobile.com

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

Presentation tackmobile.com

master

Branching

fb4f5d9 c5083fa

add_login_activity

9aa8827 fe594ce ccb6f5e

git checkout -b add_login_activity

Presentation tackmobile.com

add_login_activity

master

Branching: Merging

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

3f43fa3

git checkout master

9aa8827 fe594ce ccb6f5e

git merge add_login_activity

Presentation tackmobile.com

add_login_activity

master

Branching: Rebasing

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

3f43fa3

add_login_activity

master

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

3f43fa3

after `git rebase master`

before

Presentation tackmobile.com

Branching: Rebasing

• Better than merging in some ways...

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

• Can override with `git push -force`

• … but don’t

Presentation tackmobile.com

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`

Presentation tackmobile.com

Git StashLike a Little Repo In Your Repo

Presentation tackmobile.com

git stash

• remember: git help stash

• Stash away changes in a safe place

• A Workflow:Stash -> Fix & Commit -> Pop Stash

Presentation tackmobile.com

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

Presentation tackmobile.com

Cherry Pick

• When you need a commit’s changes

• But not its history

Presentation tackmobile.com

add_login_activity

master

Cherry-pick

fb4f5d9 c5083fa

9aa8827 fe594ce ccb6f5e

git cherry-pick fe594ce

3f43fa3

A new commit with the changes

from fe594ce

Presentation tackmobile.com

Whoops!Lots Of Ways To Fix It

Presentation tackmobile.com

git checkout

• `git checkout [filename] ̀=remove unstaged changes

Presentation tackmobile.com

git revert

• Commits the reverse of a commit

• The previous commit is still there

• != svn revert

Presentation tackmobile.com

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

Presentation tackmobile.com

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

Presentation tackmobile.com

git reset

• `git reset [filename]` = opposite of `git add [filename]`

• `git reset HEAD` = same as above - acts on all tracked changes

• `git reset HEAD^` (also ^^, or ~1, ~42, etc.) = rollback commits to working tree

• All examples of “mixed” reset

Presentation tackmobile.com

git reset --soft [commit]

1. Moves HEAD to [commit]

2. Puts the “popped” contents on the index

Presentation tackmobile.com

git reset [commit] (“mixed” - default)

1. Moves HEAD to [commit]

2. Puts the “popped” contents on the index

3. Moves the index’s changes to the working tree

4. Clears the index

Presentation tackmobile.com

git reset --hard [commit] (DESTRUCTIVE!!!)

1. Moves HEAD to [commit]

2. Puts the “popped” contents on the index

3. Moves the index’s changes to the working tree

4. Clears the index

5. Makes the working copy look like the index

Presentation tackmobile.com

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 different branch!

Presentation tackmobile.com

Git FlowA Popular Branching Model

Presentation tackmobile.com

Git Flow

• Conventions to follow

• Tools to help you follow conventions

• http://nvie.com/posts/a-successful-git-branching-model/

Presentation tackmobile.com

Git Flow Conventions: Master Branch

• The master branch is what is publicly available now

• You don’t commit directly to master

Presentation tackmobile.com

Git Flow Conventions: Develop Branch

• A branch called “develop” is what will become the next version

• Day to day work happens on develop

• “Integration branch”

Presentation tackmobile.com

Git Flow Conventions: Feature Branches

• Long running development go on feature branches: “feature/foo”

• Long running: “more than one commit”

• Can be pushed to the server and shared

• Branch from develop

Presentation tackmobile.com

Git Flow Conventions: Hotfixes

• OMG Problems in Production, create a hotfix: “hotfix/foo”

• Branch from master (not develop)

Presentation tackmobile.com

Git Flow Conventions: Releases

• When a release is almost ready on develop, create a release branch: “release/2.0.4”

• Branch from develop

• Develop continues on for the next release

• Small changes to release go on release branch

Presentation tackmobile.com

Branch Lifecycle

• Features

• Start from develop

• Finished and merged to develop

• Releases

• Start from develop

• Finished and merged to master and develop

Presentation tackmobile.com

Git Flow in Action: Features

feature/somefeature

master

develop

Presentation tackmobile.com

Git Flow in Action: Releases

release/v1.0

master

develop

Presentation tackmobile.com

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.

Thank you!Git for the Android Developer • Tony Hillerson

• Questions?

• We’re Hiring! careers@tackmobile.com

• Excellent Team

• Awesome Projects

• Great Office