Git Presentation - Purple Scout AB Malmö

125
Git Sébastien Cevey <[email protected] > http://bytes.inso.cc / Purple Scout presentation (April 17, 2009) Swiss Federal Institute of Technology, Lausanne XMMS2 Music Player Playlouder MSP

description

Sébastien Cevey gives a very nice presentation about the Git project at Purple Scout AB in April 2009.

Transcript of Git Presentation - Purple Scout AB Malmö

Page 1: Git Presentation - Purple Scout AB Malmö

GitSébastien Cevey <[email protected]>

http://bytes.inso.cc/

Purple Scout presentation (April 17, 2009)

Swiss Federal Institute of Technology, Lausanne

XMMS2 Music Player Playlouder MSP

Page 2: Git Presentation - Purple Scout AB Malmö

Outline

1. Introduction

2. Concepts & Local Workflow

3. Distributed Work

4. Advanced Features

5. Conclusion

2

Page 3: Git Presentation - Purple Scout AB Malmö

1. Introduction

3

Page 4: Git Presentation - Purple Scout AB Malmö

Version Control Systems

• Record history

• Facilitate team work

• Expose source code

• Branch/merge development paths

4

Page 5: Git Presentation - Purple Scout AB Malmö

Centralized VCS

Repository

Working Copy

Working Copy

Working Copy

Alice Bob Charlie

Master

Copies

permissionsbranches

checkout

commit

update

5

Page 6: Git Presentation - Purple Scout AB Malmö

Distributed VCS

Repository

Working Copy

Repository

Working Copy

Repository

Working Copy

Alice Bob Charlie

RepositoryRepository

Repository

clone

push

pull

6

Remote(public/private)

Local

Page 7: Git Presentation - Purple Scout AB Malmö

Centralized vs Distributed• asynchronous

- copy = repository- loose hierarchy

• offline:- history- commit- branches

• more powerful

• synchronous- unique reference- fixed hierarchy

• centralized access control

• simpler

7

Page 8: Git Presentation - Purple Scout AB Malmö

Centralized vs Distributed• asynchronous

- copy = repository- loose hierarchy

• offline:- history- commit- branches

• more powerful

• fun! ^_^

• synchronous- unique reference- fixed hierarchy

• centralized access control

• simpler

• tråkig... ;_;

8

Page 9: Git Presentation - Purple Scout AB Malmö

Git History• Linus Torvalds needed a replacement for

BitKeeper to maintain the Linux kernel

• Requirements:

• Distributed

• Scalable & Fast

• Integrity guarantee

• First prototype on April 7, 2005

• Now used by many FOSS projects:Linux kernel, Freedesktop (X.org), Ruby on Rails, XMMS2, Android, Gnome, GCC, etc.

9

• Non-linear, custom workflows

• Talk over HTTP, SSH, FTP, rsync, email, etc

Page 10: Git Presentation - Purple Scout AB Malmö

Why “Git” ?git (plural gits)

2. (British, slang, pejorative) A silly, incompetent, stupid, annoying or childish person.

“Jacko is a git.”

Linus Torvalds:

“I'm an egotistical bastard, and I name all my projects after myself. First Linux, now git.”

Alternatively: “Global Information Tracker”10

Page 11: Git Presentation - Purple Scout AB Malmö

Git vs. other DVCS(Mercurial, Darcs, Bazaar, Monotone, etc)

+ Fast, scalable (execution and setup)

+ Powerful small tools (Unix spirit)

+ Lively development

+ Linus (smart design)

11

Page 12: Git Presentation - Purple Scout AB Malmö

Git vs. other DVCS(Mercurial, Darcs, Bazaar, Monotone, etc)

+ Fast, scalable (execution and setup)

+ Powerful small tools (Unix spirit)

+ Lively development

+ Linus (smart design)

- Learning curve (grasp concepts)

- Imperfect on MS Windows

- Linus (no library)

12

Page 13: Git Presentation - Purple Scout AB Malmö

Why Git is better than X?• Cheap Local Branching

• Everything is Local

• Git is Fast

• Git is Small

• The Staging Area

• Distributed

• Any Workflow

• Easy to LearnSource: http://whygitisbetterthanx.com/

13

Page 14: Git Presentation - Purple Scout AB Malmö

2. Concepts&

Local Workflow

14

Page 15: Git Presentation - Purple Scout AB Malmö

Git Repositoryrepository = object database

15.git/objects/

Page 16: Git Presentation - Purple Scout AB Malmö

Git Repository

a23f

3347

8e9b

