Você está na página 1de 125

Git

Sbastien 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 Workow 3. Distributed Work 4. Advanced Features 5. Conclusion

1. Introduction

Version Control Systems


Record history Facilitate team work Expose source code Branch/merge development paths
4

Centralized VCS
Master
Repository
permissions branches

commit checkout update

Copies

Working Copy

Working Copy

Working Copy

Alice
5

Bob

Charlie

Distributed VCS
Remote
(public/private)
Repository Repository Repository

push pull

clone

Working Copy

Working Copy

Working Copy

Local

Repository

Repository

Repository

Alice

Bob

Charlie

Centralized vs Distributed

synchronous - unique reference - xed hierarchy centralized access control

asynchronous - copy = repository - loose hierarchy ofine: - history - commit - branches more powerful

simpler

Centralized vs Distributed

synchronous - unique reference - xed hierarchy centralized access control

asynchronous - copy = repository - loose hierarchy ofine: - history - commit - branches more powerful fun! ^_^

simpler trkig... ;_;


8

Git History
Linus Torvalds needed a replacement for
BitKeeper to maintain the Linux kernel
Distributed Scalable & Fast Integrity guarantee

Requirements:
Non-linear, custom workows Talk over HTTP, SSH, FTP, rsync,
email, etc

First prototype on April 7, 2005 Now used by many FOSS projects:


9

Linux kernel, Freedesktop (X.org), Ruby on Rails, XMMS2, Android, Gnome, GCC, 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 Workow Easy to Learn
Source: http://whygitisbetterthanx.com/
13

2. Concepts & Local Workow

14

Git Repository
repository = object database

.git/objects/

15

Git Repository
id(object) = SHA1(object content)

335e

a23f

6679 601a 5592 34fe 57bc cc39 3e93 2211 23a7 1432 1260 93a4 cc5e b3ca
16

3347

efa4 dd85

cca6

8e9b

1798 332e

5390

f943

eab4

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

Git Repository
content is stored in blob objects File
#include <stdio.h> 335e a23f 6679 601a 3347 efa4 dd85 8e9b 1798 332e f943 eab4
17

5592 34fe cca6

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

57bc

cc39

3e93 2211 5390

23a7

1432

1260 93a4 cc5e b3ca

git cat-le -p 93a4

Git Repository
structure is stored in tree objects Directory
040000 tree cc5e src 100644 blob 93a4 hw.c 335e a23f 6679 601a 3347 efa4 dd85 8e9b 1798 332e f943 eab4
18

5592 34fe cca6

57bc

cc39

3e93 2211 5390

23a7

1432

1260 93a4 cc5e b3ca

git cat-le -p 1260

Git Repository
history is stored in commit objects Authenticated hierarchical snapshots
tree 1260... parent f943... author Tom <tom@...> 1204666883 +0100 committer Max <max@...> 1204666883 +0100 Fixed a major bug in Hello World. 5390

335e

a23f

6679 601a 5592 34fe 57bc cc39 3e93 2211 23a7 1432 1260 93a4 cc5e b3ca
19

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

3347

efa4 dd85

cca6

8e9b

1798 332e

git cat-le -p 1432

f943

eab4

Git Repository
references are stored as tag objects (Signed) symbolic link
object a23f... type commit tag v1.0.7 tagger Jack <jack@...> 1136523576 -0800

335e

a23f

6679 601a 5592 34fe 57bc cc39 3e93 2211 23a7 1432 1260 93a4 cc5e b3ca
20

3347

efa4 dd85

cca6

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

8e9b

1798 332e

5390

git cat-le -p 335e

f943

eab4

feature-X

master

practical repository entry points symbolic links


335e

Git Repository
branches
point to a commit in the graph
git branch

a23f

6679 601a 5592 34fe 57bc cc39 3e93 2211 23a7 1432 1260 93a4 cc5e b3ca
21

tags
v1.0 alpha-3
point to an object (usually a commit)
git tag
8e9b 3347 efa4 dd85 1798 332e f943 eab4

cca6

5390

History Graph
feature-X
601a 3347 cc39 8e9b 1432 f943
22

HEAD

tags
v1.0 alpha-3

master
a23f

branches

Git workow
$ nd
. ./src ./src/Makele ./src/helloworld.c ./src/helloworld.h ./Makele ./IDEAS ./README

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
23

initialize
$ git init
Initialized empty Git repository in /path/example/.git/

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
24

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

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
25

add les
$ git add .

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
26

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

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
27

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

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
28

04f087b

master

hack
$ echo "Blah blah" >> README $ rm IDEAS

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h

<- edited

04f087b
29

