Effective Git - EclipseCon 2011 tutorial

57
Effective Git Tutorial EclipseCon 2011 http://eclipse.org/egit http://code.google.com/p/gerrit Matthias Sohn (SAP) + = Stefan Lay (SAP) Chris Aniszczyk(Redha t) Shawn Pearce (Google)

description

Effective Git - EclipseCon 2011 tutorialTutorial slides, the corresponding tutorial exercises are published here https://docs.google.com/leaf?id=0B4F_gjXVrHZVMzE5MWNmNjktMDcxMS00NDM3LWI4NDQtYmE1YzZkM2RkY2Rl&hl=en

Transcript of Effective Git - EclipseCon 2011 tutorial

Page 1: Effective Git - EclipseCon 2011 tutorial

Effective GitTutorial EclipseCon 2011

http://eclipse.org/egithttp://code.google.com/p/gerrit

Matthias Sohn(SAP)

+ =

Stefan Lay(SAP)

Chris Aniszczyk(Redhat)

Shawn Pearce(Google)

Page 2: Effective Git - EclipseCon 2011 tutorial

Git… a distributed revision control system built by the Linux project to facilitate code review

Distributed means no central repository• No central authority!• Easy offline usage• Easy to branch a project• Protected against manipulation by cryptographic hashes

Really good at merging• Coordination only needed "after the fact”• Easier to rejoin (or refresh) branches

Structured around commits (i.e. patches)• Tools for identifying problem commits (git bisect)• Tools for restructuring branches w/ specific commits

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 3: Effective Git - EclipseCon 2011 tutorial

Git at Eclipse

Eclipse defined a roadmap to move to GitCVS has been deprecated

EGit is an Eclipse Team provider for Git• http://www.eclipse.org/egit/

JGit is a lightweight Java library implementing Git • http://www.eclipse.org/jgit/

The goal is to build an Eclipse community around Git.

EGit/JGit are still beta and we want to establish a feedback loop to improve the tooling.

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 4: Effective Git - EclipseCon 2011 tutorial

Modern Code Review – What is it ? When one developer writes code, another

developer is asked to review that code

A careful line-by-line critique

Happens in a non-threatening context

Goal is cooperation, not fault-finding

Integral part of coding process

Otherwise this will happen: Debugging someone else's broken code – Involuntary code review: Not so good; emotions may flare

Guido van Rossum [1]

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

[1] http://code.google.com/p/rietveld/downloads/detail?name=Mondrian2006.pdf

Page 5: Effective Git - EclipseCon 2011 tutorial

Code Review – Benefits

Four eyes catch more bugso Catch bugs early to save hours of debugging

Mentoring of new developers / contributorso Learn from mistakes without breaking stuff

Establish trust relationships o Prepare for more delegation

Good alternative to pair programmingo asynchronous and across locations

Coding standardso Keep overall readability & code quality high

Guido van Rossum [1]

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

[1] http://code.google.com/p/rietveld/downloads/detail?name=Mondrian2006.pdf

Page 6: Effective Git - EclipseCon 2011 tutorial

Developer PC

Gerrit

git gitgitgit

Developer PC

gitgit

Hudson

- clone repository - fetch / push changes

- verify proposed changes- continuous integration builds

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 7: Effective Git - EclipseCon 2011 tutorial

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Developer PC

gitgit

Gerrit

git gitgitgit

push improved change 10

Developer PC

gitgit

fetch change 23 to try it

master

change 12

change 10

change 23

submit accepted change 12

fetch master to get updates

Page 8: Effective Git - EclipseCon 2011 tutorial

Git Configuration

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 9: Effective Git - EclipseCon 2011 tutorial

Git Concepts – config files

git config –lgit config –e--system--global

System<gitinst>/etc/gitconfig

Global~/.gitconfig

Repository Specific.git/config

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 10: Effective Git - EclipseCon 2011 tutorial

Basic Concepts

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 11: Effective Git - EclipseCon 2011 tutorial

Making Changes

Structure in the file system

•One working tree per repo•.git folder is the Git repo•Files/folders under the parent of .git are the working tree

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 12: Effective Git - EclipseCon 2011 tutorial

Making Changes

Checkout: populate working tree with the commit you want to start working from