f943

601a

cc39

1432

efa4

1798

1260

3e93

5592

dd85

335e

57bc

332e 23a7

34fe cca6

2211 5390

93a4 cc5e

b3ca

id(object) = SHA1(object content)

6679

eab4

16.git/objects/8e/9b...

Page 17: Git Presentation - Purple Scout AB Malmö

Git Repository

a23f

3347

8e9b

f943

601a

cc39

1432

efa4

1798

1260

3e93

5592

dd85

335e

57bc

332e 23a7

34fe cca6

2211 5390

93a4 cc5e

b3ca

content is stored in blob objects⇒ File

#include <stdio.h>

int main (){ printf(”Hello World”); return 0;}

6679

eab4

17

git cat-file -p 93a4

Page 18: Git Presentation - Purple Scout AB Malmö

Git Repository

a23f

3347

8e9b

f943

601a

cc39

1432

efa4

1798

1260

3e93

5592

dd85

335e

57bc

332e 23a7

34fe cca6

2211 5390

93a4 cc5e

b3ca

structure is stored in tree objects⇒ Directory

040000 tree cc5e src100644 blob 93a4 hw.c

6679

eab4

18

git cat-file -p 1260

Page 19: Git Presentation - Purple Scout AB Malmö

Git Repository

a23f

3347

8e9b

f943

601a

cc39

1432

efa4

1798

1260

3e93

5592

dd85

335e

57bc

332e 23a7

34fe cca6

2211 5390

93a4 cc5e

b3ca

history is stored in commit objects⇒ Authenticated hierarchical snapshots

tree 1260...parent f943...author Tom <tom@...> 1204666883 +0100committer Max <max@...> 1204666883 +0100

Fixed a major bug in Hello World.

6679

eab4

19

git cat-file -p 1432

Note: identity is a function of the whole history!i.e. build-in integrity check

Page 20: Git Presentation - Purple Scout AB Malmö

Git Repository

a23f

3347

8e9b

f943

601a

cc39

1432

efa4

1798

1260

3e93

5592

dd85

335e

57bc

332e 23a7

34fe cca6

2211 5390

93a4 cc5e

b3ca

references are stored as tag objects⇒ (Signed) symbolic link

6679

eab4

object a23f...type committag v1.0.7tagger Jack <jack@...> 1136523576 -0800

GIT 1.0.7-----BEGIN PGP SIGNATURE-----Version: GnuPG v1.4.2 (GNU/Linux)

iD8D…-----END PGP SIGNATURE-----

20

git cat-file -p 335e

Page 21: Git Presentation - Purple Scout AB Malmö

Git Repository

a23f

3347

8e9b

f943

601a

cc39

1432

efa4

1798

1260

3e93

5592

dd85

335e

57bc

332e 23a7

34fe cca6

2211 5390

93a4 cc5e

b3ca

6679

eab4

v1.0

alpha-3

mas

ter

feat

ure-

Xbranches

tags

point to a commit in the graph

21

point to an object (usually a

commit)

practical repository entry points⇒ symbolic links git branch

git tag

Page 22: Git Presentation - Purple Scout AB Malmö

History Graph

a23f

3347

8e9b

f943

601a

cc39

1432

v1.0

alpha-3

mas

ter

feat

ure-

Xbranches

tags

22

HEAD

Page 23: Git Presentation - Purple Scout AB Malmö

Git workflow

23

$ find../src./src/Makefile./src/helloworld.c./src/helloworld.h./Makefile./IDEAS./README

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

Page 24: Git Presentation - Purple Scout AB Malmö

initialize

24

$ git initInitialized empty Git repository in /path/example/.git/

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

Page 25: Git Presentation - Purple Scout AB Malmö

check status

25

$ git status# On branch master## Initial commit## Untracked files:# (use "git add <file>..." to include in what will be committed)## IDEAS# Makefile# README# src/nothing added to commit but untracked files present (use "git add" to track)

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

Page 26: Git Presentation - Purple Scout AB Malmö

add files

26

$ git add .

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

Page 27: Git Presentation - Purple Scout AB Malmö

check status

27

$ git status# On branch master## Initial commit## Changes to be committed:# (use "git rm --cached <file>..." to unstage)## new file: IDEAS# new file: Makefile# new file: README# new file: src/Makefile# new file: src/helloworld.c# new file: src/helloworld.h

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

Page 28: Git Presentation - Purple Scout AB Malmö

commit

28

