Top Banner
git internals Juan de Bravo @juandebravo git internals DevCon I 2012/11/27
122
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dev conf git_lecture

git internals

Juan de Bravo

@juandebravo

git internals DevCon I 2012/11/27

Page 2: Dev conf git_lecture

git internals DevCon I 2012/11/27

Git. Definition Page 2

free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency

Page 3: Dev conf git_lecture

git internals DevCon I 2012/11/27

Git. Distributed repository Page 3

Page 4: Dev conf git_lecture

git "usual commands"

git workflow

this talk is not about…

github/pdihub

git internals DevCon I 2012/11/27

Disclaimer Page 4

Page 5: Dev conf git_lecture

this talk is about…

git internals DevCon I 2012/11/27

Disclaimer 2 Page 5

Page 6: Dev conf git_lecture

1

2

git internals

git advanced topics

3 git tips & tricks

git internals DevCon I 2012/11/27

Page 7: Dev conf git_lecture

1

2

git internals

git advanced topics

3 git tips & tricks

git internals DevCon I 2012/11/27

Page 8: Dev conf git_lecture

git internals DevCon I 2012/11/27

1 git internals

git objects1.1

1.2 git internal structure

Page 9: Dev conf git_lecture

git internals DevCon I 2012/11/27

1 git internals

git objects1.1

1.2 git internal structure

Page 10: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1 git objects. types Page 10

- blobs : content- trees : content structure- commits: history snapshots- tags : named commits/objects

git defines 4 object types

Page 11: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1 git objects. storage Page 11

These objects are stored individually inside the .git/objects folder

λ find .git/objects | wc -l 628

λ find .git/objects | wc -l 1696

λ find .git/objects | wc -l 6543

Page 12: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1 git objects. format and filename Page 12

git object filenameSHA1 of the previous format applied to the object

- object type: blob|tree|commit|tag- content size- null byte- content

git object format

Page 13: Dev conf git_lecture

git internals DevCon I 2012/11/27

1 git internals

git objects1.1

1.1.1 git blob

Page 14: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.1 git objects. blobs Page 14

Git stores every single file content using a blob object with the git format (object type, etc)

<Blob SHA1>

Page 15: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.1 git objects. blobs 3 Page 15

git hash-object

Page 16: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.1 git objects. blobs 2 Page 16

$ echo 'Hello, world!' > foo$ git hash-object fooaf5626b4a114abcb82d63db7c8082c3c4756e51b

$ git hash-object --stdinHello, world!^Daf5626b4a114abcb82d63db7c8082c3c4756e51b

$ echo -en 'blob 14\0Hello, world!\n' | shasumaf5626b4a114abcb82d63db7c8082c3c4756e51b

Git stores every single file using a blob object with the predefined format.

Page 17: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.1 git objects. blobs. stdin Page 17

$ git init$ find .git/objects .git/objects .git/objects/info .git/objects/pack

$ echo 'Hello, world!' | git hash-object -w --stdin$ find .git/objects .git/objects .git/objects/af .git/objects/af/5626b4a114abcb82d63db7c8082c3c4756e51b .git/objects/info .git/objects/pack

Page 18: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.1 git objects. blobs. file Page 18

$ git init$ find .git/objects .git/objects .git/objects/info .git/objects/pack

$ echo 'Hello, world!' > foo$ git add foo$ find .git/objects .git/objects .git/objects/af .git/objects/af/5626b4a114abcb82d63db7c8082c3c4756e51b .git/objects/info .git/objects/pack

Page 19: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.1 git objects. blobs. cat-file 1 Page 19

git cat-file

Page 20: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.1 git objects. blobs. cat-file Page 20

$ git cat-file -t af5626b4a114abcb82d63db7c8082c3c4756e51b blob

$ git cat-file -s af5626b4a114abcb82d63db7c8082c3c4756e51b 14

$ git cat-file -p af5626b4a114abcb82d63db7c8082c3c4756e51b Hello, world!

$ git cat-file blob af5626b4a114abcb82d63db7c8082c3c4756e51b Hello, world!

Page 21: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.1 git objects. blobs. no file info Page 21

$ # porcelain$ echo 'Hello, world!' > foo$ git add foo

$ # plumbing$ echo 'Hello, world!' | git hash-object -w --stdin

The following two commands are similar

In both cases, a blob object is created in .git/objects

.git/objects/af/5626b4a114abcb82d63db7c8082c3c4756e51b

Page 22: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.1 git objects. blobs. blob content Page 22

af

fooHello, world!

5626b4a114abcb82d63db7c8082c3c4756e51b

.git/objects/af/5626b4a114abcb82d63db7c8082c3c4756e51b

.foo

blob 14\0Hello, world!\n

af5626b

Page 23: Dev conf git_lecture

git internals DevCon I 2012/11/27

1 git internals

git objects1.1

1.1.2 git trees

Page 24: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. trees Page 24

Trees are used to: - store the filename of a specific file - store a group of files together

<Tree SHA1>

Page 25: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. trees 2 Page 25

# Add a file content to git $ echo 'Hello, world!' > foo$ git add foo$ find .git/objects .git/objects .git/objects/af .git/objects/af/5626b4a114abcb82d63db7c8082c3c4756e51b .git/objects/info .git/objects/pack

