SlideShare a Scribd company logo
Effective Git http://eclipse.org/egit http://code.google.com/p/ gerrit Matthias Sohn (SAP) + = Stefan Lay (SAP) Chris Aniszczyk(Redhat) Shawn Pearce (Google)
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
Git at Eclipse Eclipse defined a roadmap to move to Git CVS 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.
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] [1]  http://code.google.com/p/rietveld/downloads/detail?name=Mondrian2006.pdf
Code Review – Benefits Four eyes catch more bugs Catch bugs early to save hours of debugging Mentoring of new developers / contributors Learn from mistakes without breaking stuff Establish trust relationships  Prepare for more delegation Good alternative to pair programming asynchronous and across locations Coding standards Keep overall readability & code quality high Guido van Rossum [1] [1]  http://code.google.com/p/rietveld/downloads/detail?name =Mondrian2006.pdf
Developer PC Gerrit git git git git Developer PC git git Hudson - clone repository  - fetch / push changes - verify proposed changes - continuous integration builds
Developer PC git git Gerrit git git git git push improved  change 10 Developer PC git git fetch change 23  to try it master change 12 change 10 change 23 submit accepted  change 12 fetch master to get updates
Git Configuration
Git Concepts – config files git config –l git config –e --system --global System <gitinst>/etc/gitconfig Global ~/.gitconfig Repository Specific .git/config
Basic Concepts
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
Making Changes Checkout:  populate working tree with the commit you want to start working from most of the time you will checkout a branch    checkout the commit pointed to by the branch  (aka: tip of the branch) per file checkout means revert ! Calculator .git <working tree> git checkout master
Making Changes Just start doing your changes modify, add, delete files Tell Git which changes you intend to commit git add EGit helps by auto-detecting what you changed
Commiting 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
Commits Commit history B is successor of A C is successor of B A B C
Branches Branch is a named pointer to a commit Commit command moves the pointer A B C master A B C master D commit
Branches The (branch) pointer can also be moved  “manually” to any commit git reset A B C master D A B C master D reset B
Branches What happens on next  git commit  ? C and D continue to exist but they are not in the history or the master branch A B C master D A B C master D E commit
Branches Usually there are many branches in  a Git repository Branches can also be deleted A B C origin/master D E feature 1 F bugfix 15 G feature 2
HEAD HEAD – pointer to a branch Means:  “Current Branch” – the branch which you have checked out A B C D E feature 1 F bugfix 15 HEAD origin/master
Cloning & Fetching
Clone Remote Repository Git is a distributed versioning system git clone <remote-repo> cloned 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
Clone Remote Repository Remote tracking branches, full names:  remotes/origin/master remotes/origin/feature-1 A B C master D E F HEAD origin/feature-1 origin/master Local clone
Remote Tracking Branches Just like any other branch, but read-only possible: git checkout origin/feature1 However, HEAD gets detached! A B C master D E F HEAD origin/feature-1 origin/master
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 HEAD origin/feature-1 origin/master Local clone G origin/feature-1 fetch
Fetch Always safe to do Updates only remote tracking branches Does never change local branches
Merge & Rebase
Merge git 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
Merge Easy Case - Fast Forward git merge feature-1 no new commit, just move the pointer A B E HEAD feature-1 A B E HEAD feature-1 1.0 1.0 merge feature-1
Git Concepts – Cherry Pick git 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
Git Concepts - Rebase Alternative to Merge - Keeping history linear git rebase 1.0 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
Pushing
Push git push origin HEAD:master From local to remote repository more 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
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 HEAD D A B C master D Remote  “origin” repo push
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” repo push
Git Concepts - 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
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
Push Which graph do you like more ? A B D master Remote  “origin” repo C E A B D master C ’ Remote  “origin” repo
Gerrit Concepts
Push Push to Gerrit is the same like push to Git with one Gerrit speciality:  refs/for   in the target branch name Compare: Push to Git: git push origin HEAD:master Push to Gerrit: git push origin HEAD: refs/for /master
Push It seems like every push to Gerrit goes to the same branch  refs/for/master However, Gerrit tricks your git client: it creates a new branch for the commit(s) you push  and creates a new open Gerrit change containing the pushed commit
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 } …
Push What if feature branch has 2 commits ? Remember Git semantics for Push? ALL commits from the local branch not available in the remote branch    2 changes in Gerrit ! A B origin/master C Local repo feature-1 HEAD D 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
Changes Change consists of Change ID (important!) metadata (owner, project, etc..) one or more patch-sets comments votes Patch Set represents a Git Commit
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
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 we need to also move a number of other related class, which we aren't quite ready to do. Change-Id:  I1bd54ea0500337799a8e792874c272eb14d555f7 Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Push New Patchset No  dependencies between Patchsets can ’t push a successor commit as the next patchset commit D can ’t be Patch Set 2 of change 1234 A B origin/master C Local repo feature-1 HEAD D Change 1234, Patch Set 1
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 HEAD D Change 1234, Patch Set 1
Push New Patch Set A Common Mistake author of the Patch Set 1 is not available and somebody else needs to continue and provide Patch Set 2 use  git pull  to get the Patch Set 1 into a local branch Fix issues in commit Push (including the same Change-Id) Gerrit rejects!
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 HEAD master
Push New Patch Set The right way: fetch  (don ’t pull) create a new branch based on the fetched patchset 1 fix the issue commit –amend push
Push New Patchset The right way: commit D is not successor of C D 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
Review and Vote Show in Gerrit Web UI Voting the lowest mark means Veto Highest marks in all voting categories needed for change to be merged
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 show in Web UI
Best Practices Create local branch for each feature or bugfix you work on branch name tells you what your intention was less likely you will mix two features in the same branch you can have many feature branches at a time and switch between them
Best Practices Push finished features only who wants to review non finished feature ? who wants non-finished features in history ? Push complete feature as one commit even if you created many commits squash them into one before push otherwise you create one change in Gerrit per each commit ! use multiple changes if feature can be split into smaller logical units and use last change to switch on the new feature
Best Practices Write good commit message First line is summary Empty line between paragraphs Explain WHY you did the change not WHAT you did (this is visible from the commit) Prefer many small changes to one big still each small change must be logically complete