$ git commit -a -m "First import"Created initial commit 04f087b: First import 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 IDEAS create mode 100644 Makefile create mode 100644 README create mode 100644 src/Makefile create mode 100644 src/helloworld.c create mode 100644 src/helloworld.h

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

Page 29: Git Presentation - Purple Scout AB Malmö

hack

29

$ echo "Blah blah" >> README

$ rm IDEAS

04f087b

mas

ter

MakefileREADME <- editedsrc|-- Makefile|-- helloworld.c`-- helloworld.h

Page 30: Git Presentation - Purple Scout AB Malmö

check status

30

$ git status# On branch master# Changed but not updated:# (use "git add/rm <file>..." to update what will be committed)## deleted: IDEAS# modified: README#no changes added to commit (use "git add" and/or "git commit -a")

04f087b

mas

ter

MakefileREADME <- editedsrc|-- Makefile|-- helloworld.c`-- helloworld.h

Page 31: Git Presentation - Purple Scout AB Malmö

commit

31

$ git commit -a -m "More stuff"Created commit 54eda77: More stuff 1 files changed, 1 insertions(+), 0 deletions(-) delete mode 100644 IDEAS

04f087b

mas

ter

54eda77

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

Page 32: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

view commit

32

$ git show commit 54eda77a85b0e5d1891653a36ff08faf133df030Author: Sebastien Cevey <[email protected]>Date: Mon Apr 13 21:23:02 2009 +0200

More stuff

diff --git a/IDEAS b/IDEASdeleted file mode 100644index e69de29..0000000diff --git a/README b/READMEindex e69de29..579c5b1 100644--- a/README+++ b/README@@ -0,0 +1 @@+Blah blah

04f087b

mas

ter

54eda77

Page 33: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

view commit

33

$ git show --pretty=rawcommit 54eda77a85b0e5d1891653a36ff08faf133df030tree 524f0cfe293064d18ac0c12a55baaf2b34d42e8dparent 04f087b23ce96c2b24f9f6b5f7cf437af33e19c6author Sebastien Cevey <[email protected]> 1239650582 +0200committer Sebastien Cevey <[email protected]> 1239650582 +0200

More stuff

[...]

04f087b

mas

ter

54eda77 524f0c

Page 34: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

view history

34

$ git logcommit 54eda77a85b0e5d1891653a36ff08faf133df030Author: Sebastien Cevey <[email protected]>Date: Mon Apr 13 21:23:02 2009 +0200

More stuff

commit 04f087b23ce96c2b24f9f6b5f7cf437af33e19c6Author: Sebastien Cevey <[email protected]>Date: Mon Apr 13 21:11:58 2009 +0200

First import

04f087b

mas

ter

54eda77

Page 35: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c <- edited`-- helloworld.h

hack again

35

$ echo "/* End of fil */" >> src/helloworld.c

04f087b

mas

ter

54eda77

Page 36: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c <- edited`-- helloworld.h

diff modifs

36

$ git diffdiff --git a/src/helloworld.c b/src/helloworld.cindex e69de29..54a606d 100644--- a/src/helloworld.c+++ b/src/helloworld.c@@ -0,0 +1 @@+/* End of fil */

04f087b

mas

ter

54eda77

Page 37: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

commit

37

$ git commit -a -m “Hasty commit”Created commit 3b5ba16: Hasty commit 1 files changed, 1 insertions(+), 0 deletions(-)

04f087b

mas

ter

54eda77

3b5ba16

Page 38: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

rewind history

38

$ git reset --hard HEAD^ # parent of HEADHEAD is now at 54eda77 More stuff

$ git status# On branch masternothing to commit (working directory clean)

04f087b

mas

ter

54eda77