Page 26: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. create a tree Page 26

# Add a file content to git $ mkdir bar$ echo 'Hello, world!' > bar/foo$ git add bar/foo$ find .git/objects .git/objects .git/objects/af .git/objects/af/5626b4a114abcb82d63db7c8082c3c4756e51b .git/objects/info .git/objects/pack

Page 27: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. create a tree 2 Page 27

$ git commit -m 'first commit' [master (root-commit) 83862bb] first commit 2 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 bar/foo create mode 100644 foo

Page 28: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. create a tree 3 Page 28

$ find .git/objects .git/objects .git/objects/83 .git/objects/83/862bbb8472f0b802be96b5e59993ca044f3c31 .git/objects/a8 .git/objects/a8/54cc8b798fb00ad0c3a7a63508b8cffe2e979b .git/objects/af .git/objects/af/5626b4a114abcb82d63db7c8082c3c4756e51b .git/objects/f2 .git/objects/f2/df5266567842bbb8a06acca56bcabf813cd73f .git/objects/info .git/objects/pack

Page 29: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. create a tree 4 Page 29

$ git cat-file -t 83862bbb8472f0b802be96b5e59993ca044f3c31 commit

$ git cat-file -t a854cc8b798fb00ad0c3a7a63508b8cffe2e979b tree

$ git cat-file -t af5626b4a114abcb82d63db7c8082c3c4756e51b blob

$ git cat-file -t f2df5266567842bbb8a06acca56bcabf813cd73f tree

Page 30: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. create a tree 5 Page 30

git ls-tree

Page 31: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. inspect a tree Page 31

$ git ls-tree a854cc8b798fb00ad0c3a7a63508b8cffe2e979b 040000 tree f2df5266567842bbb8a06acca56bcabf813cd73f bar 100644 blob af5626b4a114abcb82d63db7c8082c3c4756e51b foo

$ git ls-tree f2df5266567842bbb8a06acca56bcabf813cd73f 100644 blob af5626b4a114abcb82d63db7c8082c3c4756e51b foo

a854cc

f2df52

af5626

af5626

Page 32: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. inspect a tree 2 Page 32

- Blobs does not store any file specific info

- File specific info is stored in trees

- Same operations, run in different computers, will create the same blobs/trees.

Page 33: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. tree. plumbing Page 33

git update-indexgit write-tree

Page 34: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. tree. plumbing 3 Page 34

A commit is the easiest way to create a tree, but it can be created manually (plumbing)

$ git update-index --add --cacheinfo 100644 \af5626b4a114abcb82d63db7c8082c3c4756e51b \bazz

$ git update-index --add --cacheinfo 100644 \af5626b4a114abcb82d63db7c8082c3c4756e51b \bazz2

$ git write-tree0a5de923df76ac94a71800e28a4e449171073e3e

Page 35: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.2 git objects. tree. plumbing 2 Page 35

$ find .git/objects0a/5de923df76ac94a71800e28a4e449171073e3eaf/5626b4a114abcb82d63db7c8082c3c4756e51b

$ git ls-tree 0a5de923df76ac94a71800e28a4e449171073e3e

100644 blob af5626b4a114abcb82d63db7c8082c3c4756e51b bazz100644 blob af5626b4a114abcb82d63db7c8082c3c4756e51b bazz2

0a5de9

af5626

af5626

Page 36: Dev conf git_lecture

git internals DevCon I 2012/11/27

1 git internals

git objects1.1

1.1.3 git commit

Page 37: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.3 git objects. commits Page 37

Create a snapshot of the current status of the repository

<Commit-SHA1>

Page 38: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.3 git objects. commits 2 Page 38

$ git commit -m 'first commit' [master (root-commit) 83862bb] first commit 2 files changed, 2 insertions(+), 0 deletions(-) create mode 100644 bar/foo create mode 100644 foo

$ find .git/objects .git/objects .git/objects/83 .git/objects/83/862bbb8472f0b802be96b5e59993ca044f3c31 .git/objects/a8 .git/objects/a8/54cc8b798fb00ad0c3a7a63508b8cffe2e979b .git/objects/af .git/objects/af/5626b4a114abcb82d63db7c8082c3c4756e51b .git/objects/f2 .git/objects/f2/df5266567842bbb8a06acca56bcabf813cd73f .git/objects/info .git/objects/pack

Page 39: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.3 git objects. commits 3 Page 39

$ git cat-file -p 83862bb tree a854cc8b798fb00ad0c3a7a63508b8cffe2e979b author juandebravo <[email protected]> 1353253703 +0100 committer juandebravo <[email protected]> 1353253703 +0100

first commit

$ git ls-tree HEAD$ git cat-file -p a854cc8b$ git cat-file -p "master^{tree}"

040000 tree f2df5266567842bbb8a06acca56bcabf813cd73f bar 100644 blob af5626b4a114abcb82d63db7c8082c3c4756e51b foo

Page 40: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.3 git objects. commits 4 Page 40

83862bb af5626b

af5626b

a854cc8

f2df526

Page 41: Dev conf git_lecture

git internals DevCon I 2012/11/27

1 git internals