omost of the time you will checkout a brancho checkout the commit pointed to by the branch (aka: tip of the branch)oper file checkout means revert !

Calculator

.git

<working tree>

git checkout master

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 13: Effective Git - EclipseCon 2011 tutorial

Making Changes

Just start doing your changesomodify, add, delete files

Tell Git which changes you intend to commit

git add

EGit helps by auto-detecting what you changed

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 14: Effective Git - EclipseCon 2011 tutorial

Committing Changes

git commit

•Provide a commit message• First line is header• separated by a blank line follows the body• last paragraph is for meta data in key: value format

•commit represents a version of the complete repository•commits are identified by a globally unique ID (SHA1)•If two Git repos both contain a commit with the same ID then the content in these two commits is identical

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 15: Effective Git - EclipseCon 2011 tutorial

Commits

• Commit historyo B is successor of Ao C is successor of B

A

B

C

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 16: Effective Git - EclipseCon 2011 tutorial

Branches

• Branch is a named pointer to a commit• Commit command moves the pointer

A

B

Cmaster

A

B

C

master D

commit

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 17: Effective Git - EclipseCon 2011 tutorial

Branches

• The (branch) pointer can also be moved “manually” to any commit

git reset

A

B

C

master D

A

B

C

master

Dreset B

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 18: Effective Git - EclipseCon 2011 tutorial

Branches

• What happens on next git commit ?• C and D continue to exist but they are not in the

history of the master branch

A

B

C

master

D

A

B

Cmaster

D

E

commit

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 19: Effective Git - EclipseCon 2011 tutorial

Branches

• Usually there are many branches in a Git repository

• Branches can also be deleted

A

B

Corigin/master

D

E

feature 1Fbugfix 15

G feature 2

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 20: Effective Git - EclipseCon 2011 tutorial

HEAD

• HEAD – pointer to a branch• Means:

“Current Branch” – the branch which you have checked out

A

B

C

D

E

feature 1Fbugfix 15 HEAD

origin/master

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 21: Effective Git - EclipseCon 2011 tutorial

Cloning & Fetching

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 22: Effective Git - EclipseCon 2011 tutorial

Clone Remote Repositorygit clone <remote-repo>

Git is a distributed versioning systemocloned repo gets local name “origin” (by default)

A

B

C

master D

E

F

HEAD

feature-1

A

B

C

master D

E

F

HEAD

origin/feature-1

origin/master

Remote “origin” Local clone

clone

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 23: Effective Git - EclipseCon 2011 tutorial

Clone Remote Repository

Remote tracking branches, full names: oremotes/origin/masteroremotes/origin/feature-1

A

B

C

master D

E

F

HEAD

origin/feature-1

origin/master

Local clone

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 24: Effective Git - EclipseCon 2011 tutorial

Remote Tracking Branches

Just like any other branch, but read-onlyopossible: git checkout origin/feature1oHowever, HEAD gets detached!

A

B

C

master D

E

F

HEAD

origin/feature-1

origin/master

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 25: Effective Git - EclipseCon 2011 tutorial

Fetch

git fetch will update all remote tracking branches to reflect the changes done in the “origin” repo

A

B

C

master D

E

F

HEAD feature-1

Remote “origin”

G

feature-1

A

B

C

master D

E

F

HEADorigin/feature-1

origin/master

Local clone

G

origin/feature-1

fetch

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 26: Effective Git - EclipseCon 2011 tutorial

Fetch

• Always safe to do• Updates only remote tracking branches• Does never change local branches

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 27: Effective Git - EclipseCon 2011 tutorial

Merge & Rebase

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 28: Effective Git - EclipseCon 2011 tutorial

Mergegit merge feature-1

•will replay all changes done in feature-1 since it diverged from 1.0 (E and F)

A

B

C

D

E

F

HEAD

feature-1

A

B

C

1.0

D

E

F

HEAD

feature-1

G

1

2

1 2

1.0

merge feature-1

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 29: Effective Git - EclipseCon 2011 tutorial

Merge

git merge feature-1

Easy Case - Fast Forwardono new commit, just move the pointer

A

B

EHEAD

feature-1

A

B

EHEAD feature-11.0

1.0