Page 39: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c <- edited`-- helloworld.h

rewind history

39

$ git reset HEAD^ # parent of HEADsrc/helloworld.c: locally modified

$ git diffdiff --git a/src/helloworld.c b/src/helloworld.cindex e69de29..54a606d 100644--- a/src/helloworld.c+++ b/src/helloworld.c@@ -0,0 +1 @@+/* End of fil */

04f087b

mas

ter

54eda77

Page 40: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

new history

40

$ $EDITOR src/helloworld.c

$ git commit -a -m "Proper commit"Created commit 0657c63: Proper commit 1 files changed, 1 insertions(+), 0 deletions(-)

04f087b

mas

ter

54eda77

0657c63

Page 41: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

tag

41

$ git tag stable

04f087b

mas

ter

54eda77

0657c63stable

Page 42: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

new branch

42

$ git branch feature-X 54eda77

$ git branch feature-X* master

04f087b

mas

ter

54eda77

0657c63stable

feat

ure-

X

Page 43: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

switch branch

43

$ git checkout feature-XSwitched to branch "feature-X"

$ git branch* feature-X master

04f087b

mas

ter

54eda77

0657c63stable

feat

ure-

X

Page 44: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

commit branch

44

$ $EDITOR src/helloworld.c

$ git commit -a -m "Work on X"Created commit 6195849: Work on X 1 files changed, 1 insertions(+), 0 deletions(-)

04f087b

mas

ter

54eda77

0657c63stable

feat

ure-

X

6195849

Page 45: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

commit branch

45

$ $EDITOR README

$ git commit -a -m "Edit README for X"Created commit 20fbfdd: Edit README for X 1 files changed, 1 insertions(+), 0 deletions(-)

04f087b

mas

ter

54eda77

0657c63stable

feat

ure-

X

20fbfdd

02bbf10

Page 46: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

diff branches

46

$ git checkout masterSwitched to branch "master"

$ git diff feature-Xdiff --git a/README b/READMEindex 29a6a68..579c5b1 100644--- a/README+++ b/README@@ -1,2 +1 @@ Blah blah-foo[...]

04f087b

mas

ter

54eda77

0657c63stable

feat

ure-

X

1e4cfe4

02bbf10

Page 47: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

merge

47

$ git merge feature-XMerge made by recursive. README | 1 + src/helloworld.h | 1 + 2 files changed, 2 insertions(+), 0 deletions(-)

04f087b

mas

ter

54eda77

0657c63stable

feat

ure-

X

1e4cfe4

02bbf10

69e105

Page 48: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

view merge commit

48

$ git showcommit 69e105f3d1991c34d74d079197d5f1b1917d3120Merge: 0657c63... 20fbfdd...Author: Sebastien Cevey <[email protected]>Date: Mon Apr 13 22:49:16 2009 +0200

Merge branch 'feature-X'

04f087b

mas

ter

54eda77

0657c63stable

feat

ure-

X

1e4cfe4

02bbf10

69e105

Page 49: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

undo

49

$ git reset --hard stable # undo last mergeHEAD is now at 0657c63 Proper commit

$ git checkout feature-XSwitched to branch "feature-X"

04f087b

mas

ter

54eda77

0657c63stable

feat

ure-

X

20fbfdd

02bbf10

Page 50: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

rebase

50

$ git rebase masterFirst, rewinding head to replay your work on top of it...Applying: Work on XApplying: Edit README for X

$ git show-branch * [feature-X] Edit README for X ! [master] Proper commit- -* [feature-X] Edit README for X* [feature-X^] Work on X*+ [master] Proper commit

04f087b

mas

ter

54eda77

0657c63stable

feat

ure-

X

aeaf0a

2e309b

Page 51: Git Presentation - Purple Scout AB Malmö

MakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

trivial merge

51

$ git checkout masterSwitched to branch "master"

$ git merge feature-X Updating 0657c63..aeaf0a9Fast forward README | 1 + src/helloworld.h | 1 + 2 files changed, 2 insertions(+), 0 deletions(-)

04f087b

mas

ter

54eda77

0657c63stable

feat

ure-

X

aeaf0a

2e309b

Page 52: Git Presentation - Purple Scout AB Malmö

git show-branch

52

Page 53: Git Presentation - Purple Scout AB Malmö

gitk

53

Page 54: Git Presentation - Purple Scout AB Malmö

gitk

54

Page 55: Git Presentation - Purple Scout AB Malmö

3. Distributed Work

55

Page 56: Git Presentation - Purple Scout AB Malmö

public repo

public repository

mas

ter

fix-s

egfa

ult

A

B

C

D

E

56

Page 57: Git Presentation - Purple Scout AB Malmö

clone

(over local filesystem, ssh://, git://, rsync://, ftp://etc)

57

public repo

mas

ter

fix-s

egfa

ult

A

B

C

D

E

local repo

mas

ter

fix-s

egfa

ult

A

B

C

D

E

$ git clone $URLInitialized empty Git repository in local/copy/.git/

Page 58: Git Presentation - Purple Scout AB Malmö

origin/

branch namespace

58

public repo

mas

ter

fix-s

egfa

ult

A

B

C

D

E

local repo

mas

ter

fix-s

egfa

ult

A

B

C

D

E

mas

ter

$ git branch --all*master origin/HEAD origin/fix-segfault origin/master

Page 59: Git Presentation - Purple Scout AB Malmö

public repo

origin/

public repository updated

59

mas

ter

fix-s

egfa

ult

A

B

C

D

E

local repo

mas

ter

fix-s

egfa

ult

A

B

C

D

E

F

mas

ter

Page 60: Git Presentation - Purple Scout AB Malmö

origin/

local repo

public repo

fetch update

60

mas

ter

fix-s

egfa

ult

A

B

C

D

Em

aste

r

fix-s

egfa

ult

A

B

C

D

E

$ git fetch originremote: Counting objects: 5, done.remote: Compressing objects: 100% (2/2), done.remote: Total 3 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (3/3), done.From /public/repo E..F fix-segfault -> origin/fix-segfault

F

F

mas

ter

Page 61: Git Presentation - Purple Scout AB Malmö

origin/

local repo

public repo

checkout tracking branch

61

mas

ter

fix-s

egfa

ult

A

B

C

D

Em

aste

r

fix-s

egfa

ult

A

B

C

D

E

$ git checkout -b work \ origin/fix-segfaultBranch work set up to track remote branch refs/remotes/origin/fix-segfault.Switched to a new branch "work"

F

F

mas

ter

wor

k

“topic branch” “tracking branches”

Page 62: Git Presentation - Purple Scout AB Malmö

origin/

local repo

public repo

commit topic branch

62

mas

ter

fix-s

egfa

ult

mas

ter

fix-s

egfa

ult

B

C

D

E

$ $EDITOR$ git commit -a -m "Another fix"[...]

F

mas

ter

wor

k

G

B

C

D

E

F

Page 63: Git Presentation - Purple Scout AB Malmö

origin/

local repo

public repo

push new commit

63

mas

ter

fix-s

egfa

ult

B

C

D

Em

aste

r

fix-s

egfa

ult

B

C

D

E

$ git push origin work:fix-segfaultTotal 0 (delta 0), reused 0 (delta 0)To /public/repo F..G work -> feature-X

F

F

mas

ter

wor

k

G

G

Page 64: Git Presentation - Purple Scout AB Malmö

origin/

local repo

public repo

public repository updated

64

mas

ter

fix-s

egfa

ult

C

D

E

mas

ter

fix-s

egfa

ult

B

C

D

E

F

F

mas

ter

wor

k

G

G

H

Page 65: Git Presentation - Purple Scout AB Malmö

origin/

local repo

public repo

fetch update

65

mas

ter

fix-s

egfa

ult

C

D

E

mas

ter

fix-s

egfa

ult

C

D

$ git fetch origin[...]From /public/repo G..H fix-segfault -> origin/fix-segfault

F

F

mas

ter

wor

k

G

G

H

H

Page 66: Git Presentation - Purple Scout AB Malmö

origin/

local repo

public repo

pull to merge update

66

mas

ter

fix-s

egfa

ult

C

D

E

mas

ter

fix-s

egfa

ult

C

D

$ git pull originUpdating G..HFast forward README | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)

F

F

mas

ter

wor

k

G

G

H

H

Page 67: Git Presentation - Purple Scout AB Malmö

• git clone: init a copy of a repository

• git fetch: sync remote repo R with local branches R/*

• git pull: fetch + merge

• git push <repo> [<local-branch>:<remote-branch>]

Basic multi-repo commands

67

Page 68: Git Presentation - Purple Scout AB Malmö

dev/ stable/

local repo

stable

Real-life layout

68

mas

ter

fix-s

egfa

ult

mas

ter

fix-s

egfa

ult

mas

ter

wor

k

pre-

mer

ge

mas

ter

anna

mas

ter

fix-s

egfa

ult

expe

rimen

tal

dev

mas

ter

bugg

y-cr

ap

test

Page 69: Git Presentation - Purple Scout AB Malmö

dev/ anna/ stable/

local repo

stable

Real-life layout

69

mas

ter

fix-s

egfa

ult

mas

ter

fix-s

egfa

ult

mas

ter

wor

k

pre-

mer

ge

mas

ter

anna

mas

ter

fix-s

egfa

ult

expe

rimen

tal

dev

mas

ter

bugg

y-cr

ap

test

git remote add anna $URL

Page 70: Git Presentation - Purple Scout AB Malmö

dev/ anna/ stable/

local repo

stable

Real-life layout

70

mas

ter

fix-s

egfa

ult

mas

ter

fix-s

egfa

ult

mas

ter

wor

k

pre-

mer

ge

expe

rimen

tal

fix-s

egfa

ult

mas

ter

mas

ter

anna

mas

ter

fix-s

egfa

ult

expe

rimen

tal

dev

mas

ter

bugg

y-cr

ap

test

git fetch anna

Page 71: Git Presentation - Purple Scout AB Malmö

dev/ anna/ stable/

local repo

stable

Real-life layout

71

mas

ter

fix-s

egfa

ult

mas

ter

fix-s

egfa

ult

mas

ter

wor

k

pre-

mer

ge

expe

rimen

tal

fix-s

egfa

ult

mas

ter

mas

ter

anna

mas

ter

fix-s

egfa

ult

expe

rimen

tal

dev

mas

ter

bugg

y-cr

ap

test

mine

git --bare init

“just a repo,not in .git/”

Page 72: Git Presentation - Purple Scout AB Malmö

dev/ anna/ stable/

local repo

stable

Real-life layout

72

mas

ter

fix-s

egfa

ult

mas

ter

fix-s

egfa

ult

mas

ter

wor

k

pre-

mer

ge

expe

rimen

tal

fix-s

egfa

ult

mas

ter

mas

ter

anna

mas

ter

fix-s

egfa

ult

expe

rimen

tal

dev

mas

ter

bugg

y-cr

ap

test

mine

git remote add mine $URL

Page 73: Git Presentation - Purple Scout AB Malmö

mine/ dev/ anna/ stable/

local repo x

stable

Real-life layout

73

mas

ter

fix-s

egfa

ult

mas

ter

mas

ter

wor

k

pre-

mer

ge

expe

rimen

tal

fix-s

egfa

ult

mas

ter

mas

ter

anna

mas

ter

fix-s

egfa

ult

expe

rimen

tal

dev

mas

ter

bugg

y-cr

ap

test

mine

mas

ter

pre-

mer

ge

mas

ter

pre-

mer

ge

“remotes”git push mine work:master pre-merge

Page 74: Git Presentation - Purple Scout AB Malmö

• Help track remote (i.e. other) repositories

• Pull from / push to them

• Use tracking branches like any branch (merge, rebase, diff, log, show, etc)

git remote

74

Page 75: Git Presentation - Purple Scout AB Malmö

e.g. XMMS2 trees

tru anders theefer

publictheeferpublic

tru

Joe Sixpack

stable develOfficial trees

Developer public trees

Local trees

developermaintainersuser 75

Page 76: Git Presentation - Purple Scout AB Malmö

e.g. XMMS2 trees

tru anders theefer

publictheeferpublic

tru

Joe Sixpack

stable devel

developermaintainersuser

olof

publicolof

contributer

olof’s server

76

Page 77: Git Presentation - Purple Scout AB Malmö

4. Advanced Features

77

Page 78: Git Presentation - Purple Scout AB Malmö

Between the working directory and the repository

78

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

WorkingDirectory

Repository

Page 79: Git Presentation - Purple Scout AB Malmö

The Index (or “Staging Area”)

79

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

WorkingDirectory

Repository

Index

Page 80: Git Presentation - Purple Scout AB Malmö

Committing using the index

80

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

WorkingDirectory

Repository

Index

git add/rm git commit

Page 81: Git Presentation - Purple Scout AB Malmö

Bypassing the Index

81

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

WorkingDirectory

Repository

Index

git commit -a

Page 82: Git Presentation - Purple Scout AB Malmö

new file

82

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO <- new file

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

WorkingDirectory

Repository

Index

Page 83: Git Presentation - Purple Scout AB Malmö

new file

83

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO <- new file

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.h

WorkingDirectory

Repository

Index

$ git status# On branch master# Changed but not updated:# (use "git add <file>..." to update what will be committed)## modified: test.c# modified: TODO#no changes added to commit (use "git add" and/or "git commit -a")

Page 84: Git Presentation - Purple Scout AB Malmö

new file: add to index

84

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO <- new file

WorkingDirectory

Repository

Index

git add TODO

Page 85: Git Presentation - Purple Scout AB Malmö

new file: add to index

85

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO <- new file

WorkingDirectory

Repository

Index

git add TODO

$ git status# On branch master# Changes to be committed:# (use "git reset HEAD <file>..." to unstage)## modified: TODO## Changed but not updated: [...]

Page 86: Git Presentation - Purple Scout AB Malmö

new file: commit index

86

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

git commit

deadbeef

Page 87: Git Presentation - Purple Scout AB Malmö

edited file

87

04f087b

mas

ter

IDEASMakefileREADME <- editedsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

deadbeef

Page 88: Git Presentation - Purple Scout AB Malmö

edited file: update index

88

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO

IDEASMakefileREADME <- editedsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

deadbeef

git add README

Page 89: Git Presentation - Purple Scout AB Malmö

edited file: commit index

89

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

git commit

deadbeef

f4ec56

Page 90: Git Presentation - Purple Scout AB Malmö

edited file

90

04f087b

mas

ter

IDEAS <- 3 hunksMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

deadbeef

f4ec56

Page 91: Git Presentation - Purple Scout AB Malmö

add selected hunks to index

91

04f087b

mas

ter

IDEAS <- 2 hunksMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO

IDEAS <- 1 hunkMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

deadbeef

f4ec56

git add -p IDEAS

(also see git add --interactive)

Page 92: Git Presentation - Purple Scout AB Malmö

add selected hunks to index

92

04f087b

mas

ter

IDEAS <- 2 hunksMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO

IDEAS <- 1 hunkMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

deadbeef

f4ec56

git add -p IDEAS

(also see git add --interactive)

$ git add -p src/xmms/testclient.c [...]@@ -44,7 +44,7 @@ int main (int argc, char **argv) fd = xmms_ipc_transport_fd_get (transport); - msg = xmms_ipc_msg_string_new (42, "korv!");+ msg = xmms_ipc_msg_string_new (42, "apan!"); i = 5; Stage this hunk [y/n/a/d/j/J/?]?

Page 93: Git Presentation - Purple Scout AB Malmö

diff ’ing options

93

04f087b

mas

ter

IDEAS <- 2 hunksMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.htest.c <- new fileTODO

IDEAS <- 1 hunkMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

deadbeef

f4ec56

git diff

git diff --cached

git diff HEAD

Page 94: Git Presentation - Purple Scout AB Malmö

Conflicts: marked in index

94

04f087b

mas

ter

IDEAS <- mergedMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

IDEAS <- conflictedMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

deadbeef

f4ec56

Page 95: Git Presentation - Purple Scout AB Malmö

Reset changes: 3 options

95

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

deadbeef

f4ec56

Page 96: Git Presentation - Purple Scout AB Malmö

reset --hard

96

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

deadbeefgit reset --hard deadbeef

reset the repository HEAD, the index and the working directory.

Warning: any uncommitted change is lost!

deadbeef

deadbeef

f4ec56

Page 97: Git Presentation - Purple Scout AB Malmö

reset --soft

97

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

deadbeefgit reset --soft deadbeef

only reset the repository HEAD.

f4ec56

f4ec56

f4ec56

Previous commit becomes “changes to be committed”.

Page 98: Git Presentation - Purple Scout AB Malmö

reset --mixed (default)

98

04f087b

mas

ter

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

IDEASMakefileREADMEsrc|-- Makefile|-- helloworld.c`-- helloworld.hTODO