git objects1.1

1.1.4 git tag

Page 42: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.4 git objects. tags. lightweight tag Page 42

lightweight tag

Similar to a commit, but points to a commit rather than to a tree

Like a branch reference, but it never moves

Page 43: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.4 git objects. tags. lightweight tag 1 Page 43

λ git tag v1.0

λ git cat-file -p v1.0tree dba14fbffe61ae959c139d847fcf7709ac7f03d5parent 83862bbb8472f0b802be96b5e59993ca044f3c31author juandebravo <[email protected]> 1353954525 +0100committer juandebravo <[email protected]> 1353954525 +0100

Page 44: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.4 git objects. tags. annotated tag Page 44

annotated tag

Stored as full objects:- checksum- tagger name, email, date- comment- can be signed and verified

Page 45: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.4 git objects. tags. anotated tag Page 45

λ git tag -a v1.1 -m "version 1.1"

λ git cat-file -p v1.1object c2120756cce9f359373d2fd42f8eac5cd5f9cbe9type committag v1.1tagger juandebravo <[email protected]> Tue Nov 22 17:42:02 2012 +0100

version 1.1

Page 46: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.4 git objects. tags. anotated tag 2 Page 46

λ cd CONNECT/tid-commonsλ ls -ltotal 32-rw-r--r-- 1 jdbd staff 1594 27 Nov 07:47 LICENSE.txt-rw-r--r-- 1 jdbd staff 1909 27 Nov 07:47 README.mddrwxr-xr-x 2 jdbd staff 68 22 Aug 13:21 dist-rw-r--r-- 1 jdbd staff 114 30 Oct 23:53 setup.cfg-rw-r--r-- 1 jdbd staff 312 27 Nov 07:47 setup.pydrwxr-xr-x 18 jdbd staff 612 27 Nov 07:47 testsdrwxr-xr-x 24 jdbd staff 816 27 Nov 07:47 tid_commons

Page 47: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.4 git objects. tags. anotated tag 3 Page 47

λ git ls-tree HEAD100644 blob 0d20b6487c61e7d1bde93acf4a14b7a89083a16d .gitignore100644 blob a27144556d3b857a6fb609f39be204996e1f7792 LICENSE.txt100644 blob 15446c94618f3863584814d669048dc3def789cf README.md100644 blob b6c8d6c27ce614109dcb07b2e655ce13727675fa setup.cfg100644 blob 6ee4913e18e4a6a2320ac1e540a585f12bc110b2 setup.py040000 tree a7c28bc15f440bcf3783915f24ff3336b81dd01f tests040000 tree 35ea155eb6899a337b1b0ed5307d0882a0b7858e tid_commons

Page 48: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.1.4 git objects. tags. anotated tag 4 Page 48

λ git tag -a LICENSE a2714455 -m "License file"

λ git cat-file -p LICENSEobject a27144556d3b857a6fb609f39be204996e1f7792type blobtag LICENSEtagger juandebravo <[email protected]> Tue Nov 27 07:48:36 2012 +0100

License file

Page 49: Dev conf git_lecture

git internals DevCon I 2012/11/27

1 git internals

git objects1.1

1.2 git internal structure

Page 50: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2 git internal structure Page 50

.git!"" COMMIT_EDITMSG!"" HEAD!"" branches!"" config!"" description!"" hooks!"" index!"" info#!! $"" exclude!"" logs#!! !"" HEAD#!! $"" refs#!! $"" heads#!! $"" master

!"" objects#!! !"" 83#!! #!! $"" 862bbb8472f0b802be96b5e59993ca044f3c31#!! !"" a8#!! #!! $"" 54cc8b798fb00ad0c3a7a63508b8cffe2e979b#!! !"" af#!! #!! $"" 5626b4a114abcb82d63db7c8082c3c4756e51b#!! !"" f2#!! #!! $"" df5266567842bbb8a06acca56bcabf813cd73f#!! !"" info#!! $"" pack$"" refs !"" heads #!! $"" master $"" tags

Page 51: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.1 git internal structure. HEAD 1 Page 51

HEAD

reference to current checkout commit

Page 52: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.1 git internal structure. HEAD 2 Page 52

$ cat .git/HEAD

ref: refs/heads/master

$ git status

# On branch masternothing to commit (working directory clean)

Page 53: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.1 git internal structure. HEAD 3 Page 53

$ git checkout feature/memoize_client$ cat .git/HEAD

ref: refs/heads/feature/memoize/client

$ git branch feature/memoize_client$ cat .git/HEAD

ref: refs/heads/master

Page 54: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.1 git internal structure. HEAD 4 Page 54

# Modify HEAD$ git symbolic-ref HEAD refs/head/master$ cat .git/HEAD

refs/heads/master

# Check where HEAD is pointing to$ git symbolic-ref HEAD

refs/heads/feature/memoize/client

Page 55: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.1 git internal structure. HEAD 5 Page 55

# Example. zsh# get the name of the branch we are onfunction git_prompt_info() { ref=$(git symbolic-ref HEAD 2> /dev/null) || return echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"}

juandebravo [~/projects/git_internals/repo] (master) ✓

Page 56: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. config Page 56

CONFIGrepository specific configuration

