working with git

46
Working with Git For the Android Developer slideshare.net/thillerson Tony Hillerson, AnDevCon Spring 2014

Upload: tony-hillerson

Post on 19-May-2015

333 views

Category:

Software


0 download

DESCRIPTION

Talk given at AnDevCon Spring 2014 in Boston.

TRANSCRIPT

Page 1: Working with Git

Working with GitFor the Android Developer

slideshare.net/thillerson

Tony Hillerson, AnDevCon Spring 2014

Page 2: Working with Git

Presentation tackmobile.comPresentation tackmobile.com

About Me

• @thillerson, +thillerson

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

• Android, iOS, and Mobile Web

• Rails, Node, maybe Elixir (one day)

Page 3: Working with Git

Presentation tackmobile.comPresentation tackmobile.com

What We’ll Cover Today

• The Git Database

• Configuration

• Working with Multiple Repositories

• ssh, submodules, subtrees

• Advanced Querying

• Git Flow for Delivering Releases

Page 4: Working with Git

Presentation tackmobile.com

The Git DatabaseGet Acquainted

Page 5: Working with Git

Presentation tackmobile.com

.git

• Each git repository has a database in the .git directory

• HEAD, FETCH_HEAD, ORIG_HEAD

• objects

• refs

• etc… hooks

Page 6: Working with Git

Presentation tackmobile.com

Poking Around

• git show <commit>

• Shows blob contents, tree info, commit/ref logs

• git ls-tree

• git rev-list

Page 7: Working with Git

Presentation tackmobile.com

ConfigurationGet Comfortable

Page 8: Working with Git

Presentation tackmobile.com

gitconfig locations

Global /etc/gitconfig

User ~/.gitconfig

Local Project(not shared)

.git/config

git-scm.com/book/en/Customizing-Git-Git-Configuration

Page 9: Working with Git

Presentation tackmobile.com

Configure Colors

• git config color.diff auto

• [color] diff = autostatus = autobranch = autoui = true

Page 10: Working with Git

Presentation tackmobile.com

Configure Aliases

• git config alias.co checkout

• [alias] co = checkoutcp = cherry-pickunstage = reset HEADlop = log -p

Page 11: Working with Git

Presentation tackmobile.com

Terse Log

• [alias] lol = log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --graph —decorate

• - Scott Chacon

Page 12: Working with Git

Presentation tackmobile.com

You Have QuestionsGet Answers

Page 13: Working with Git

Presentation tackmobile.com

When Was The First Commit?

• Assuming “master” is the conventional “master”:

• git log master --oneline | tail -n1

Page 14: Working with Git

Presentation tackmobile.com

When Did This Branch Branch?

• git merge-base <branch> <parent>

• probably…

• git help merge-base

Page 15: Working with Git

Presentation tackmobile.com

Do I Have The Latest Changes?

• git fetch - git must have the latest changes locally to tell you

• git log develop..origin/develop

• If no commits - yes, you have the latest

• Tools like SourceTree will often do automatic fetches

Page 16: Working with Git

Presentation tackmobile.com

Do I Have Unpushed Changes?

• The opposite!

• git log origin/develop..develop

Page 17: Working with Git

Presentation tackmobile.com

What’s Upstream?

• Upstream is the repository/branch from which changes come

• ➜ git status # On branch develop# Your branch is behind 'origin/develop' by 2 commits, and can be fast-forwarded.# (use "git pull" to update your local branch) #nothing to commit, working directory clean

Page 18: Working with Git

Presentation tackmobile.com

Setting Upstream

• In .gitconfig:[branch]autosetupmerge = always

• git branch -u <remote> [branch]

Page 19: Working with Git

Presentation tackmobile.com

Checking All Branches

• ➜ git branch -lvv * develop 24c4398 [origin/develop: behind 2] warning fixed. master 3cc12bb [origin/master: behind 3] Giving username an email keyboard for what seems to be the common use case.

• Add -a to include remotes

Page 20: Working with Git

Presentation tackmobile.com

Is This Branch Merged?

• Which branches contain a given commit?

• git branch --contains <branch/commit>