WorkingDirectory

Repository

Index

deadbeefgit reset --mixed deadbeef

f4ec56

deadbeef

reset the repository HEAD and the index, but not the working directory.

Keep changes but not marked for commit.

f4ec56

Page 99: Git Presentation - Purple Scout AB Malmö

• “Invisible” area to prepare commits

• Interact with:- working directory (add, rm)- repository (reset, commit)

• Analyze state (status, diff)

• Can usually be bypassed (using -a)

• Warning:- Can commit files in inconsistent state (e.g. not tested)

Using the Index (or not)

99

Page 100: Git Presentation - Purple Scout AB Malmö

Git layers

100

Source: http://osteele.com/archives/2008/05/commit-policies

Page 101: Git Presentation - Purple Scout AB Malmö

• Changing the history = point to a new history

• Correct mistakes

• Cleanup messy iterative development

• Warning:- Changes commit ids- (Possibly) creates conflicts if when people merge the rewritten branch- Best used locally, not on an important tree

Rewrite history!

101

Page 102: Git Presentation - Purple Scout AB Malmö

Rewrite history! (1)

102

mas

ter

A

B

C

D

• git commit --amend

• “Replace the latest commit by the current state of the index”

• Allows to keep or edit the commit message

K

Page 103: Git Presentation - Purple Scout AB Malmö

