Git
For the Android Developer
Tony Hillerson, AnDevCon Fall 2013
#AnDevCon @tackmobile @thillerson
About Me
•

@thillerson, +thillerson

•

Developer at Tack Mobile

(tackmobile.com), @tackmobile

•

Android, iOS, and Mob...
Diving Right In
Learning by Doing

Image © Dennis Barnes

http://www.flickr.com/photos/dennisbarnes/2817664242
git
init

or

git
clone

changes

git
add

git commit

86650c185

changes

git
add

git commit

6facfd9f3

changes

git
ad...
.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

tackm...
Remotes
•

remote add

•

clone

•

fetch

•

pull

•

push

Presentation

tackmobile.com
Tagging
git tag -a -m"Tagging v1.0" v1.0 c5083fa
master

fb4f5d9

c5083fa

3f43fa3

•

Both “-v1.0” and c5083fa will point...
Recap of Simple Commands
•

git init - Creates an empty Git repository

•

git add - Adds a file to the stage (“stages a fil...
Why Source Control?
•

For the solo developer?
•

Protection against mistakes

•

Freedom
•
•

•

... to refactor
... to e...
Preliminaries
Getting Git and Getting Set Up
What’s a Git?
A completely ignorant, childish person with no
manners. - http://urbandictionary.com

Linus Torvalds
http://...
What’s a Git?
Git is a free & open source, distributed version
control system designed to handle everything
from small to ...
Getting Set Up on Mac
•

Homebrew

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

•

brew install git

MacPorts

http://www.macports....
Getting Set Up on Windows
•

msysgit

http://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/tor...
IDE Integration
•

Android Studio

You’re All Set

•

Eclipse

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

Presentation

tackmobile.com
Reference
•

Git

http://git-scm.com/

•

ProGit

http://progit.org/book/

•

Insider Guide to Github

http://www.pragprog...
The Command Line
A Short Sermon

Presentation

tackmobile.com
The Guts of Git
The Little Bits that Make Git Different
What’s With all the Characters?
•

SHA1 Hash e.g.

86650c185eda50c9f9d58e2fbdf8b7113e5dee54

•

Uniquely identifies a commi...
SHA-1 Hash Keys

“

... to have a probability of a SHA1-hash collision
rise to 1/2, you need about 10^24 objects ...
- Sco...
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 ...
Trees
•

Trees give structure to blobs

•

Trees are also stored in .git/objects

•

Identified by SHA-1

•

View a tree wi...
Commits
•

Identified by a SHA-1

•

Points to one tree

•

Has a required message

•

May have one (or more) parent commit...
Refs
•

Point to commits

•

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

•

HEAD - the latest commit on the cu...
Blobs Are Content

b84ed8ed

579a3b1

Presentation

e8d5cf6

tackmobile.com
Trees Give Structure
9899d2c

com/yourcompany/androidapp

b84ed8ed

579a3b1

FooFragment.java

e8d5cf6

BarView.java

3ffb...
Commits Point to Trees
d414c3e

9899d2c

com/yourcompany/androidapp

“Fixed bug # 42”
b84ed8ed

579a3b1

FooFragment.java
...
Commits Have Parents
090c953

4

“Updated the main activity”

d414c3e

3

“Fixed bug #42”

4493671

2

“Typed awesome code...
Refs Point to Commits
HEAD

090c953

4

“Updated the main activity”

d414c3e

3

“Fixed bug #42”

4493671

2

“Typed aweso...
And That’s All You Need To
Know About Git

Presentation

tackmobile.com
And That’s All You Need To
Know About Git
(Mostly)

Presentation

tackmobile.com
Day to Day Git
“What would you say you *do* here?”
Semantic Commits
Make 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 chan...
Interactive Add - SourceTree

Presentation

tackmobile.com
Branching
Like 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

tackmo...
How To Think About Branching
•

Mainline
•

What do you want “master” to mean?

•

Topic Branches

•

Branching examples

...
Topic Branches
•

Branching is about controlling feature sets

•

Make a new branch for a story

•

Make a new branch for ...
Team Branching Strategies
•

What do you want “master” to mean?

•

Keep master deployable?
•

•

one strategy for web sof...
Branching
git checkout -b add_login_activity
master

fb4f5d9

c5083fa
add_login_activity

9aa8827

Presentation

fe594ce

...
Branching: Merging
git checkout master
git merge add_login_activity
master

fb4f5d9

c5083fa

9aa8827

3f43fa3

fe594ce

c...
Branching: Rebasing
master
fb4f5d9

c5083fa

3f43fa3

add_login_activity

before

9aa8827

fe594ce

ccb6f5e

master
fb4f5d...
Branching: Rebasing
•

Better than merging in some ways...

•

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

Can o...
Git Pull --rebase
•

Also available: `git pull --rebase`
•

Helpful for avoiding merge commits

•

May cause problems if g...
Git Stash
Like 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 ...
Cherry Pick
I’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
Cherry-pick
git cherry-pick fe594ce
A new commit
with the changes
from fe594ce

master

fb4f5d9

c5083fa

3f43fa3

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

tack...
git commit --amend
•

Oops! I misspelled something in the commit
message

•

Oops! I did `git commit -a` and forgot to `gi...
Interactive Rebase - Fixing History
•

git rebase -i [commit]

•

A list of all commits in the current order

•

Reorder

...
git reset
•

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

•

`git reset HEAD` = same as above - acts on all
tr...
git reset --soft [commit]
1. Moves HEAD to [commit]
2. Puts the “popped” contents on the index

Presentation

tackmobile.c...
git reset [commit] (“mixed” - default)
1. Moves HEAD to [commit]
2. Puts the “popped” contents on the index
3. Moves the i...
git reset --hard [commit] (DESTRUCTIVE!!!)
1. Moves HEAD to [commit]
2. Puts the “popped” contents on the index
3. Moves t...
git reset use cases
•

Back that last commit up (git reset HEAD^)

•

Don’t forget `commit --amend`

•

Oops, didn’t mean ...
Git Flow
A Popular Branching Model

Presentation

tackmobile.com
Git Flow
•

Conventions to follow

•

Tools to help you follow conventions

•

http://nvie.com/posts/a-successful-gitbranc...
Git Flow Conventions: Master Branch
•

The master branch is what is publicly available
now

•

You don’t commit directly t...
Git Flow Conventions: Develop Branch
•

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

•

Day to day work...
Git Flow Conventions: Feature Branches
•

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

•

Long running:...
Git Flow Conventions: Hotfixes
•

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

•

Branch from master (not devel...
Git Flow Conventions: Releases
•

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

•

...
Branch Lifecycle
•

Features
•
•

•

Start from develop
Finished and merged to develop

Releases
•

Start from develop

•
...
Git Flow in Action: Features

master

develop

feature/somefeature

Presentation

tackmobile.com
Git Flow in Action: Releases

master
release/v1.0

develop

Presentation

tackmobile.com
The Take Home
•

SCM Is Important

•

No matter what kind of developer you are

•

Git is fundamentally different from the...
Thank you!
Git for the Android Developer • Tony Hillerson

•

Questions?

•

We’re Hiring! careers@tackmobile.com
•

Excel...
Upcoming SlideShare
Loading in...5
×

Git for Android Developers

10,122

Published on

Slides for a presentation I gave at AnDevCon

Published in: Technology

Git for Android Developers

  1. 1. Git For the Android Developer Tony Hillerson, AnDevCon Fall 2013 #AnDevCon @tackmobile @thillerson
  2. 2. About Me • @thillerson, +thillerson • Developer at Tack Mobile
 (tackmobile.com), @tackmobile • Android, iOS, and Mobile Web • Rails, Node, maybe Elixir (one day) Presentation tackmobile.com
  3. 3. Diving Right In Learning by Doing Image © Dennis Barnes
 http://www.flickr.com/photos/dennisbarnes/2817664242
  4. 4. git init or git clone changes git add git commit 86650c185 changes git add git commit 6facfd9f3 changes git add git commit b02ef5bf1 ... ∞ Presentation tackmobile.com
  5. 5. .gitignore • Can be nested deeply • https://github.com/github/gitignore Presentation tackmobile.com
  6. 6. Git Log - The Project’s History • What got committed? • Commit messages • Content • When? Who? Presentation tackmobile.com
  7. 7. Remotes • remote add • clone • fetch • pull • push Presentation tackmobile.com
  8. 8. Tagging git tag -a -m"Tagging v1.0" v1.0 c5083fa master fb4f5d9 c5083fa 3f43fa3 • Both “-v1.0” and c5083fa will point to c5083fa • Push this tag with `git push --tags` • Can be cryptologically signed Presentation tackmobile.com
  9. 9. 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
  10. 10. 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 Presentation tackmobile.com
  11. 11. Preliminaries Getting Git and Getting Set Up
  12. 12. 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
  13. 13. 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
  14. 14. Getting Set Up on Mac • Homebrew
 http://mxcl.github.com/homebrew/ • • brew install git MacPorts
 http://www.macports.org/ Presentation tackmobile.com
  15. 15. Getting Set Up on Windows • msysgit
 http://code.google.com/p/msysgit/ Presentation tackmobile.com
  16. 16. Getting Set Up on Linux • apt, etc - you probably know the drill Presentation tackmobile.com
  17. 17. Gooies! • SourceTree (Mac and Windows)
 http://sourcetreeapp.com/ • TortoiseGit (Windows)
 http://code.google.com/p/tortoisegit/ Presentation tackmobile.com
  18. 18. IDE Integration • Android Studio
 You’re All Set • Eclipse
 http://www.eclipse.org/egit/ Presentation tackmobile.com
  19. 19. Reference • Git
 http://git-scm.com/ • ProGit
 http://progit.org/book/ • Insider Guide to Github
 http://www.pragprog.com/screencasts/vscgithub/insider-guide-to-github Presentation tackmobile.com
  20. 20. The Command Line A Short Sermon Presentation tackmobile.com
  21. 21. The Guts of Git The Little Bits that Make Git Different
  22. 22. 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
  23. 23. 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
  24. 24. In Git There Are Only... • Blobs • Trees • Commits Presentation tackmobile.com
  25. 25. 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
  26. 26. 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
  27. 27. 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
  28. 28. Refs • Point to commits • .git/refs/heads - the latest commits in local branches • HEAD - the latest commit on the current branch Presentation tackmobile.com
  29. 29. Blobs Are Content b84ed8ed 579a3b1 Presentation e8d5cf6 tackmobile.com
  30. 30. Trees Give Structure 9899d2c com/yourcompany/androidapp b84ed8ed 579a3b1 FooFragment.java e8d5cf6 BarView.java 3ffb35b Presentation MainActivity.java /anotherpackage trees can point to other trees tackmobile.com
  31. 31. Commits Point to Trees d414c3e 9899d2c com/yourcompany/androidapp “Fixed bug # 42” b84ed8ed 579a3b1 FooFragment.java e8d5cf6 BarView.java 3ffb35b Presentation MainActivity.java /anotherpackage tackmobile.com
  32. 32. Commits Have Parents 090c953 4 “Updated the main activity” d414c3e 3 “Fixed bug #42” 4493671 2 “Typed awesome code” c1d1f60 1 “Initial commit” Presentation tackmobile.com
  33. 33. Refs Point to Commits HEAD 090c953 4 “Updated the main activity” d414c3e 3 “Fixed bug #42” 4493671 2 “Typed awesome code” c1d1f60 1 “Initial commit” Presentation tackmobile.com
  34. 34. And That’s All You Need To Know About Git Presentation tackmobile.com
  35. 35. And That’s All You Need To Know About Git (Mostly) Presentation tackmobile.com
  36. 36. Day to Day Git “What would you say you *do* here?”
  37. 37. Semantic Commits Make Commits Mean Something Presentation tackmobile.com
  38. 38. Semantic Commits • Git’s not just a big truck • Commits should each mean something Presentation tackmobile.com
  39. 39. 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
  40. 40. Interactive Add - SourceTree Presentation tackmobile.com
  41. 41. Branching Like Hitting Save Before You Fight the Level Boss Presentation tackmobile.com
  42. 42. Branching • New branch: git checkout -b <name> • A branch is a named ref • merging • rebasing Presentation tackmobile.com
  43. 43. How To Think About Branching • Mainline • What do you want “master” to mean? • Topic Branches • Branching examples Presentation tackmobile.com
  44. 44. 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
  45. 45. 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
  46. 46. Branching git checkout -b add_login_activity master fb4f5d9 c5083fa add_login_activity 9aa8827 Presentation fe594ce ccb6f5e tackmobile.com
  47. 47. Branching: Merging git checkout master git merge add_login_activity master fb4f5d9 c5083fa 9aa8827 3f43fa3 fe594ce ccb6f5e add_login_activity 9aa8827 Presentation fe594ce ccb6f5e tackmobile.com
  48. 48. Branching: Rebasing master fb4f5d9 c5083fa 3f43fa3 add_login_activity before 9aa8827 fe594ce ccb6f5e master fb4f5d9 c5083fa 3f43fa3 add_login_activity after `git rebase master` Presentation 9aa8827 fe594ce ccb6f5e tackmobile.com
  49. 49. 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
  50. 50. Git Pull --rebase • Also available: `git pull --rebase` • Helpful for avoiding merge commits • May cause problems if git can’t automatically merge • Presentation `git reset HEAD` and start over with normal `git pull` tackmobile.com
  51. 51. Git Stash Like a Little Repo In Your Repo Presentation tackmobile.com
  52. 52. git stash • remember: git help stash • Stash away changes in a safe place • A Workflow:
 Stash -> Fix & Commit -> Pop Stash Presentation tackmobile.com
  53. 53. Cherry Pick I’ll Take One Of Those... And One Of Those... Presentation tackmobile.com
  54. 54. Cherry Pick • When you need a commit’s changes • But not its history Presentation tackmobile.com
  55. 55. Cherry-pick git cherry-pick fe594ce A new commit with the changes from fe594ce master fb4f5d9 c5083fa 3f43fa3 add_login_activity 9aa8827 Presentation fe594ce ccb6f5e tackmobile.com
  56. 56. Whoops! Lots Of Ways To Fix It Presentation tackmobile.com
  57. 57. git checkout • `git checkout [filename]`
 =
 remove unstaged changes Presentation tackmobile.com
  58. 58. git revert • Commits the reverse of a commit • The previous commit is still there • != svn revert Presentation tackmobile.com
  59. 59. 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
  60. 60. 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
  61. 61. 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
  62. 62. git reset --soft [commit] 1. Moves HEAD to [commit] 2. Puts the “popped” contents on the index Presentation tackmobile.com
  63. 63. 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
  64. 64. 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
  65. 65. 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
  66. 66. Git Flow A Popular Branching Model Presentation tackmobile.com
  67. 67. Git Flow • Conventions to follow • Tools to help you follow conventions • http://nvie.com/posts/a-successful-gitbranching-model/ Presentation tackmobile.com
  68. 68. Git Flow Conventions: Master Branch • The master branch is what is publicly available now • You don’t commit directly to master Presentation tackmobile.com
  69. 69. 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
  70. 70. 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
  71. 71. Git Flow Conventions: Hotfixes • OMG Problems in Production, create a hotfix: “hotfix/foo” • Branch from master (not develop) Presentation tackmobile.com
  72. 72. 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
  73. 73. 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
  74. 74. Git Flow in Action: Features master develop feature/somefeature Presentation tackmobile.com
  75. 75. Git Flow in Action: Releases master release/v1.0 develop Presentation tackmobile.com
  76. 76. 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. Presentation tackmobile.com
  77. 77. Thank you! Git for the Android Developer • Tony Hillerson • Questions? • We’re Hiring! careers@tackmobile.com • Excellent Team • Awesome Projects • Great Office
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×