More Related Content

What's hot (20)

PDF
Git training v10
Skander Hamza
 
PPTX
Git 101 for Beginners
Anurag Upadhaya
 
PDF
Builder Design Pattern (Generic Construction -Different Representation)
Sameer Rathoud
 
PDF
Git Started With Git
Nick Quaranto
 
PDF
Git real slides
Lucas Couto
 
PDF
git and github
Darren Oakley
 
PDF
Git & GitHub WorkShop
SheilaJimenezMorejon
 
KEY
Introduction To Git
Arnaud Seilles
 
PPTX
git, 이해부터 활용까지
jylee1229
 
PDF
Git Introduction Tutorial
Thomas Rausch
 
PPTX
Git+github
Guilherme Lima Pereira
 
PPTX
Introduction git
Dian Sigit Prastowo
 
PPTX
Git n git hub
Jiwon Baek
 
PDF
Github - Git Training Slides: Foundations
Lee Hanxue
 
PDF
Git and git flow
Fran García
 
PDF
Jenkins Pipelines
Steffen Gebert
 
PDF
Git pour les (pas si) nuls
Malk Zameth
 
PDF
Git slides
Nanyak S
 
PPTX
Overview of github
Sangeetha Subramani
 
PPTX
Git Lab Introduction
Krunal Doshi
 
Git training v10
Skander Hamza
 
Git 101 for Beginners
Anurag Upadhaya
 
Builder Design Pattern (Generic Construction -Different Representation)
Sameer Rathoud
 
Git Started With Git
Nick Quaranto
 
Git real slides
Lucas Couto
 
git and github
Darren Oakley
 
Git & GitHub WorkShop
SheilaJimenezMorejon
 
Introduction To Git
Arnaud Seilles
 
git, 이해부터 활용까지
jylee1229
 
Git Introduction Tutorial
Thomas Rausch
 
Introduction git
Dian Sigit Prastowo
 
Git n git hub
Jiwon Baek
 
Github - Git Training Slides: Foundations
Lee Hanxue
 
Git and git flow
Fran García
 
Jenkins Pipelines
Steffen Gebert
 
Git pour les (pas si) nuls
Malk Zameth
 
Git slides
Nanyak S
 
Overview of github
Sangeetha Subramani
 
Git Lab Introduction
Krunal Doshi
 

Viewers also liked (9)

PDF
Effective Development With Eclipse Mylyn, Git, Gerrit and Hudson
Chris Aniszczyk
 
PDF
EclipseCon 2010 tutorial: Understanding git at Eclipse
msohn
 
PDF
EclipseCon 2010 talk: Towards contributors heaven
msohn
 