Rewrite history! (1)

103

mas

ter

A

B

C

D

• git commit --amend

• “Replace the latest commit by the current state of the index”

• Allows to keep or edit the commit message

K

Page 104: Git Presentation - Purple Scout AB Malmö

• git rebase --interactive

• Rewrite a sequence of commits:- omit- squash with parent- edit

Rewrite history! (2)

104

E

F

mas

ter

A

B

C

D

Page 105: Git Presentation - Purple Scout AB Malmö

• git rebase --interactive

• Rewrite a sequence of commits:- omit- squash with parent- edit

Rewrite history! (2)

105

E

F

mas

ter

A

B

C

D

T (E+F)

B

S (D)

Page 106: Git Presentation - Purple Scout AB Malmö

• git filter-branch

• Rewrite history using functions/scripts

• Edit author infos, commit message, directories, commits, tags, etc.

• RTFM...

Rewrite history! (3)

106

Page 107: Git Presentation - Purple Scout AB Malmö

git bisect• Useful to find the commit that

introduces a problem

• git bisect start <bad> [<good>]

107m

aste

r

A

B

C

D

H HEAD

Page 108: Git Presentation - Purple Scout AB Malmö

git bisect• Useful to find the commit that

introduces a problem

• git bisect start <bad> [<good>]