Page 57: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. config 2 Page 57

λ cat .git/config [core]repositoryformatversion = 0filemode = truebare = falselogallrefupdates = trueignorecase = true

Page 58: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. REFS Page 58

REFSreference to tags and branches commits

Page 59: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. REFS 2 Page 59

!"" objects#!! !"" 83#!! #!! $"" 862bbb8472f0b802be96b5e59993ca044f3c31#!! !"" a8#!! #!! $"" 54cc8b798fb00ad0c3a7a63508b8cffe2e979b#!! !"" af#!! #!! $"" 5626b4a114abcb82d63db7c8082c3c4756e51b#!! !"" f2#!! #!! $"" df5266567842bbb8a06acca56bcabf813cd73f#!! !"" info#!! $"" pack$"" refs !"" heads #!! $"" feature #!! #!! $"" memoize_client #!! $"" master $"" tags

Page 60: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. REFS 3 Page 60

λ find .git/refs .git/refs.git/refs/heads.git/refs/heads/feature.git/refs/heads/feature/memoize_client.git/refs/heads/master.git/refs/tags

Page 61: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. REFS 5 Page 61

λ cat .git/refs/heads/feature/memoize_client

λ cat .git/refs/heads/master

83862bbb8472f0b802be96b5e59993ca044f3c31

83862bbb8472f0b802be96b5e59993ca044f3c31

λ git cat-file -p 83862bbb8472f0b802be96b5e59993ca044f3c31 tree a854cc8b798fb00ad0c3a7a63508b8cffe2e979bauthor juandebravo <[email protected]> 1353253703 +0100committer juandebravo <[email protected]> 1353253703 +0100

first commit

Page 62: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. REFS 6 Page 62

λ git checkout feature/memoize_clientλ echo "¿Cómo están ustedes?" >> foo

λ git commit -am "miliki, we love you"

[feature/memoize_client 7d52f00] miliki, we love you 1 files changed, 2 insertions(+), 0 deletions(-)

Page 63: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. REFS 7 Page 63

λ cat .git/refs/heads/feature/memoize_client

7d52f0083ebf11fdfd4ea00f6806e29fd8089ab2

λ cat .git/refs/heads/master

83862bbb8472f0b802be96b5e59993ca044f3c31

Page 64: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. REFS 8 Page 64

λ git log

7d52f00 - (HEAD, feature/memoize_client) miliki, we love you - juandebravo (15 minutes ago)

83862bb - (master) first commit - juandebravo (8 days ago)

Page 65: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. REFS 9 Page 65

λ git log

7d52f00 - (HEAD, feature/memoize_client) miliki, we love you - juandebravo (15 minutes ago)

83862bb - (master) first commit - juandebravo (8 days ago)

λ git tag v1.07d52f00 - (HEAD, v1.0, feature/memoize_client) miliki, we love you - juandebravo (15 minutes ago)

83862bb - (master) first commit - juandebravo (8 days ago)

Page 66: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. REFS 10 Page 66

λ find .git/refs .git/refs.git/refs/heads.git/refs/heads/feature.git/refs/heads/feature/memoize_client.git/refs/heads/master.git/refs/tags.git/refs/tags/v1.0

Page 67: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. REFS 11 Page 67

λ echo "Bieeeeeeeeen" >> foo

λ git commit -am "yet another commit"

[feature/memoize_client c212075] yet another commit 1 files changed, 1 insertions(+), 0 deletions(-)

λ cat .git/refs/tags/v1.0

7d52f0083ebf11fdfd4ea00f6806e29fd8089ab2

Page 68: Dev conf git_lecture

git internals DevCon I 2012/11/27

1.2.2 git internal structure. REFS 12 Page 68

λ git log

c212075 - (HEAD, feature/memoize_client) yet another commit - juandebravo (87 seconds ago)7d52f00 - (v1.0) miliki, we love you - juandebravo (25 minutes ago)83862bb - (master) first commit - juandebravo (8 days ago)

Page 69: Dev conf git_lecture

1

2

git internals

git advanced topics

3 git tips & tricks

git internals DevCon I 2012/11/27

Page 70: Dev conf git_lecture

2 git advanced topics

git bisect2.1

2.2 git reflog

2.3 git stash

git internals DevCon I 2012/11/27

Page 71: Dev conf git_lecture

2 git advanced topics

git bisect2.1

2.2 git reflog

2.3 git stash

git internals DevCon I 2012/11/27

Page 72: Dev conf git_lecture

The bisect command does a binary search through your commit history to help you identify as quickly as possible which commit introduced an issue.

git internals DevCon I 2012/11/27

2.1 git bisect Page 72

Page 73: Dev conf git_lecture

git internals DevCon I 2012/11/27

2.1 git bisect 17 Page 73

Page 74: Dev conf git_lecture

λ tree.!"" main#!! !"" __init__.py#!! $"" foo.py$"" tests $"" foo_tests.py

git internals DevCon I 2012/11/27

2.1 git bisect 2 Page 74

Page 75: Dev conf git_lecture

import re

reg_expr = re.compile('^\d{,10}$')

class Foo(object):

@staticmethod def is_valid(value): _value = reg_expr.match(str(value)) return True if _value else False