master

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

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h

<- edited

04f087b
30

master

commit
$ git commit -a -m "More stuff"

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h


31

54eda77

04f087b

master

Created commit 54eda77: More stuff 1 les changed, 1 insertions(+), 0 deletions(-) delete mode 100644 IDEAS

view commit
$ git show
More stuff diff --git a/IDEAS b/IDEAS deleted le mode 100644 index e69de29..0000000 diff --git a/README b/README index e69de29..579c5b1 100644 --- a/README +++ b/README @@ -0,0 +1 @@ +Blah blah

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h


32

54eda77

04f087b

master

commit 54eda77a85b0e5d1891653a36ff08faf133df030 Author: Sebastien Cevey <seb@cine7.net> Date: Mon Apr 13 21:23:02 2009 +0200

view commit
$ git show --pretty=raw
commit 54eda77a85b0e5d1891653a36ff08faf133df030 tree 524f0cfe293064d18ac0c12a55baaf2b34d42e8d parent 04f087b23ce96c2b24f9f6b5f7cf437af33e19c6 author Sebastien Cevey <seb@cine7.net> 1239650582 +0200 committer Sebastien Cevey <seb@cine7.net> 1239650582 +0200 More stuff [...]

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h


33

54eda77

master
524f0c 04f087b

view history
$ git log
More stuff commit 04f087b23ce96c2b24f9f6b5f7cf437af33e19c6 Author: Sebastien Cevey <seb@cine7.net> Date: Mon Apr 13 21:11:58 2009 +0200 First import

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h


34

54eda77

04f087b

master

commit 54eda77a85b0e5d1891653a36ff08faf133df030 Author: Sebastien Cevey <seb@cine7.net> Date: Mon Apr 13 21:23:02 2009 +0200

hack again
$ echo "/* End of l */" >> src/helloworld.c

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h

54eda77

<- edited
04f087b
35

master

diff modifs
$ git diff
diff --git a/src/helloworld.c b/src/helloworld.c index e69de29..54a606d 100644 --- a/src/helloworld.c +++ b/src/helloworld.c @@ -0,0 +1 @@ +/* End of l */

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h

54eda77

<- edited
04f087b
36

master

commit
$ git commit -a -m Hasty commit

3b5ba16

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h


37

54eda77

04f087b

master

Created commit 3b5ba16: Hasty commit 1 les changed, 1 insertions(+), 0 deletions(-)

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

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

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h


38

54eda77

04f087b

master

rewind history
$ git reset HEAD^ # parent of HEAD
src/helloworld.c: locally modied

$ git diff
diff --git a/src/helloworld.c b/src/helloworld.c index e69de29..54a606d 100644 --- a/src/helloworld.c +++ b/src/helloworld.c @@ -0,0 +1 @@ +/* End of l */

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h

54eda77

<- edited
04f087b
39

master

new history
$ $EDITOR src/helloworld.c
Created commit 0657c63: Proper commit 1 les changed, 1 insertions(+), 0 deletions(-)

0657c63

Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h


40

54eda77

04f087b

master

$ git commit -a -m "Proper commit"

tag
$ git tag stable

stable
Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
41

0657c63

54eda77

04f087b

master

new branch
$ git branch feature-X 54eda77 $ git branch
feature-X * master

stable
Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
42

0657c63

54eda77

04f087b

feature-X

master

switch branch
$ git checkout feature-X $ git branch
* feature-X master

stable
Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
43

0657c63

54eda77

04f087b

feature-X

Switched to branch "feature-X"

master

commit branch
$ $EDITOR src/helloworld.c $ git commit -a -m "Work on X"
Created commit 6195849: Work on X 1 les changed, 1 insertions(+), 0 deletions(-)

stable
Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
44

0657c63 6195849 54eda77

04f087b

feature-X

master

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

20fbfdd

stable
Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
45

0657c63 02bbf10 54eda77

04f087b

feature-X

master

diff branches
$ git checkout master $ git diff feature-X
diff --git a/README b/README index 29a6a68..579c5b1 100644 --- a/README +++ b/README @@ -1,2 +1 @@ Blah blah -foo [...]

1e4cfe4

stable
Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
46

0657c63 02bbf10 54eda77

04f087b

feature-X

Switched to branch "master"

master

merge
$ git merge feature-X

69e105 1e4cfe4

stable
Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
47

0657c63 02bbf10 54eda77

04f087b

feature-X

master

Merge made by recursive. README | 1+ src/helloworld.h | 1 + 2 les changed, 2 insertions(+), 0 deletions(-)

view merge commit


$ git show
Merge branch 'feature-X'