• ➜ git branch --contains develop * develop

• ➜ git branch --contains master* developmaster

Page 21: Working with Git

Presentation tackmobile.com

Where Did This Bug Come From?

• git bisect!

• Known Good Commit <|> Latest Buggy Commit

• build and run

• mark a commit good or bad

• git walks forward or backward

• repeat

Page 22: Working with Git

Presentation tackmobile.com

Where Did My Commit Go?

• Have you lost a commit you know you made?

• git reflog!

Page 23: Working with Git

Presentation tackmobile.com

Working With Multiple RepositoriesGet Integrated

Page 24: Working with Git

Presentation tackmobile.com

Local Repos

• git clone <path> <new path>

• git push origin <branch>

• git pull origin <branch>

WAT

Page 25: Working with Git

Presentation tackmobile.com

ssh

• git clone ssh://user@host/path/.git

• Push to origin

• Pull from origin

• Use when just starting out a private project?

Page 26: Working with Git

Presentation tackmobile.com

Multiple Remotes

• git remote add <path>

• git push otherremote branch

• git pull otherremote branch

• Why? other upstream repo, maybe a fork

Page 27: Working with Git

Presentation tackmobile.com

Submodules

• A git repository tracked as part of the tree of another repository

• Git (kind of) manages it

• cd in -> get another git repo

• Sort of a broken SVN external (the only thing I like better about SVN than git)

Page 28: Working with Git

Presentation tackmobile.com

Submodules: What are they good for?

• Dependencies

• That are under frequent development

• That are not distributed as a jar or through maven

Page 29: Working with Git

Presentation tackmobile.com

Submodules: Why do they suck?

• You have to be explicit about them; git won’t manage them for you

• Devs on your team need to be educated

• Have to jump through hoops to connect the repo back to its origin

• Hard to share local changes

Page 30: Working with Git

Presentation tackmobile.com

Working with Submodules

• git submodule add <repo> <local name>

• .gitmodules

• Keeps submodule at a explicit commit

• ignore = dirty

• git submodule deinit, git rm

Page 31: Working with Git

Presentation tackmobile.com

Submodules: Workflow

• If you need to move the submodule’s commit

• cd to submodule

• move to the new commit

• cd to parent

• git add and commit

• When you pull, add:git submodule update —install —recursive

Page 32: Working with Git

Presentation tackmobile.com

Submodule Examples

• Utility Project

Page 33: Working with Git

Presentation tackmobile.com

Submodule tips

• Avoid them.

• If you need to use them, make sure your team understands them.

Page 34: Working with Git

Presentation tackmobile.com

Subtrees

• Add repo contents directly to tree

• http://blogs.atlassian.com/2013/05/alternatives-to-git-submodule-git-subtree/

Page 35: Working with Git

Presentation tackmobile.com

Git FlowGetting Apps Shipped

Page 36: Working with Git

Presentation tackmobile.com

Git Flow

• Conventions to follow

• Tools to help you follow conventions

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

Page 37: Working with Git

Presentation tackmobile.com

Git Flow Conventions: Master Branch

• The master branch is what is publicly available now

• You don’t commit directly to master

Page 38: Working with Git

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”

Page 39: Working with Git

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

Page 40: Working with Git

Presentation tackmobile.com

Git Flow Conventions: Hotfixes

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

• Branch from master (not develop)

Page 41: Working with Git

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

Page 42: Working with Git

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

Page 43: Working with Git

Presentation tackmobile.com

Git Flow in Action: Features

feature/somefeature

master

develop

Page 44: Working with Git

Presentation tackmobile.com

Git Flow in Action: Releases

release/v1.0

master

develop

Page 45: Working with Git

Presentation tackmobile.com

The Take Home

• You know your way around .git

• You can customize and configure git

• You understand submodules and where they work (and don’t work)

• You know how git flow can help when releasing apps with a team

Page 46: Working with Git

Thank you!Working with Git for the Android Developer • Tony Hillerson

• Questions?

• We’re Hiring! [email protected]

• Excellent Team

• Awesome Projects

• Great Office