git internals DevCon I 2012/11/27

2.1 git bisect 3 Page 75

Page 76: Dev conf git_lecture

import unittestfrom pyshould import should

from main.foo import Foo

class TestFoo(unittest.TestCase):

def setUp(self): self.foo = Foo

def test_is_valid_string(self): self.foo.is_valid('1234') | should.be_true

def test_is_valid_number(self): self.foo.is_valid(1234) | should.be_true

def test_is_valid_one_digit(self): self.foo.is_valid(1) | should.be_true

git internals DevCon I 2012/11/27

2.1 git bisect 4 Page 76

Page 77: Dev conf git_lecture

λ nosetests -sv test_is_valid_number (foo_tests.TestFoo) ... oktest_is_valid_one_digit (foo_tests.TestFoo) ... oktest_is_valid_string (foo_tests.TestFoo) ... ok

---------------------------------------------------------Ran 3 tests in 0.013s

OK

git internals DevCon I 2012/11/27

2.1 git bisect 5 Page 77

Page 78: Dev conf git_lecture

λ git lg* 2ca0083 - (HEAD, master) change regepx - juandebravo (12 minutes ago)* 1824e4f - change regepx - juandebravo (12 minutes ago)* 898489c - change regepx - juandebravo (12 minutes ago)* f683803 - change regepx - juandebravo (12 minutes ago)* dbe4c2e - change regepx - juandebravo (12 minutes ago)* ec56109 - change regepx - juandebravo (12 minutes ago)* 14b0d5f - change regepx - juandebravo (13 minutes ago)* 938a43b - change regepx - juandebravo (13 minutes ago)* bb9f17a - change regepx - juandebravo (13 minutes ago)* a8aa38f - change regepx - juandebravo (13 minutes ago)* 74e307f - remove pyc files - juandebravo (2 hours ago)* 82e49f3 - fix .gitignore - juandebravo (2 hours ago)* 9a8bdf8 - first commit - juandebravo (2 hours ago)

git internals DevCon I 2012/11/27

2.1 git bisect 6 Page 78

Page 79: Dev conf git_lecture

λ nosetests -sv test_is_valid_number (foo_tests.TestFoo) ... FAILtest_is_valid_one_digit (foo_tests.TestFoo) ... oktest_is_valid_string (foo_tests.TestFoo) ... FAIL

-------------------------------------------------------Ran 3 tests in 0.013s

FAILED (failures=2)

git internals DevCon I 2012/11/27

2.1 git bisect 7 Page 79

Page 80: Dev conf git_lecture

1) Figure out a commit that was right: good commit2) Figure out a commit that is wrong: the current one, bad commit3) run git bisect command

git bisect is your friend

git internals DevCon I 2012/11/27

2.1 git bisect 8 Page 80

Page 81: Dev conf git_lecture

λ git lg* dc46efc - (HEAD, master) add unittest main - juandebravo (4 minutes ago)* 2ca0083 - change regepx - juandebravo (12 minutes ago)* 1824e4f - change regepx - juandebravo (12 minutes ago)* 898489c - change regepx - juandebravo (12 minutes ago)* f683803 - change regepx - juandebravo (12 minutes ago)* dbe4c2e - change regepx - juandebravo (12 minutes ago)* ec56109 - change regepx - juandebravo (12 minutes ago)* 14b0d5f - change regepx - juandebravo (13 minutes ago)* 938a43b - change regepx - juandebravo (13 minutes ago)* bb9f17a - change regepx - juandebravo (13 minutes ago)* a8aa38f - change regepx - juandebravo (13 minutes ago)* 74e307f - remove pyc files - juandebravo (2 hours ago)* 82e49f3 - fix .gitignore - juandebravo (2 hours ago)* 9a8bdf8 - first commit - juandebravo (2 hours ago)

git internals DevCon I 2012/11/27

2.1 git bisect 9 Page 81

Page 82: Dev conf git_lecture

reg = r'^\s*(?=^.{7,}$)(?=.*[a-zA-Z])(?=.*\d)(?!.*\s).*?\s*$'

reg_expr = re.compile(reg)

git internals DevCon I 2012/11/27

2.1 git bisect 18 Page 82

Page 83: Dev conf git_lecture

λ git bisect start HEAD 9a8bdf8

Bisecting: 5 revisions left to test after this (roughly 3 steps)[ec56109cc441293571a0773c83006afe483ff0e6] change regepx

git internals DevCon I 2012/11/27

2.1 git bisect 10 Page 83

Page 84: Dev conf git_lecture

λ git lg* dc46efc - (HEAD, master) add unittest main - juandebravo (4 minutes ago)* 2ca0083 - change regepx - juandebravo (12 minutes ago)* 1824e4f - change regepx - juandebravo (12 minutes ago)* 898489c - change regepx - juandebravo (12 minutes ago)* f683803 - change regepx - juandebravo (12 minutes ago)* dbe4c2e - change regepx - juandebravo (12 minutes ago)* ec56109 - change regepx - juandebravo (12 minutes ago)* 14b0d5f - change regepx - juandebravo (13 minutes ago)* 938a43b - change regepx - juandebravo (13 minutes ago)* bb9f17a - change regepx - juandebravo (13 minutes ago)* a8aa38f - change regepx - juandebravo (13 minutes ago)* 74e307f - remove pyc files - juandebravo (2 hours ago)* 82e49f3 - fix .gitignore - juandebravo (2 hours ago)* 9a8bdf8 - first commit - juandebravo (2 hours ago)