PPT
Understanding and Using Git at Eclipse
Chris Aniszczyk
 
PDF
Git - Get Ready To Use It
Daniel Kummer
 
PDF
News from Git in Eclipse - EclipseCon EU - 2016-10-26
msohn
 
PPTX
Git like a Pro (How to use it as it was meant to)
Dennis Doomen
 
PPTX
Gerrit Code Review
Luca Milanesio
 
Effective Development With Eclipse Mylyn, Git, Gerrit and Hudson
Chris Aniszczyk
 
EclipseCon 2010 tutorial: Understanding git at Eclipse
msohn
 
EclipseCon 2010 talk: Towards contributors heaven
msohn
 
Understanding and Using Git at Eclipse
Chris Aniszczyk
 
Git - Get Ready To Use It
Daniel Kummer
 
News from Git in Eclipse - EclipseCon EU - 2016-10-26
msohn
 
Git like a Pro (How to use it as it was meant to)
Dennis Doomen
 
Gerrit Code Review
Luca Milanesio
 
Ad

Similar to Effective Git with Eclipse (20)

PPT
Effective Git - EclipseCon 2011 tutorial
msohn
 
PPTX
Learn Git - For Beginners and Intermediate levels
Gorav Singal
 
PDF
Git training
eric7master
 
PDF
[PUBLIC] Git – Concepts and Workflows.pdf
ChimaEzeamama1
 
PDF
git-presentation.pdf
2022bcacsbshubh12897
 
PPTX
Introduction to git, a version control system
Kumaresh Chandra Baruri
 
PPT
3 Git
Fabio Fumarola
 
PDF
Introduction to git, an efficient distributed version control system
AlbanLevy
 
PPTX
Git Basics for Software Version Management
ishanmittal49
 
PPTX
Git presentation bixlabs
Bixlabs
 
PPTX
Git and GitHub
Priya Nayak
 
PDF
Git with the flow
Dana White
 
PPTX
Intro to Git and Github
Andrew Babiec
 
PPT
Git
Vijay Kani
 
PDF
Git basics with notes
Surabhi Gupta
 
PPTX
Git and Github workshop GDSC MLRITM
gdsc13
 
PPTX
Git 101 - An introduction to Version Control using Git
John Tighe
 
ODP
Git tech talk
razasayed
 
PPTX
Introduction to Git and GitHub
Bioinformatics and Computational Biosciences Branch
 
ODP
Practical git for developers
Wim Godden
 
Effective Git - EclipseCon 2011 tutorial
msohn
 
Learn Git - For Beginners and Intermediate levels
Gorav Singal
 
Git training
eric7master
 
[PUBLIC] Git – Concepts and Workflows.pdf
ChimaEzeamama1
 
git-presentation.pdf
2022bcacsbshubh12897
 
Introduction to git, a version control system
Kumaresh Chandra Baruri
 
Introduction to git, an efficient distributed version control system
AlbanLevy
 
Git Basics for Software Version Management
ishanmittal49
 
Git presentation bixlabs
Bixlabs
 
Git and GitHub
Priya Nayak
 
Git with the flow
Dana White
 
Intro to Git and Github
Andrew Babiec
 
Git basics with notes
Surabhi Gupta
 
Git and Github workshop GDSC MLRITM
gdsc13
 
Git 101 - An introduction to Version Control using Git
John Tighe
 
Git tech talk
razasayed
 
Practical git for developers
Wim Godden
 
Ad

More from Chris Aniszczyk (20)

PDF
Bringing an open source project to the Linux Foundation
Chris Aniszczyk
 
PDF
Starting an Open Source Program Office (OSPO)
Chris Aniszczyk
 
PDF
Open Container Initiative Update
Chris Aniszczyk
 
PDF
Cloud Native Landscape (CNCF and OCI)
Chris Aniszczyk
 
PDF
Rise of Open Source Programs
Chris Aniszczyk
 
PDF
The Open Container Initiative (OCI) at 12 months
Chris Aniszczyk
 
PDF
Open Source Lessons from the TODO Group
Chris Aniszczyk
 
PDF
Getting Students Involved in Open Source
Chris Aniszczyk
 
PDF
Life at Twitter + Career Advice for Students
Chris Aniszczyk
 
PDF
Creating an Open Source Office: Lessons from Twitter
Chris Aniszczyk
 
PDF
The Open Source... Behind the Tweets
Chris Aniszczyk
 
PDF
Apache Mesos at Twitter (Texas LinuxFest 2014)
Chris Aniszczyk
 
