Date post: | 10-May-2015 |
Category: |
Technology |
Upload: | jazoon13 |
View: | 327 times |
Download: | 2 times |
© 2013 Cambridge Technology Partners 1 Copyright © 2013 Cambridge Technology Partners All Rights Reserved. Cambridge, its logo, and Get IT Right are trademarks of Cambridge Technology Partners.
Get IT right
Git Kung Fu
Bartosz Majsak (@majson), Thomas Hug (@gizmoo360) http://ctp.com http://github.com/ctpconsulting
© 2013 Cambridge Technology Partners 2
Bartosz Majsak Java Developer by day
Open source junkie by night (Arquillian core team member)
Conference speaker by passion (Devoxx, Jazoon ...)
10/25/2013
About Us
Thomas Hug With Cambridge Technology Partners since 2002
Java Developer, TTL, Solution Architect
Apache Committer, OSS contributor and aficionado
© 2013 Cambridge Technology Partners 3
No Central Server – Distributed VCS
Performance and Efficiency
Robustness
10/25/2013
Git Concepts
© 2013 Cambridge Technology Partners
Git Internals
© 2013 Cambridge Technology Partners 5
Plumbing and Porcelain Composition of low-level commands into high-level ones
Unix design principles
Local as much as possible
10/25/2013
Git Architecture
git-diff-index git-update-server-info git-check-ref-format …
git-add git-commit …
© 2013 Cambridge Technology Partners 6
Delta storage vs. Directed Acyclic Graph (DAG)
10/25/2013
Git Storage
C1 C2 C3 C4 C5
File A
File B
File C
D1 D2
D1 D2
D1 D2 D3
A
B
C
A1
B
C1
A1
B
C2
A2
B1
C2
A2
B2
C3
Delta Storage (SVN, Mercurial,…)
DAG Storage (Git, cp -r, …)
© 2013 Cambridge Technology Partners 7
Git tracks content, not files
Content identified by 40 character SHA1 hash Modified content easily identifiable
Immutable in the object database
Objects: Blob, Tree, Commit, Tag
References: HEAD, Tags, Remotes Mutable, pointers to commits
Git Storage – Object Model (1)
Empty directories are not considered as content. Add an empty .gitignore if you need a folder tracked.
© 2013 Cambridge Technology Partners 8
Git Storage – Object Model (2)
├── lib
├ ├── inc
├ ├ └── tricks.rb
├ └── mylib.rc
└── README
© 2013 Cambridge Technology Partners 9
The repository .git directory
$ cd .git
$ tree -L 1
├── branches
├── config
├── description
├── HEAD
├── hooks
├── info
├── objects
└── refs
10/25/2013
Git Storage – Local Repository
# Pointers to branches
# Repository local configuration
# Repository description
# Pointer to HEAD in current branch
# Pre- and post action hooks
# Additional information about the repository
# Object database
# Pointers to branches
© 2013 Cambridge Technology Partners
Rewriting History
© 2013 Cambridge Technology Partners 11
$ git checkout master
$ git rebase mybranch
Rebasing
Rewriting history: Interactive rebase last four commits $ git rebase --i HEAD~4
git rebase
© 2013 Cambridge Technology Partners 12
Objectives: Learn how interactive rebasing works.
Experiment with interactive rebase on selected branch Reword commit messages
Combine commits into one
$ git rebase –i [commits range]
10/25/2013
All your base are belong to us
© 2013 Cambridge Technology Partners 13
$ git cherry-pick [-x]
Cherry-pick „replays” arbitrary commits onto
your current branch.
$ git cherry –v <other_branch>
Lets you check if given commit from other branch
has been already applied on the current branch
10/25/2013
Cherry-pick
Featu
re
© 2013 Cambridge Technology Partners 14
$ git filter-branch
Removing a file from every commit
Changing commit metadata globally
10/25/2013
Filter branch
© 2013 Cambridge Technology Partners
Recovering from Mistakes
© 2013 Cambridge Technology Partners 16
Is there a way to fix poor commit messages?
$ git commit --amend
$ git rebase --i HEAD~X
10/25/2013
Typos
© 2013 Cambridge Technology Partners 17
For not yet pushed commits:
$ git commit --amend
Unstage a file:
$ git reset HEAD file.txt
Discard local changes:
$ git checkout -- file.txt
Fixing Commits and Staging Area
git reset
Fully revert to a previous commit: $ git reset --hard HEAD
© 2013 Cambridge Technology Partners 18
$ git revert HEAD|hash|parent
Revert
git revert
Upgrading Library
Revert ‘Upgrading Library’
© 2013 Cambridge Technology Partners 19
Disaster recovery.
What if...
$ git reset --hard HEAD^
$ git reflog
$ git reset --hard HEAD@{X}
10/25/2013
One bridge too far
© 2013 Cambridge Technology Partners 20
$ git blame FILE
$ git bisect start
$ git bisect bad
$ git bisect good <HASH>
Who broke the build?!
git blame
git bisect
© 2013 Cambridge Technology Partners 21
$ git log –S'search_term' [-p] <resource>
$ git log –G'regex'
--name-status
--pickaxe-all
Who broke the build?! - Pickaxe
git log –S|G
© 2013 Cambridge Technology Partners
Sharing without network
© 2013 Cambridge Technology Partners 23
$ git archive --format zip --output "repo.zip" master
Archives
git archive
© 2013 Cambridge Technology Partners 24
$ git bundle create \ ../jazoon.bundle master HEAD
$ git bundle create <filename> <refs ...>
$ git bundle create ../jazoon.bundle \
--since="one week ago" master
$ git bundle verify /tmp/jazoon.bundle
$ git pull /tmp/ejmr.bundle master:master
Bundles
git bundle
You can also add a bundle as a remote: $ git remote add bundle /path/to/bundle.bundle
© 2013 Cambridge Technology Partners
Repeat Yourself Repeat Yourself Repeat Yourself
© 2013 Cambridge Technology Partners 26
$ git config rerere.enabled true
… # create a merge conflict
$ git rerere status
$ git rerere diff
… # resolve conflict
$ git rerere diff
… # commit, reset hard HEAD^1, redo merge
Reuse Recorded Resolution (ReReRe)
10/25/2013
git rerere
Evict old recorded resolutions from repository: $ git rerere gc
© 2013 Cambridge Technology Partners
Other tricks
© 2013 Cambridge Technology Partners 28
$ git init
$ git config core.sparsecheckout true
$ echo path/to/checkout \ >> .git/info/sparse-checkout
$ git remote add -f origin ...
$ git pull origin ...
Sparse Checkouts
10/25/2013
You can also push to sparse checkout repositories
© 2013 Cambridge Technology Partners 29
$ git checkout --orphan [branch]
$ git rm -rf *
Use for:
Documentation (combine with hooks or CI server!)
Resources
Orphan Branches
10/25/2013
Have a look at the GitHub gh-pages for some ideas what orphan branches can do.
??
© 2013 Cambridge Technology Partners 30
Annotates existing commits.
$ git notes add HEAD
$ git notes add –m’Fixes everything’ HEAD
$ git notes --ref=jazoon edit master~1
$ git push origin refs/notes/jazoon
Notes
10/25/2013
© 2013 Cambridge Technology Partners 31
$ git diff --word-diff --color-words
$ git config --global help.autocorrect 1
$ git rebase -i --autosquash HEAD~3 Commit message should contain squash! or fixup! as message prefix and
title or hash of target commit
Other goodies
10/25/2013
© 2013 Cambridge Technology Partners
Hooks
© 2013 Cambridge Technology Partners 33
$ cd .git/hooks
Client-side pre-commit
prepare-commit-msg
commit-msg
post-commit
Server-side pre-receive
post-receive
update
Git hooks
10/25/2013
© 2013 Cambridge Technology Partners
Workflows
© 2013 Cambridge Technology Partners 35
Git Flow
$ git flow init git flow
master hotfix release develop Feature branches
© 2013 Cambridge Technology Partners
Git in the Enterprise
© 2013 Cambridge Technology Partners 37
Get IT right
Thank you!
© 2013 Cambridge Technology Partners 38
Icons provided by Icons8: http://icons8.com/
10/25/2013
Credits