git internals DevCon I 2012/11/27

2.1 git bisect 11 Page 84

Page 85: Dev conf git_lecture

$ git bisect run nosetests -sv

Ran 3 tests in 0.014s

OKBisecting: 2 revisions left to test after this (roughly 2 steps)[898489c1e7b77989717a84f08f49f43cf215c120] change regepx

git internals DevCon I 2012/11/27

2.1 git bisect 12 Page 85

Page 86: Dev conf git_lecture

λ git lg* dc46efc - (HEAD, master) add unittest main - juandebravo (4 minutes ago)* 2ca0083 - change regepx - juandebravo (12 minutes ago)* 1824e4f - change regepx - juandebravo (12 minutes ago)* 898489c - change regepx - juandebravo (12 minutes ago)* f683803 - change regepx - juandebravo (12 minutes ago)* dbe4c2e - change regepx - juandebravo (12 minutes ago)* ec56109 - change regepx - juandebravo (12 minutes ago)* 14b0d5f - change regepx - juandebravo (13 minutes ago)* 938a43b - change regepx - juandebravo (13 minutes ago)* bb9f17a - change regepx - juandebravo (13 minutes ago)* a8aa38f - change regepx - juandebravo (13 minutes ago)* 74e307f - remove pyc files - juandebravo (2 hours ago)* 82e49f3 - fix .gitignore - juandebravo (2 hours ago)* 9a8bdf8 - first commit - juandebravo (2 hours ago)

git internals DevCon I 2012/11/27

2.1 git bisect 19 Page 86

Page 87: Dev conf git_lecture

----------------------------------------------------------Ran 3 tests in 0.014s

FAILED (failures=2)Bisecting: 0 revisions left to test after this (roughly 1 step)[f6838032010b18a7e3998f4a2fdcde65124424a2] change regepx

git internals DevCon I 2012/11/27

2.1 git bisect 13 Page 87

Page 88: Dev conf git_lecture

λ git lg* dc46efc - (HEAD, master) add unittest main - juandebravo (4 minutes ago)* 2ca0083 - change regepx - juandebravo (12 minutes ago)* 1824e4f - change regepx - juandebravo (12 minutes ago)* 898489c - change regepx - juandebravo (12 minutes ago)* f683803 - change regepx - juandebravo (12 minutes ago)* dbe4c2e - change regepx - juandebravo (12 minutes ago)* ec56109 - change regepx - juandebravo (12 minutes ago)* 14b0d5f - change regepx - juandebravo (13 minutes ago)* 938a43b - change regepx - juandebravo (13 minutes ago)* bb9f17a - change regepx - juandebravo (13 minutes ago)* a8aa38f - change regepx - juandebravo (13 minutes ago)* 74e307f - remove pyc files - juandebravo (2 hours ago)* 82e49f3 - fix .gitignore - juandebravo (2 hours ago)* 9a8bdf8 - first commit - juandebravo (2 hours ago)

git internals DevCon I 2012/11/27

2.1 git bisect 20 Page 88

Page 89: Dev conf git_lecture

----------------------------------------------------------Ran 3 tests in 0.013s

OK898489c1e7b77989717a84f08f49f43cf215c120 is the first bad commitcommit 898489c1e7b77989717a84f08f49f43cf215c120Author: juandebravo <[email protected]>Date: Mon Nov 26 21:48:50 2012 +0100

change regepx

:040000 040000 70ccfdcf5d796cbd581a840c1c92ebe47b40345e 3912c8faed49a91e6fe3f68e7370a86c120c2844 M

mainbisect run success

git internals DevCon I 2012/11/27

2.1 git bisect 14 Page 89

Page 90: Dev conf git_lecture

