Git Presentation - Purple Scout AB Malmö

Post on 12-May-2015

3.241 views 2 download

Tags:

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ö

GitSébastien Cevey <seb@cine7.net>

http://bytes.inso.cc/

Purple Scout presentation (April 17, 2009)

Swiss Federal Institute of Technology, Lausanne

XMMS2 Music Player Playlouder MSP

Outline

1. Introduction

2. Concepts & Local Workflow

3. Distributed Work

4. Advanced Features

5. Conclusion

2

1. Introduction

3

Version Control Systems

• Record history

• Facilitate team work

• Expose source code

• Branch/merge development paths

4

Centralized VCS

Repository

Working Copy

Working Copy

Working Copy

Alice Bob Charlie

Master

Copies

permissionsbranches

checkout

commit

update

5

Distributed VCS

Repository

Working Copy

Repository

Working Copy

Repository

Working Copy

Alice Bob Charlie

RepositoryRepository

Repository

clone

push

pull

6

Remote(public/private)

Local

Centralized vs Distributed• asynchronous

- copy = repository- loose hierarchy

• offline:- history- commit- branches

• more powerful

• synchronous- unique reference- fixed hierarchy

• centralized access control

• simpler

7

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

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

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

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

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

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

2. Concepts&

Local Workflow

14

Git Repositoryrepository = object database

15.git/objects/

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

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

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

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

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

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

History Graph

a23f

3347

8e9b

f943

601a

cc39

1432

v1.0

alpha-3

mas

ter

feat

ure-

Xbranches

tags

22

HEAD

Git workflow

23

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

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

initialize

24

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

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

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

add files

26

$ git add .

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

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

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

hack

29

$ echo "Blah blah" >> README

$ rm IDEAS

04f087b

mas

ter

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

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

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

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

view commit

32

$ git show commit 54eda77a85b0e5d1891653a36ff08faf133df030Author: Sebastien Cevey <seb@cine7.net>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

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

view commit

33

$ git show --pretty=rawcommit 54eda77a85b0e5d1891653a36ff08faf133df030tree 524f0cfe293064d18ac0c12a55baaf2b34d42e8dparent 04f087b23ce96c2b24f9f6b5f7cf437af33e19c6author Sebastien Cevey <seb@cine7.net> 1239650582 +0200committer Sebastien Cevey <seb@cine7.net> 1239650582 +0200

More stuff

[...]

04f087b

mas

ter

54eda77 524f0c

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

view history

34

$ git logcommit 54eda77a85b0e5d1891653a36ff08faf133df030Author: Sebastien Cevey <seb@cine7.net>Date: Mon Apr 13 21:23:02 2009 +0200

More stuff

commit 04f087b23ce96c2b24f9f6b5f7cf437af33e19c6Author: Sebastien Cevey <seb@cine7.net>Date: Mon Apr 13 21:11:58 2009 +0200

First import

04f087b

mas

ter

54eda77

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

hack again

35

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

04f087b

mas

ter

54eda77

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

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

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

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

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

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

tag

41

$ git tag stable

04f087b

mas

ter

54eda77

0657c63stable

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

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

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

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

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

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

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

view merge commit

48

$ git showcommit 69e105f3d1991c34d74d079197d5f1b1917d3120Merge: 0657c63... 20fbfdd...Author: Sebastien Cevey <seb@cine7.net>Date: Mon Apr 13 22:49:16 2009 +0200

Merge branch 'feature-X'

04f087b

mas

ter

54eda77

0657c63stable

feat

ure-

X

1e4cfe4

02bbf10

69e105

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

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

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

git show-branch

52

gitk

53

gitk

54

3. Distributed Work

55

public repo

public repository

mas

ter

fix-s

egfa

ult

A

B

C

D

E

56

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/

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

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

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

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”

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

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

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

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

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

• 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

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

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

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

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/”

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

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

• 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

e.g. XMMS2 trees

tru anders theefer

publictheeferpublic

tru

Joe Sixpack

stable develOfficial trees

Developer public trees

Local trees

developermaintainersuser 75

e.g. XMMS2 trees

tru anders theefer

publictheeferpublic

tru

Joe Sixpack

stable devel

developermaintainersuser

olof

publicolof

contributer

olof’s server

76

4. Advanced Features

77

Between the working directory and the repository

78

04f087b

mas

ter

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

WorkingDirectory

Repository

The Index (or “Staging Area”)

79

04f087b

mas

ter

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

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

WorkingDirectory

Repository

Index

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

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

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

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")

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

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: [...]

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

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

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

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

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

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)

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/?]?

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

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

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

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

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

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

• “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

Git layers

100

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

• 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

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

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

• 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

• 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)

• git filter-branch

• Rewrite history using functions/scripts

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

• RTFM...

Rewrite history! (3)

106

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

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

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

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

git blame• git blame D <file>

111m

aste

r

A

B

C

D

H

HEAD

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

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

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

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

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

And also...• Submodules

• Cherry-pick

• Shallow copies

• Bundles

• Reflog

• Git daemon

• Hooks

• etc...117

Eclipse Git plugin

118

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

egg-mode (emacs)

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

Gitweb – Web repo browser

120

Github – Git Social Network

121 Url: http://github.com/

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

5. Conclusion

123

Conclusion

• Different approach to VCS

• Custom and flexible workflow

• Power vs. Learning curve

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

124

Questions?

http://git-scm.com/

125