merge feature-1

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 30: Effective Git - EclipseCon 2011 tutorial

Cherry Pickgit cherry-pick F

•applies changes introduced by F, means delta-2•no merge relation

A

B

C

1.0 D

E

F

HEAD

feature-1

1

2

A

B

C

1.0

D

E

F

HEAD

feature-1

G

2

cherry-pick F

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 31: Effective Git - EclipseCon 2011 tutorial

Rebase

git rebase 1.0

•alternative to merge - keeping history linear•after rebase fast-forward possible!

A

B

C

1.0 D

E

F

HEAD

feature-1

A

B

C

1.0 D

E’

F’

HEAD

feature-1

1

2

1

2

rebase 1.0

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 32: Effective Git - EclipseCon 2011 tutorial

Pushing

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 33: Effective Git - EclipseCon 2011 tutorial

Push

git push origin HEAD:master

from local to remote repositoryomore precisely: from a local to a remote branch

A

B origin/master

C

Local repo

feature-1 HEAD

A

B

C master

Remote “origin” repo

push

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 34: Effective Git - EclipseCon 2011 tutorial

Push

• Which commits get pushed?

• ALL commits from the local branch not available in the remote branch

A

B origin/master

C

Local repo

feature-1 HEADD

A

B

C

masterD

Remote “origin” repo

push

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 35: Effective Git - EclipseCon 2011 tutorial

Push

Remote branch has changed

•git push will fail because fast forward is not possible

A

B origin/master

C

Local repo

feature-1 HEAD

A

B

D master

Remote “origin” repopush

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 36: Effective Git - EclipseCon 2011 tutorial

Push

Possibility One

pull (fetch + merge), push

A

B origin/master

C

Local repo

feature-1 HEAD

A

B

D

master

Remote “origin” repo

D

E

C

E

push

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 37: Effective Git - EclipseCon 2011 tutorial

Push

Possibility Two

fetch, rebase, push

A

B origin/master

Local repo

feature-1 HEAD

D

C’

A

B

D

master

Remote “origin” repo

C’

push

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 38: Effective Git - EclipseCon 2011 tutorial

Push

Which graph do you like more ?

A

B

D

master

Remote “origin” repo

C

E

A

B

D

masterC’

Remote “origin” repo

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 39: Effective Git - EclipseCon 2011 tutorial

Gerrit Concepts

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 40: Effective Git - EclipseCon 2011 tutorial

Push

Push to Gerrit is the same like push to Git

owith one Gerrit speciality: refs/for in the target branch name

Compare:

oPush to Git:git push origin HEAD:master

oPush to Gerrit:git push origin HEAD:refs/for/master

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 41: Effective Git - EclipseCon 2011 tutorial

Push

It seems like every push to Gerrit goes to the same branch refs/for/master

However, Gerrit tricks your git client:

o it creates a new branch for the commit(s) you push

o and creates a new open Gerrit change for each pushed commit

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 42: Effective Git - EclipseCon 2011 tutorial

Push

git push origin HEAD:refs/for/master

A

B origin/master

C

Local repo

feature-1 HEAD

A

B

C

master

Gerrit hosted “origin” repo

refs/changes/63/363/1

Gerrit DB - Open Changes:…{Change-ID = 1234,Patch-Set-1 = refs/changes/63/363/1}…

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 43: Effective Git - EclipseCon 2011 tutorial

Push

What if feature branch has 2 commits ?

Remember Git semantics for Push?oALL commits from the local branch not available in the remote branch 2 changes in Gerrit !

A

B origin/master

C

Local repo

feature-1 HEADD

A

B

C

master

Gerrit hosted “origin” repo

refs/changes/63/363/1, Change 1234

D refs/changes/64/364/1, Change 1235

depends on

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 44: Effective Git - EclipseCon 2011 tutorial

Changes

Change consists of

oChange ID (important!)ometadata (owner, project, etc..)oone or more patch-setsocommentsovotes

Patch Set represents a Git Commit

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 45: Effective Git - EclipseCon 2011 tutorial

New Change vs New Patch Set

• How does Gerrit know whether to create a new Change or a new Patch Set for an existing change?

• It looks at the Commit Message and searches for String “Change-Id: <ISHA1>” in the last paragraph of the commit message