λ git lg* dc46efc - (master) add unittest main - juandebravo (4 minutes ago)* 2ca0083 - change regepx - juandebravo (49 minutes ago)* 1824e4f - change regepx - juandebravo (49 minutes ago)* 898489c - (refs/bisect/bad) change regepx - juandebravo (49 minutes ago)* f683803 - (HEAD, refs/bisect/good-f6838032010b18a7e3998f4a2fdcde65124424a2) change regepx - juandebravo (49 minutes* dbe4c2e - change regepx - juandebravo (49 minutes ago)* ec56109 - (refs/bisect/good-ec56109cc441293571a0773c83006afe483ff0e6) change regepx - juandebravo (50 minutes ago)* 14b0d5f - change regepx - juandebravo (50 minutes ago)* 938a43b - change regepx - juandebravo (50 minutes ago)* bb9f17a - change regepx - juandebravo (50 minutes ago)* a8aa38f - change regepx - juandebravo (50 minutes ago)* 74e307f - (refs/bisect/good-74e307f9003e661f3c5e5f9697915d2a1de2e96e) remove pyc files - juandebravo (2 hours ago)* 82e49f3 - fix .gitignore - juandebravo (2 hours ago)* 9a8bdf8 - first commit - juandebravo (2 hours ago)

git internals DevCon I 2012/11/27

2.1 git bisect 15 Page 90

Page 91: Dev conf git_lecture

λ git bisect resetPrevious HEAD position was f683803... change regepxSwitched to branch 'master'

Reset: remove bisect information from history

git internals DevCon I 2012/11/27

2.1 git bisect 16 Page 91

Page 92: Dev conf git_lecture

2 git advanced topics

git bisect2.1

2.2 git reflog

git stash

2.2

2.1

2.3

2.2

git internals DevCon I 2012/11/27

Page 93: Dev conf git_lecture

A log of where your HEAD and branch references have been for the last few months.

git internals DevCon I 2012/11/27

2.2 git reflog Page 93

Page 94: Dev conf git_lecture

It persists independently of other changes in your repository.

I could unlink any commit from my repository (using reset), yet it would still be referenced by the reflog for another 30 days!

git internals DevCon I 2012/11/27

2.2 git reflog 4 Page 94

Page 95: Dev conf git_lecture

dc46efc HEAD@{0}: checkout: moving from 2ca0083fc6ba6bdfbf3000122eba51359309ae7f to master2ca0083 HEAD@{1}: checkout: moving from master to HEAD@{22}dc46efc HEAD@{2}: checkout: moving from 1824e4fdcf1fc963554b2ae671ede799a692838a to master1824e4f HEAD@{3}: checkout: moving from ec56109cc441293571a0773c83006afe483ff0e6 to HEAD@{36}ec56109 HEAD@{4}: checkout: moving from master to HEAD@{70}dc46efc HEAD@{5}: checkout: moving from f6838032010b18a7e3998f4a2fdcde65124424a2 to masterf683803 HEAD@{6}: checkout: moving from 898489c1e7b77989717a84f08f49f43cf215c120 to f6838032010b18a7e3998f4a2fdcde651898489c HEAD@{7}: checkout: moving from ec56109cc441293571a0773c83006afe483ff0e6 to 898489c1e7b77989717a84f08f49f43cfec56109 HEAD@{8}: checkout: moving from master to ec56109cc441293571a0773c83006afe483ff0e6dc46efc HEAD@{9}: checkout: moving from master to masterdc46efc HEAD@{10}: checkout: moving from a8aa38f28e9bc76bdc73603b73f1a31a674b28dd to master

$ git reflog

git internals DevCon I 2012/11/27

2.2 git reflog 2 Page 95

Page 96: Dev conf git_lecture

$ git show HEAD@{5}

$ git show master@{2.months.ago}

git internals DevCon I 2012/11/27

2.2 git reflog 3 Page 96

Page 97: Dev conf git_lecture

2 git advanced topics

git rebase2.1

2.2

git stash

2.2 git bisect

2.1

2.3

git internals DevCon I 2012/11/27

Page 98: Dev conf git_lecture

Imagine this scenario...

git internals DevCon I 2012/11/27

2.3 git stash 9 Page 98

Page 99: Dev conf git_lecture

1) you are working in a cool user story2) a JIRA task is assigned to you

git internals DevCon I 2012/11/27

2.3 git stash Page 99

Page 100: Dev conf git_lecture

1) you are working in a cool user story2) a JIRA task is assigned to you3) JIRA is down

git internals DevCon I 2012/11/27

2.3 git stash 11 Page 100

Page 101: Dev conf git_lecture

1) you are working in a cool user story2) a JIRA task is assigned to you3) JIRA is down4) coffee while JIRA comes back to life5) you figure out what you should do6) you need to switch to another branch7) but you don't want to commit half-work

git internals DevCon I 2012/11/27

2.3 git stash 10 Page 101

Page 102: Dev conf git_lecture

git internals DevCon I 2012/11/27

2.3 git stash 2 Page 102

Page 103: Dev conf git_lecture

λ git status# On branch master# Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in working directory)## modified: main/foo.py#no changes added to commit (use "git add" and/or "git commit -a")

git internals DevCon I 2012/11/27

2.3 git stash 3 Page 103

Page 104: Dev conf git_lecture

λ git stashSaved working directory and index state WIP on master: dc46efc add unittest mainHEAD is now at dc46efc add unittest main

λ git stash liststash@{0}: WIP on master: dc46efc add unittest main

git internals DevCon I 2012/11/27

2.3 git stash 4 Page 104

Page 105: Dev conf git_lecture

λ git lg* 7cf1d41 - (refs/stash) WIP on master: dc46efc add main - juandebravo (6 minutes ago)|\ | * 7bcc6de - index on master: dc46efc add unittest main - juandebravo (6 minutes ago)|/ * dc46efc - (HEAD, master) add unittest main - juandebravo (46 minutes ago)* 2ca0083 - change regepx - juandebravo (2 hours ago)* 1824e4f - change regepx - juandebravo (2 hours ago)* 898489c - change regepx - juandebravo (2 hours ago)

git internals DevCon I 2012/11/27

2.3 git stash 5 Page 105

Page 106: Dev conf git_lecture