69e105 1e4cfe4

stable
Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
48

0657c63 02bbf10 54eda77

04f087b

feature-X

master

commit 69e105f3d1991c34d74d079197d5f1b1917d3120 Merge: 0657c63... 20fbfdd... Author: Sebastien Cevey <seb@cine7.net> Date: Mon Apr 13 22:49:16 2009 +0200

undo
$ git reset --hard stable # undo last merge $ git checkout feature-X
Switched to branch "feature-X"

20fbfdd

stable
Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
49

0657c63 02bbf10 54eda77

04f087b

feature-X

HEAD is now at 0657c63 Proper commit

master

rebase
$ git rebase master $ 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

aeaf0a

2e309b

stable
Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
50

0657c63

54eda77

04f087b

feature-X

master

First, rewinding head to replay your work on top of it... Applying: Work on X Applying: Edit README for X

trivial merge
$ git checkout master $ git merge feature-X
Updating 0657c63..aeaf0a9 Fast forward README | 1+ src/helloworld.h | 1 + 2 les changed, 2 insertions(+), 0 deletions(-)

aeaf0a

2e309b

stable
Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h
51

0657c63

54eda77

04f087b

feature-X

Switched to branch "master"

master

git show-branch

52

gitk

53

gitk

54

3. Distributed Work

55

public repository
ste ma
D C B A E

x-

56

seg f

aul

public repo

clone
ste ma

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

D C E

ste

fau

lt

B A

x-

xseg

ma

$ git clone $URL


D C B A E

Initialized empty Git repository in local/ copy/.git/

local repo
57

seg f

aul

public repo

branch namespace
ste ma
D

origin/
ste ste fau r r lt xseg ma ma

C B A

xE

$ git branch --all


D C B A E

*master origin/HEAD origin/x-segfault origin/master

local repo
58

seg f

aul

public repo

public repository updated


ste ma
D

origin/
ste ste fau r r lt xseg ma ma

C B A

xF E

D C B A E

local repo
59

seg f

aul

public repo

fetch update
ste ma
D

origin/
ste ste fau r r lt xseg ma ma

C B A

xF E

$ git fetch origin


D C B A F E

local repo
60

remote: 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 x-segfault -> origin/x-segfault

seg f

aul

public repo

checkout tracking branch


ste ma

topic branch

tracking branches
origin/

D C

wo rk

ste

ste

fau

lt

B A

xF E

xseg

ma

ma

D C B A

F E

$ git checkout -b work \ origin/x-segfault


Branch work set up to track remote branch refs/remotes/origin/x-segfault. Switched to a new branch "work"

local repo
61

seg f

aul

public repo

commit topic branch


ste ma xF E

origin/
wo rk ste ste fau r r lt xseg ma ma

D C B

$ $EDITOR $ git commit -a -m "Another x"


[...]
D C B F E

local repo
62

seg f

aul

public repo

push new commit


ste ma xG F E

origin/
wo rk ste ste fau r r lt xseg ma ma

D C B

$ git push origin work:x-segfault


G D C B F E

Total 0 (delta 0), reused 0 (delta 0) To /public/repo F..G work -> feature-X

local repo
63

seg f

aul

public repo

public repository updated


ste ma xH G F E

origin/
wo rk ste ste fau r r lt
D C

D C B

local repo
64

xseg

ma

ma

G F E

seg f

aul

public repo

fetch update
ste ma xH G F E

origin/
wo rk ste ste fau r r lt
D C

xseg

ma

ma

$ git fetch origin


H G D C F

[...] From /public/repo G..H x-segfault -> origin/x-segfault

local repo
65

seg f

aul

public repo

pull to merge update


ste ma xH G F E

origin/
wo rk ste ste fau r r lt
D C

xseg

ma

ma

$ git pull origin


H G D C F

Updating G..H Fast forward README | 1 + 1 les changed, 1 insertions(+), 0 deletions(-)

local repo
66

seg f

aul

public repo

Basic multi-repo commands


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


67

ma ste r wo rk pre -m e rge

ma

dev/
ste r

ma

ste tes bug t gy-

dev

cra p

Real-life layout

68

ma

ste exp eri x-

r me nta l

anna

seg f

aul

stable/
ma ste xseg r fau lt

ma x-

ste seg f

local repo

stable

aul

ma ste r wo rk pre -m e rge

ma

dev/
ste r

ma

ste tes bug t gy-

dev

git remote add anna $URL

cra p

anna/

Real-life layout

69

ma

ste exp eri x-

r me nta l

anna

seg f

aul

stable/
ma ste xseg r fau lt

ma x-

ste seg f

local repo

stable

