38
© 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

JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

Embed Size (px)

DESCRIPTION

http://guide13.jazoon.com/#/submissions/157

Citation preview

Page 1: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 2: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 3: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners 3

No Central Server – Distributed VCS

Performance and Efficiency

Robustness

10/25/2013

Git Concepts

Page 4: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners

Git Internals

Page 5: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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 …

Page 6: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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, …)

Page 7: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

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

Page 8: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners 8

Git Storage – Object Model (2)

├── lib

├ ├── inc

├ ├ └── tricks.rb

├ └── mylib.rc

└── README

Page 9: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 10: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners

Rewriting History

Page 11: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 12: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 13: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 14: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners 14

$ git filter-branch

Removing a file from every commit

Changing commit metadata globally

10/25/2013

Filter branch

Page 15: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners

Recovering from Mistakes

Page 16: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 17: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 18: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners 18

$ git revert HEAD|hash|parent

Revert

git revert

Upgrading Library

Revert ‘Upgrading Library’

Page 19: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 20: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 21: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 22: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners

Sharing without network

Page 23: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners 23

$ git archive --format zip --output "repo.zip" master

Archives

git archive

Page 24: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 25: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners

Repeat Yourself Repeat Yourself Repeat Yourself

Page 26: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 27: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners

Other tricks

Page 28: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 29: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

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

??

Page 30: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 31: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 32: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners

Hooks

Page 33: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 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

Page 34: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners

Workflows

Page 35: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners 35

Git Flow

$ git flow init git flow

master hotfix release develop Feature branches

Page 36: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners

Git in the Enterprise

Page 37: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners 37

Get IT right

Thank you!

Page 38: JAZOON'13 - Bartosz Majsak - Git Workshop - Kung Fu

© 2013 Cambridge Technology Partners 38

Icons provided by Icons8: http://icons8.com/

10/25/2013

Credits