• git bisect bag|good

108m

aste

r

A

B

C

D

H

HEAD

Page 109: Git Presentation - Purple Scout AB Malmö

git bisect• Useful to find the commit that

introduces a problem

• git bisect start <bad> [<good>]

• git bisect bag|good

109m

aste

r

A

B

C

D

H

HEAD

Page 110: Git Presentation - Purple Scout AB Malmö

git bisect• Useful to find the commit that

introduces a problem

• git bisect start <bad> [<good>]

• git bisect bag|good

110m

aste

r

A

B

C

D

H

HEAD

Page 111: Git Presentation - Purple Scout AB Malmö

git blame• git blame D <file>

111m

aste

r

A

B

C

D

H

HEAD

Page 112: Git Presentation - Purple Scout AB Malmö

git stash• git stash [save [message]]

• git stash apply

• git stash pop # apply and drop

• Can also preserve the state of the index

112m

aste

r

modifs

Page 113: Git Presentation - Purple Scout AB Malmö

git stash• git stash [save [message]]

• git stash apply

• git stash pop # apply and drop

• Can also preserve the state of the index

113m

aste

r

stas

h@{0

}

modifs

Page 114: Git Presentation - Purple Scout AB Malmö

git stash• git stash [save [message]]

• git stash apply

• git stash pop # apply and drop