• If found it will create a new patchset for this changes, otherwise it will create a new change

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 46: Effective Git - EclipseCon 2011 tutorial

New Change vs New Patch Set

• Example Commit Message with Change-Id:

Make lib.Repository abstract and lib.FileRepository its implementation

To support other storage models other than just the local filesystem,…will rename it into storage.file.FileRepository, but to do that weneed to also move a number of other related class, which we aren'tquite ready to do.

Change-Id: I1bd54ea0500337799a8e792874c272eb14d555f7Signed-off-by: Joe Developer <[email protected]>

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 47: Effective Git - EclipseCon 2011 tutorial

Push New Patchset

No dependencies between Patchsetsocan’t push a successor commit as the next patchsetocommit D can’t be Patch Set 2 of change 1234

A

B origin/master

C

Local repo

feature-1 HEADD

Change 1234, Patch Set 1

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 48: Effective Git - EclipseCon 2011 tutorial

Push New Patchset• If you pushed C and need to replace it by a new commit as

a new patchset use

git commit –amend

• with –amend option the new commit replaces the current instead of becoming successor

A

B origin/master

C

Local repo

feature-1 HEAD

Change 1234, Patch Set 1

A

B origin/master

C

Local repo

feature-1 HEADD

Change 1234, Patch Set 1

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 49: Effective Git - EclipseCon 2011 tutorial

Push New Patch Set

A Common Mistakeoauthor of the Patch Set 1 is not available and somebody else needs to continue and provide Patch Set 2

ouse git pull to get the Patch Set 1 into a local branchoFix issues in commitoPush (including the same Change-Id)

oGerrit rejects!

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 50: Effective Git - EclipseCon 2011 tutorial

Push New Patchset

A Common Mistake

git pull origin refs/changes/66/366/1

•D is successor of C and cannot be Patch Set 2

A

B

Local repo

HEAD

A

B

C

Local repo

Change 1234, Patchset 1

master

D HEADmaster

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 51: Effective Git - EclipseCon 2011 tutorial

Push New Patch Set

The right way:

fetch (don’t pull)

ocreate a new branch based on the fetched patchset 1

ofix the issue

commit –amendpush

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 52: Effective Git - EclipseCon 2011 tutorial

Push New Patchset

The right way:

ocommit D is not successor of CoD can become patchset 2

A

B

C

Local repo

Change 1234, Patch Set 1

D HEAD

feature-1

A

B

C

Local repo

Change 1234, Patch Set 1

HEAD

feature-1

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 53: Effective Git - EclipseCon 2011 tutorial

Review and Vote

• Explore the Gerrit Web UI

• Click “Review” to vote or comment• Voting the lowest mark means Veto• Highest marks in all voting categories needed for

change to be merged

• “Submit” to merge accepted change into master

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 54: Effective Git - EclipseCon 2011 tutorial

Fetch Open Change Locally

• Don’t forget that every patchset is a commit• Use git fetch to fetch it locally if you want to

play with it• Hudson Gerrit plugin does just that !• Gerrit creates fetch command for you

• In EGit use “Fetch from Gerrit…”

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 55: Effective Git - EclipseCon 2011 tutorial

Best Practices

Create local branch for each feature or bugfix you work on

obranch name tells you what your intention was

oless likely you will mix two features in the same branch

oyou can have many feature branches at a timeoand switch between them

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 56: Effective Git - EclipseCon 2011 tutorial

Best Practices

Push finished features onlyowho wants to review non finished feature ?owho wants non-finished features in history ?

Push complete feature as one commitoeven if you created many commits squash them into one before pushootherwise you create one change in Gerrit per each commit !ouse multiple changes if feature can be split into smaller logical units and use last change to switch on the new feature

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce

Page 57: Effective Git - EclipseCon 2011 tutorial

Best Practices

Write good commit messageoFirst line is summaryoEmpty line after summary and between paragraphsoExplain WHY you did the change not WHAT you did (this is visible from the commit)

Prefer many small changes to one bigostill each small change must be logically complete

Copyright © M. Sohn, S. Lay, S. Zivkov, C. Halstrick, C. Aniszczyk, S. Pearce