PDF
Evolution of The Twitter Stack
Chris Aniszczyk
 
PDF
Open Source Craft at Twitter
Chris Aniszczyk
 
KEY
Open Source Compliance at Twitter
Chris Aniszczyk
 
ODP
Evolution of Version Control In Open Source
Chris Aniszczyk
 
ODP
ESE 2010: Using Git in Eclipse
Chris Aniszczyk
 
KEY
SWTBot Tutorial
Chris Aniszczyk
 
KEY
Helios in Action: Git at Eclipse
Chris Aniszczyk
 
PDF
Eclipse e4
Chris Aniszczyk
 
Bringing an open source project to the Linux Foundation
Chris Aniszczyk
 
Starting an Open Source Program Office (OSPO)
Chris Aniszczyk
 
Open Container Initiative Update
Chris Aniszczyk
 
Cloud Native Landscape (CNCF and OCI)
Chris Aniszczyk
 
Rise of Open Source Programs
Chris Aniszczyk
 
The Open Container Initiative (OCI) at 12 months
Chris Aniszczyk
 
Open Source Lessons from the TODO Group
Chris Aniszczyk
 
Getting Students Involved in Open Source
Chris Aniszczyk
 
Life at Twitter + Career Advice for Students
Chris Aniszczyk
 
Creating an Open Source Office: Lessons from Twitter
Chris Aniszczyk
 
The Open Source... Behind the Tweets
Chris Aniszczyk
 
Apache Mesos at Twitter (Texas LinuxFest 2014)
Chris Aniszczyk
 
Evolution of The Twitter Stack
Chris Aniszczyk
 
Open Source Craft at Twitter
Chris Aniszczyk
 
Open Source Compliance at Twitter
Chris Aniszczyk
 
Evolution of Version Control In Open Source
Chris Aniszczyk
 
ESE 2010: Using Git in Eclipse
Chris Aniszczyk
 
SWTBot Tutorial
Chris Aniszczyk
 
Helios in Action: Git at Eclipse
Chris Aniszczyk
 
Eclipse e4
Chris Aniszczyk
 