aul

ma ste r wo rk pre -m e rge

ma

dev/
ste r

ma

ste tes bug t gy-

dev

git fetch anna

ma ste r al fau lt exp eri me nt xseg

cra p

anna/

Real-life layout

70

ma

ste exp eri x-

r me nta l

anna

seg f

aul

stable/
ma ste xseg r fau lt

ma x-

ste seg f

local repo

stable

aul

just a repo, not in .git/

ma

git --bare init

ste r

wo rk

mine

pre -m e rge

ma

dev/
ste r

ma

ste tes bug t gy-

dev

ma ste r al fau lt exp eri me nt xseg

cra p

anna/

Real-life layout

71

ma

ste exp eri x-

r me nta l

anna

seg f

aul

stable/
ma ste xseg r fau lt

ma x-

ste seg f

local repo

stable

aul

ma ste r wo rk

mine

pre -m e rge

ma

dev/
ste r

ma

ste tes bug t gy-

git remote add mine $URL

dev

ma ste r al fau lt exp eri me nt xseg

cra p

anna/

Real-life layout

72

ma

ste exp eri x-

r me nta l

anna

seg f

aul

stable/
ma ste xseg r fau lt

ma x-

ste seg f

local repo

stable

aul

ma ste r ma ste r pre -m erg e wo rk

mine

pre -m e rge

mine/
r

ma ste pre -m e rge

ma

ste tes bug t gy-

dev

cra p

git push mine work:master pre-merge

Real-life layout

73

dev/
ste r

ma

ma

ste exp eri

r me nta l

anna/
ma exp eri ste r me nt xseg fau al lt

x-

anna

seg f

aul

remotes

ma x-

ste seg f

stable

local repo

aul

stable/
ma ste r

git remote
Help track remote (i.e. other) repositories Pull from / push to them Use tracking branches like any branch (merge, rebase,
diff, log, show, etc)

74

e.g. XMMS2 trees


Ofcial trees
stable devel

Developer public trees

public tru

public theefer

Local trees

Joe Sixpack

tru

anders

theefer

user

75

maintainers

developer

e.g. XMMS2 trees


stable devel

olofs server
public olof public theefer

public tru

olof

Joe Sixpack

tru

anders

theefer

contributer

user

76

maintainers

developer

4. Advanced Features

77

Between the working directory and the repository


master
04f087b
78

Working Directory
IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h

Repository

The Index (or Staging Area)


Index Working Directory
IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h

Repository
master
04f087b

79

Committing using the index


Index Working Directory
IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h

Repository
master
git commit
04f087b

git add/rm
80

Bypassing the Index


Index Working Directory
IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h

Repository
master
git commit -a
04f087b

81

new le
Index Working Directory
IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO <- new file
04f087b
82

master

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h

Repository

new le
Index Working Directory
IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO <- new file IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h

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

Repository
master
04f087b

83

new le: add to index


Index Working Directory
IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO <- new file

Repository
master
04f087b

git add TODO


84

new le: add to index


$ git status

Index Working Directory


IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO <- new file

# # # # # # #

On branch master Changes to be committed: (use "git reset HEAD <le>..." to unstage) modied: TODO Changed but not updated: [...]

Repository
master
04f087b

git add TODO


85

new le: commit index


Index Working Directory
IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

Repository
master
deadbeef

git commit
86

04f087b

edited le
Index Working Directory
IDEAS Makefile README <- edited src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

Repository
master
deadbeef 04f087b

87

edited le: update index


Index Working Directory
IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO IDEAS Makefile README <- edited src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

Repository
master
deadbeef 04f087b

git add README


88

edited le: commit index


Index Working Directory
IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

Repository
master
f4ec56 deadbeef

git commit
89

04f087b

edited le
Index Working Directory
IDEAS <- 3 hunks Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

Repository
master
f4ec56 deadbeef 04f087b

90

add selected hunks to index


Index Working Directory
IDEAS <- 2 hunks Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO IDEAS <- 1 hunk Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

Repository
master
f4ec56 deadbeef 04f087b

(also see git add --interactive)


git add -p IDEAS
91

add selected hunks to index


$ git add -p src/xmms/testclient.c
[...] @@ -44,7 +44,7 @@ int main (int argc, char **argv) IDEAS <- 1 hunk

Index

IDEAS <- 2 hunks Stage this hunk [y/n/a/d/j/J/?]? Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO

f4ec56

(also see git add --interactive)


git add -p IDEAS

deadbeef

04f087b
92

master

Working - + Directory