• Can also preserve the state of the index

114m

aste

r

stas

h@{0

}

modifs

Page 115: Git Presentation - Purple Scout AB Malmö

git stash• git stash [save [message]]

• git stash apply

• git stash pop # apply and drop

• Can also preserve the state of the index

115

modifs

mas

ter

stas

h@{0

}

modifs

Page 116: Git Presentation - Purple Scout AB Malmö

git svn• Import an SVN repository into a local Git repository

• Use Git features (branches, merge, log, etc)

• Talk to SVN from Git

• Requires streamlined Git history

116

GitRepository

GitWorking

Copy

SVNRepository

:-) :-(

SVNWorking

Copy

git svn svnGit SVN

Page 117: Git Presentation - Purple Scout AB Malmö

And also...• Submodules

• Cherry-pick

• Shallow copies

• Bundles

• Reflog

• Git daemon

• Hooks

• etc...117

Page 118: Git Presentation - Purple Scout AB Malmö

Eclipse Git plugin

118

Source: http://chem-bla-ics.blogspot.com/2008/10/git-eclipse-integration.html

Page 119: Git Presentation - Purple Scout AB Malmö

egg-mode (emacs)

119Source: http://bogolisk.blogspot.com/

Page 120: Git Presentation - Purple Scout AB Malmö

Gitweb – Web repo browser

120

Page 121: Git Presentation - Purple Scout AB Malmö

Github – Git Social Network

121 Url: http://github.com/

Page 122: Git Presentation - Purple Scout AB Malmö

Reference links• Official site:

http://git-scm.com/

• man gittutorial / git <command> --help

• Randal Schwartz Google Tech Talk:http://www.youtube.com/watch?v=8dhZ9BXQgc4

• Linus Torvalds Google Tech Talk:http://www.youtube.com/watch?v=4XpnKHJAok8

• Why Git Is Better Than X?http://whygitisbetterthanx.com/

122

Page 123: Git Presentation - Purple Scout AB Malmö

5. Conclusion

123

Page 124: Git Presentation - Purple Scout AB Malmö

Conclusion

• Different approach to VCS

• Custom and flexible workflow

• Power vs. Learning curve

“Smart tool to manipulate versioned information and throw it around.”

124

Page 125: Git Presentation - Purple Scout AB Malmö

Questions?

http://git-scm.com/

125