Effective Git with Eclipse

  • 1. Effective Git http://eclipse.org/egit http://code.google.com/p/ gerrit Matthias Sohn (SAP) + = Stefan Lay (SAP) Chris Aniszczyk(Redhat) Shawn Pearce (Google)
  • 2. 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 &quot;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
  • 3. Git at Eclipse Eclipse defined a roadmap to move to Git CVS 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.
  • 4. 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] [1] http://code.google.com/p/rietveld/downloads/detail?name=Mondrian2006.pdf
  • 5. Code Review – Benefits Four eyes catch more bugs Catch bugs early to save hours of debugging Mentoring of new developers / contributors Learn from mistakes without breaking stuff Establish trust relationships Prepare for more delegation Good alternative to pair programming asynchronous and across locations Coding standards Keep overall readability & code quality high Guido van Rossum [1] [1] http://code.google.com/p/rietveld/downloads/detail?name =Mondrian2006.pdf
  • 6. Developer PC Gerrit git git git git Developer PC git git Hudson - clone repository - fetch / push changes - verify proposed changes - continuous integration builds
  • 7. Developer PC git git Gerrit git git git git push improved change 10 Developer PC git git fetch change 23 to try it master change 12 change 10 change 23 submit accepted change 12 fetch master to get updates
  • 9. Git Concepts – config files git config –l git config –e --system --global System <gitinst>/etc/gitconfig Global ~/.gitconfig Repository Specific .git/config
  • 11. 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
  • 12. Making Changes Checkout: populate working tree with the commit you want to start working from most of the time you will checkout a branch  checkout the commit pointed to by the branch (aka: tip of the branch) per file checkout means revert ! Calculator .git <working tree> git checkout master
  • 13. Making Changes Just start doing your changes modify, add, delete files Tell Git which changes you intend to commit git add EGit helps by auto-detecting what you changed
  • 14. Commiting 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
  • 15. Commits Commit history B is successor of A C is successor of B A B C
  • 16. Branches Branch is a named pointer to a commit Commit command moves the pointer A B C master A B C master D commit
  • 17. Branches The (branch) pointer can also be moved “manually” to any commit git reset A B C master D A B C master D reset B
  • 18. Branches What happens on next git commit ? C and D continue to exist but they are not in the history or the master branch A B C master D A B C master D E commit
  • 19. Branches Usually there are many branches in a Git repository Branches can also be deleted A B C origin/master D E feature 1 F bugfix 15 G feature 2
  • 20. HEAD HEAD – pointer to a branch Means: “Current Branch” – the branch which you have checked out A B C D E feature 1 F bugfix 15 HEAD origin/master
  • 22. Clone Remote Repository Git is a distributed versioning system git clone <remote-repo> cloned 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
  • 23. Clone Remote Repository Remote tracking branches, full names: remotes/origin/master remotes/origin/feature-1 A B C master D E F HEAD origin/feature-1 origin/master Local clone
  • 24. Remote Tracking Branches Just like any other branch, but read-only possible: git checkout origin/feature1 However, HEAD gets detached! A B C master D E F HEAD origin/feature-1 origin/master
  • 25. 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 HEAD origin/feature-1 origin/master Local clone G origin/feature-1 fetch
  • 26. Fetch Always safe to do Updates only remote tracking branches Does never change local branches
  • 28. Merge git 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
  • 29. Merge Easy Case - Fast Forward git merge feature-1 no new commit, just move the pointer A B E HEAD feature-1 A B E HEAD feature-1 1.0 1.0 merge feature-1
  • 30. Git Concepts – Cherry Pick git 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
  • 31. Git Concepts - Rebase Alternative to Merge - Keeping history linear git rebase 1.0 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
  • 33. Push git push origin HEAD:master From local to remote repository more 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
  • 34. 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 HEAD D A B C master D Remote “origin” repo push
  • 35. 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” repo push
  • 36. Git Concepts - 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
  • 37. 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
  • 38. Push Which graph do you like more ? A B D master Remote “origin” repo C E A B D master C ’ Remote “origin” repo
  • 40. Push Push to Gerrit is the same like push to Git with one Gerrit speciality: refs/for in the target branch name Compare: Push to Git: git push origin HEAD:master Push to Gerrit: git push origin HEAD: refs/for /master
  • 41. Push It seems like every push to Gerrit goes to the same branch refs/for/master However, Gerrit tricks your git client: it creates a new branch for the commit(s) you push and creates a new open Gerrit change containing the pushed commit
  • 42. 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 } …
  • 43. Push What if feature branch has 2 commits ? Remember Git semantics for Push? ALL commits from the local branch not available in the remote branch  2 changes in Gerrit ! A B origin/master C Local repo feature-1 HEAD D 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
  • 44. Changes Change consists of Change ID (important!) metadata (owner, project, etc..) one or more patch-sets comments votes Patch Set represents a Git Commit
  • 45. 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
  • 46. 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 we need to also move a number of other related class, which we aren't quite ready to do. Change-Id: I1bd54ea0500337799a8e792874c272eb14d555f7 Signed-off-by: Shawn O. Pearce <[email protected]>
  • 47. Push New Patchset No dependencies between Patchsets can ’t push a successor commit as the next patchset commit D can ’t be Patch Set 2 of change 1234 A B origin/master C Local repo feature-1 HEAD D Change 1234, Patch Set 1
  • 48. 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 HEAD D Change 1234, Patch Set 1
  • 49. Push New Patch Set A Common Mistake author of the Patch Set 1 is not available and somebody else needs to continue and provide Patch Set 2 use git pull to get the Patch Set 1 into a local branch Fix issues in commit Push (including the same Change-Id) Gerrit rejects!
  • 50. 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 HEAD master
  • 51. Push New Patch Set The right way: fetch (don ’t pull) create a new branch based on the fetched patchset 1 fix the issue commit –amend push
  • 52. Push New Patchset The right way: commit D is not successor of C D 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
  • 53. Review and Vote Show in Gerrit Web UI Voting the lowest mark means Veto Highest marks in all voting categories needed for change to be merged
  • 54. 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 show in Web UI
  • 55. Best Practices Create local branch for each feature or bugfix you work on branch name tells you what your intention was less likely you will mix two features in the same branch you can have many feature branches at a time and switch between them
  • 56. Best Practices Push finished features only who wants to review non finished feature ? who wants non-finished features in history ? Push complete feature as one commit even if you created many commits squash them into one before push otherwise you create one change in Gerrit per each commit ! use multiple changes if feature can be split into smaller logical units and use last change to switch on the new feature
  • 57. Best Practices Write good commit message First line is summary Empty line between paragraphs Explain WHY you did the change not WHAT you did (this is visible from the commit) Prefer many small changes to one big still each small change must be logically complete