λ git cat-file commit 7cf1d41tree f1d28d6483820c19e43bc17d46f3a9ab393aba6fparent dc46efcfa8fee91b94ed40d78e8b43f7afc53196parent 7bcc6de1d49f8113d019e2c45b87938363bb54bdauthor juandebravo <[email protected]> 1353968043 +0100committer juandebravo <[email protected]> 1353968043 +0100

WIP on master: dc46efc add unittest main

git internals DevCon I 2012/11/27

2.3 git stash 6 Page 106

Page 107: Dev conf git_lecture

λ git cat-file commit 7bcc6detree 71d53f971a4924e0cec9264a0774ba63a6eba3b9parent dc46efcfa8fee91b94ed40d78e8b43f7afc53196author juandebravo <[email protected]> 1353968043 +0100committer juandebravo <[email protected]> 1353968043 +0100

index on master: dc46efc add unittest main

git internals DevCon I 2012/11/27

2.3 git stash 7 Page 107

Page 108: Dev conf git_lecture

λ git stash list

λ cat .git/refs/stash

stash@{0}: WIP on master: dc46efc add unittest main

7cf1d41c1352d4df176d53d566354b8ca4d517cd

git internals DevCon I 2012/11/27

2.3 git stash 8 Page 108

Page 109: Dev conf git_lecture

1

2 git advanced topics

3 git tips & tricks

git internals

git internals DevCon I 2012/11/27

Page 110: Dev conf git_lecture

WORK WITH MORE THAN ONE REMOTE

git internals DevCon I 2012/11/27

3. git tips and tricks Page 110

Page 111: Dev conf git_lecture

$ git clone [email protected]:jdbd/txsip.git

1) Clone repository

0) Fork repository

$ git remote -vorigin [email protected]:jdbd/txsip.git (fetch)origin [email protected]:jdbd/txsip.git (push)

git internals DevCon I 2012/11/27

3. git tips and tricks 2 Page 111

Page 112: Dev conf git_lecture

$ git remote -vorigin [email protected]:jdbd/txsip.git (fetch)origin [email protected]:jdbd/txsip.git (push)upstream [email protected]:ggb/txsip.git (fetch)upstream [email protected]:ggb/txsip.git (push)

$ git remote add upstream [email protected]:ggb/txsip.git

2) Add upstream repository

git internals DevCon I 2012/11/27

3. git tips and tricks 4 Page 112

Page 113: Dev conf git_lecture

$ git checkout -b task/minor_changes..git commit -m "bla bla"git push origin task/minor_changes

3) Work...

4) Pull request...

git internals DevCon I 2012/11/27

3. git tips and tricks 3 Page 113

Page 114: Dev conf git_lecture

5) Your pull request is accepted by the upstream repo owner (thanks @ggb)

git internals DevCon I 2012/11/27

3. git tips and tricks 5 Page 114

Page 115: Dev conf git_lecture

6) Fetch and sync changes$ git fetch upstream$ git checkout master$ git merge upstream/master

Updating c87ea52..eec65b1Fast-forward .gitignore | 1 + README.md | 2 +- libs/pjsua.pyc | Bin 104192 -> 100106 bytes txsip/__init__.py | 1 + txsip.py => txsip/main.py | 0 5 files changed, 3 insertions(+), 1 deletions(-) create mode 100644 .gitignore create mode 100644 txsip/__init__.py rename txsip.py => txsip/main.py (100%)

git internals DevCon I 2012/11/27

3. git tips and tricks 6 Page 115

Page 116: Dev conf git_lecture

7) Push changes to origingit push origin mastergit lg

* eec65b1 - (HEAD, upstream/master, origin/master, origin/HEAD, master) Merge pull request #2 from jdbd/task/minor_|\ | * f9e29a5 - (origin/task/minor_changes) PDI folder structure - juandebravo (4 days ago)| * 4b653ac - fix typo - juandebravo (4 days ago)| * 100a3db - create .gitignore file - juandebravo (4 days ago)| * 41ea559 - remove *.pyc - juandebravo (4 days ago)|/ * c87ea52 - UPDATE packet size limit in pjsip - ggb (5 weeks ago)

git internals DevCon I 2012/11/27

3. git tips and tricks 7 Page 116

Page 117: Dev conf git_lecture

HIGHLIGHT YOUR SHELL

git internals DevCon I 2012/11/27

3. git tips and tricks 8 Page 117

Page 118: Dev conf git_lecture

git internals DevCon I 2012/11/27

3. git tips and tricks 9 Page 118

Page 119: Dev conf git_lecture

USE .GITCONFIG

git internals DevCon I 2012/11/27

3. git tips and tricks 12 Page 119

Page 120: Dev conf git_lecture

[user] name = juandebravo email = [email protected][alias] lg= log --graph --abbrev-commit --date=relative --all co = checkout br = branch ci = commit st = status db = branch -d unstage = reset HEAD -- last = log -1 HEAD release = !"sh -c 'git checkout -b release/$1' develop"

git internals DevCon I 2012/11/27

3. git tips and tricks 13 Page 120

Page 121: Dev conf git_lecture

git internals DevCon I 2012/11/27

3. git tips and tricks 14 Page 121

Page 122: Dev conf git_lecture

git internalsgit advanced topics

Juan de Bravo Questions?

git internals DevCon I 2012/11/27