Makefile README fd = xmms_ipc_transport_fd_get (transport); src |-Makefile msg = xmms_ipc_msg_string_new (42, "korv!"); |-helloworld.c msg = xmms_ipc_msg_string_new (42, "apan!"); `-- helloworld.h TODO i = 5;

Repository

diffing options
Index Working Directory
IDEAS <- 2 hunks Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h test.c <- new file TODO IDEAS <- 1 hunk Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

Repository
git diff --cached

f4ec56

git diff
deadbeef

git diff HEAD


04f087b
93

master

Conicts: marked in index


Index Working Directory
IDEAS <- merged Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO IDEAS <- conflicted Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

Repository
master
f4ec56 deadbeef 04f087b

94

Reset changes: 3 options


Index Working Directory
IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

Repository
master
f4ec56 deadbeef 04f087b

95

reset --hard
reset the repository HEAD, the index and the working directory.
deadbeef

Index Repository
master
f4ec56 deadbeef 04f087b

deadbeef

Working Directory

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

git reset --hard deadbeef

Warning: any uncommitted change is lost!


96

reset --soft
only reset the repository HEAD.
f4ec56

Index Repository
master
f4ec56 deadbeef 04f087b

f4ec56

Working Directory

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

git reset --soft deadbeef

Previous commit becomes changes to be committed.


97

reset --mixed (default)


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

Index Repository
master
f4ec56 deadbeef 04f087b

f4ec56

Working Directory

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

IDEAS Makefile README src |-- Makefile |-- helloworld.c `-- helloworld.h TODO

git reset --mixed deadbeef

Keep changes but not marked for commit.


98

Using the Index (or not)


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:
99

- Can commit les in inconsistent state (e.g. not tested)

Git layers

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

Rewrite history!
Changing the history = point to a new history Correct mistakes Cleanup messy iterative development Warning:
- Changes commit ids - (Possibly) creates conicts if when people merge the rewritten branch - Best used locally, not on an important tree
101

Rewrite history! (1)


git commit --amend Replace the latest commit by
Allows to keep or edit the commit message the current state of the index
K
D C B A

102

ma

ste

Rewrite history! (1)


git commit --amend Replace the latest commit by
Allows to keep or edit the commit message the current state of the index
D C B A

ma
K

103

ste

Rewrite history! (2)


git rebase --interactive Rewrite a sequence of commits:
- omit - squash with parent - edit
ma
F E D C B A

104

ste

Rewrite history! (2)


git rebase --interactive Rewrite a sequence of commits:
- omit - squash with parent - edit
ma
F E D C B A B S (D)
T (E+F)

105

ste

Rewrite history! (3)


git lter-branch Rewrite history using functions/scripts Edit author infos, commit message,
directories, commits, tags, etc.

RTFM...

106

git bisect
git bisect start <bad> [<good>]
ma ste

Useful to nd the commit that


introduces a problem
H D C B A

HEAD

107

git bisect
git bisect start <bad> [<good>] git bisect bag|good
H D C B A

ma

ste

Useful to nd the commit that


introduces a problem

HEAD

108

git bisect
git bisect start <bad> [<good>] git bisect bag|good
H D C B A

ma

ste

Useful to nd the commit that


introduces a problem

HEAD

109

git bisect
git bisect start <bad> [<good>] git bisect bag|good
H D C B A

ma

ste

Useful to nd the commit that


introduces a problem

HEAD

110

git blame
H D C B A

ma

ste

git blame D <le>

HEAD

111

git stash
git stash [save [message]] git stash apply git stash pop # apply and drop
modifs
ma ste r

Can also preserve the state of


the index
112

git stash
git stash [save [message]] git stash apply git stash pop # apply and drop Can also preserve the state of
the index
113

ste

modifs

sta sh@

ma

{0}

git stash
git stash [save [message]] git stash apply git stash pop # apply and drop Can also preserve the state of
the index
114

ste

modifs

sta sh@

ma

{0}

git stash
git stash [save [message]] git stash apply git stash pop # apply and drop
modifs
ste sta sh@ ma {0} r

modifs

Can also preserve the state of


the index
115

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
:-)
Git
Git Working Copy

:-(
SVN

git svn

Git Repository
116

SVN Repository

svn

SVN Working Copy

Submodules Cherry-pick Shallow copies Bundles Reog Git daemon Hooks etc...

And also...

117

Eclipse Git plugin

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

egg-mode (emacs)

119

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

Gitweb Web repo browser

120

Github Git Social Network

121

Url: http://github.com/

Reference links

Ofcial 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 exible workow Power vs. Learning curve
Smart tool to manipulate versioned information and throw it around.

124

Questions?
http://git-scm.com/

125

Você também pode gostar