working with git
DESCRIPTION
Talk given at AnDevCon Spring 2014 in Boston.TRANSCRIPT
Working with GitFor the Android Developer
slideshare.net/thillerson
Tony Hillerson, AnDevCon Spring 2014
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)
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
Presentation tackmobile.com
The Git DatabaseGet Acquainted
Presentation tackmobile.com
.git
• Each git repository has a database in the .git directory
• HEAD, FETCH_HEAD, ORIG_HEAD
• objects
• refs
• etc… hooks
Presentation tackmobile.com
Poking Around
• git show <commit>
• Shows blob contents, tree info, commit/ref logs
• git ls-tree
• git rev-list
Presentation tackmobile.com
ConfigurationGet Comfortable
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
Presentation tackmobile.com
Configure Colors
• git config color.diff auto
• [color] diff = autostatus = autobranch = autoui = true
Presentation tackmobile.com
Configure Aliases
• git config alias.co checkout
• [alias] co = checkoutcp = cherry-pickunstage = reset HEADlop = log -p
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
Presentation tackmobile.com
You Have QuestionsGet Answers
Presentation tackmobile.com
When Was The First Commit?
• Assuming “master” is the conventional “master”:
• git log master --oneline | tail -n1
Presentation tackmobile.com
When Did This Branch Branch?
• git merge-base <branch> <parent>
• probably…
• git help merge-base
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
Presentation tackmobile.com
Do I Have Unpushed Changes?
• The opposite!
• git log origin/develop..develop
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
Presentation tackmobile.com
Setting Upstream
• In .gitconfig:[branch]autosetupmerge = always
• git branch -u <remote> [branch]
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
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
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
Presentation tackmobile.com
Where Did My Commit Go?
• Have you lost a commit you know you made?
• git reflog!
Presentation tackmobile.com
Working With Multiple RepositoriesGet Integrated
Presentation tackmobile.com
Local Repos
• git clone <path> <new path>
• git push origin <branch>
• git pull origin <branch>
WAT
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?
Presentation tackmobile.com
Multiple Remotes
• git remote add <path>
• git push otherremote branch
• git pull otherremote branch
• Why? other upstream repo, maybe a fork
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)
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
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
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
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
Presentation tackmobile.com
Submodule Examples
• Utility Project
Presentation tackmobile.com
Submodule tips
• Avoid them.
• If you need to use them, make sure your team understands them.
Presentation tackmobile.com
Subtrees
• Add repo contents directly to tree
• http://blogs.atlassian.com/2013/05/alternatives-to-git-submodule-git-subtree/
Presentation tackmobile.com
Git FlowGetting Apps Shipped
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
• 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
Thank you!Working with Git for the Android Developer • Tony Hillerson
• Questions?
• We’re Hiring! [email protected]
• Excellent Team
• Awesome Projects
• Great Office