Working with Git and Gerrit » History » Version 23

Karsten Dambekalns, 2010-08-13 11:32
Using dsa instead of rsa keys, added hint on testing ssh connection. Added hint on configuring remote for push to gerrit. Reworked section on revising changes.

1 1 Karsten Dambekalns
h1. Working with Git and Gerrit Code Review
2 22 Karsten Dambekalns
4 1 Karsten Dambekalns
5 20 Karsten Dambekalns
*Note:* We have not yet switched to Git and Gerrit, but are currently in the process of doing so. Planned date for the switch to git for FLOW3 and TYPO3 Phoenix projects is August 23rd.
6 1 Karsten Dambekalns
7 20 Karsten Dambekalns
For FLOW3 and TYPO3 Phoenix we are using Git and the Gerrit Code Review system to channel commits for review.
8 1 Karsten Dambekalns
9 20 Karsten Dambekalns
Now here is what you need to know to contribute, more details on Git are available in the "Pro Git book":, the "Gerrit documentation": and other resources.
10 1 Karsten Dambekalns
h2. Git
h3. Getting Git
Firstly, if your machine doesn't already have it installed, get a copy of the 'git' version control system. This is available for many platforms from their upstream package repositories or, failing that, can be downloaded in both source and binary form from
If you look for a GUI tool to use with git, you can use the built-in GUI by starting gitk from the command line. For Windows there is "TortoiseGit":, on the Mac you can use "gitx": and for all platforms (Mac, Linux, Windows) there is "SmartGit": - more tools exist, if those don't fit your needs.
h3. First-time Git setup
You must tell git a little about yourself so it knows who you are when doing commits. So simply do this:
<pre>$ git config --global "John Doe"
$ git config --global</pre>
More details and helpful configuration options are explained in the "Pro Git book, chapter 1.5":
h3. Getting the codebase
30 15 Karsten Dambekalns
*Note:* To clone what we call "distribution" or "application" a single clone command does not suffice! We used svn:externals, and a replacement for this is in git are "submodules". See below for a recipe to clone those types of project.
31 7 Karsten Dambekalns
32 12 Karsten Dambekalns
You can download the repository for a project by running one of
<pre>git clone git://<projectname>.git
git clone git://<projectname>.git <name-of-directory></pre>
35 13 Karsten Dambekalns
to place the clone in a directory called like the project or to place your clone in a specific directory.
36 19 Karsten Dambekalns
This will give you a complete copy of the repository and unless you are exceptionally short of either disk space, or time, is the approach we recommend. Unlike with Subversion, you are not just checking out a particular branch, but making a local copy of the project's whole revision history, across all of it's branches. This does make the download pretty large - but makes daily work with Git blazingly fast and allows for all the goodies of a distributed SCM.
39 1 Karsten Dambekalns
h3. Updating the local copy
41 15 Karsten Dambekalns
*Note:* To update what we call "distribution" or "application" a single pull command does not suffice! We used svn:externals, and a replacement for this is in git are "submodules". See below for a recipe to update those types of project.
42 7 Karsten Dambekalns
43 1 Karsten Dambekalns
When you want to update the local repository with the central one, running
<pre>git pull</pre>
will pull all of the new changes into your local repository, and merge those changes into your correct working tree. Note that whilst this is fine when you are browsing the repository, you may want to exercise more control over how upstream changes are merged into your development code.
47 7 Karsten Dambekalns
h3. Handling "superprojects" bundling multiple packages
49 15 Karsten Dambekalns
We used svn:externals a lot to combine some common configuration and code with assorted packages to form "distributions" (e.g. FLOW3 and TYPO3 Phoenix) or "applications" (e.g. the Blog). Git does have a replacement for svn:externals called submodules. It does not allow pointing to trunk instead of a fixed revision, so some care needs to be taken.
50 1 Karsten Dambekalns
51 15 Karsten Dambekalns
h4. Cloning a superproject
52 1 Karsten Dambekalns
53 15 Karsten Dambekalns
As usual you need to clone the distribution or application. Then do the following from the root folder of the new clone:
<pre>git submodule init
git submodule update</pre>
56 7 Karsten Dambekalns
57 16 Karsten Dambekalns
If you are lazy, the same effect can be achieved cloning like this right from the start:
<pre>git clone --recursive git://<projectname>.git</pre>
60 15 Karsten Dambekalns
*Important:* You must then install the gerrit commit-msg hook into the superproject as well as any new clone. This can be done with the help of @git submodule foreach@ as shown above.
61 7 Karsten Dambekalns
62 15 Karsten Dambekalns
h4. Updating a superproject
63 1 Karsten Dambekalns
64 17 Bastian Waidelich
When you update the superproject using @git pull@ the submodules will not be updated, you need to run an additional @git submodule update@ afterwards.
65 1 Karsten Dambekalns
66 15 Karsten Dambekalns
*Note:* Make sure you know what you are doing!
67 9 Karsten Dambekalns
68 15 Karsten Dambekalns
If you have pulled changes into a submodule or worked on the code there, submodule update will override the changes and check out the commit recorded in the superproject.
69 7 Karsten Dambekalns
70 15 Karsten Dambekalns
If you worked on a topic branch (very recommended!) you can simply checkout that branch again to get your work back.
71 7 Karsten Dambekalns
72 15 Karsten Dambekalns
If you want to update submodules prior to them being "officially" included in the superproject you can do that by
<pre>git checkout master
git pull</pre>
in the submodule root folder. So, to simulate what we had earlier with svn:externals pointing to trunk of all packages included with a superproject you can do this:
<pre>git submodule foreach 'git checkout master; git pull'</pre>
77 7 Karsten Dambekalns
78 2 Karsten Dambekalns
h3. Working on the code
79 1 Karsten Dambekalns
80 2 Karsten Dambekalns
We strongly recommend that you do all of your development upon 'topic branches.' This allows you to isolate multiple unrelated changes, and makes it easier to keep your tree in sync with the upstream one.
81 1 Karsten Dambekalns
Before creating a new topic branch, running
<pre>git fetch</pre>
84 2 Karsten Dambekalns
will make sure that your repository knows about the latest upstream changes (unlike @git pull@, this will not update any files that you may have checked out).
85 1 Karsten Dambekalns
To create a new topic branch:
<pre>git checkout -b <branch></pre>
88 2 Karsten Dambekalns
For example, to work on a patch to fix ereg warnings, based on the current development code, I would do:
<pre>git checkout -b fix-ereg-warnings origin/master</pre>
90 1 Karsten Dambekalns
This puts me on a new branch, ready to start writing code. All new development should be based upon the origin/master branch, submissions based upon other branches are unlikely to be accepted, unless they address issues that are solely present in that branch.
92 2 Karsten Dambekalns
To tell git about any new files you create as part of your patch
<pre>git add</pre>
is used. If your patch results in any new byproducts (cache, metadata files, etc) that git should not be tracking, please make sure that they're caught by the .gitignore mechanism. You can do this by checking that they don't appear in the output from @git status@
95 1 Karsten Dambekalns
96 2 Karsten Dambekalns
@git mv@ and @git rm@ are used to move and delete files respectively.
97 1 Karsten Dambekalns
98 2 Karsten Dambekalns
<pre>git commit -a</pre> is used to commit code to all of the files that git is currently tracking (that is, all of the files that you have checked out from the repository, and all those which you have run git add on)
100 1 Karsten Dambekalns
Be sure to explore the power of staging your changes: unlike with Subversion you can stage some changes to a file for commit while not (yet) stage other changes to the same file. This can come in very handy. To shelve work in progress, have a look at @git stash@.
h3. When you can't see the wood for the trees
If, in the middle of development, you discover that you've gone down a blind alley, and wish to go back to the state of your last commit
<pre>git reset --hard</pre>
will discard all of the changes you have made since the last commit, or
<pre>git checkout -f <file></pre>
will restore <file> to the state it was in at the last commit.
h3. Keeping up with upstream changes
If you're working on a long running development project, you will find that the point your created your topic branch rapidly recedes into history. At some point (and at least before you share your code with us), you'll probably want to update your tree. There are a number of ways of doing this.
If you haven't shared your tree with anyone else, then you can use
115 2 Karsten Dambekalns
<pre>git rebase <branch> <topic></pre>
116 1 Karsten Dambekalns
(Where <branch> is the name of the upstream branch - for example origin/master, and <topic> is the name of the topic branch you are currently working on)
117 2 Karsten Dambekalns
Note that git rebase changes your local history (it moves the branch point of your topic branch to the tip of the upstream branch), and is a bad idea if others have cloned your repository. See @man git-rebase@ for more details.
119 1 Karsten Dambekalns
If you can't rebase, then consider either merging the changes onto your local branch, or creating a new topic branch and cherry picking your changes onto it. The man pages for @git merge@ and @git cherry-pick@ provide more detail on these options.
121 2 Karsten Dambekalns
122 1 Karsten Dambekalns
h2. Sharing your code with us
How you work from this point onwards depends on how you intend making your code available to us. We're still happy to receive submission by patch (attach the patch to the issue you created or worked on using the Forge), but it makes it much easier for us if you push directly from your git tree to our code review system, gerrit.
h3. Registering with gerrit
To register with gerrit, visit and log in using your username. Click on 'Settings' -> 'Profile', and check that gerrit correctly knows your name and email address.
In order to be able to upload code, you now need to create a ssh key that gerrit can use to identify you, or tell gerrit about one that already exists.
To create a new ssh key, if you don't already have one, run
133 23 Karsten Dambekalns
<pre>ssh-keygen -t dsa -f ~/.ssh/id_dsa</pre>
The public key for this is now stored in ~/.ssh/
135 1 Karsten Dambekalns
To tell gerrit about your key, log in, and go to 'Settings'. Select 'SSH Keys', and paste your public key into the "Add SSH Public Key" box, or click on the 'Open Key...' option to load it from the filesystem. Click on 'Add' to add the new public key.
137 23 Karsten Dambekalns
To make things easier, set up ssh so that it knows about the defaults for the gerrit server. Edit @~/.ssh/config@, and add a section like:
138 1 Karsten Dambekalns
User <username> 
140 23 Karsten Dambekalns
IdentityFile ~/.ssh/id_dsa 
141 1 Karsten Dambekalns
Port 29418</pre>
142 23 Karsten Dambekalns
(where <username> is what you logged in with, i.e. your username)
143 1 Karsten Dambekalns
144 23 Karsten Dambekalns
To verify your ssh key is working correctly, try using an ssh client to connect to gerrit. You should get the following output if it all works:
<pre>$ ssh
gerrit: no shell available
Connection to closed.</pre>
149 20 Karsten Dambekalns
h3. The change id hook
151 23 Karsten Dambekalns
Gerrit introduces the concept of "Change IDs". This is a unique reference for a particular change, which remains constant regardless of any changes that are made to the implementation. This allows a single reference to be attached to a given modification, irrespective of any rewrites that may occur as a result of review comments. Manually maintaining change ids is a pain, so gerrit provides a git hook which can be used to automatically add a change id to any new modifications you create.
152 20 Karsten Dambekalns
153 23 Karsten Dambekalns
Because hooks cannot be transferred with a clone of a project, you must copy them to your @.git/hooks@ directory and make them executable. The hook can be downloaded from the gerrit server by running the following in the top level of your git tree
154 20 Karsten Dambekalns
<pre>scp -p -P 29418 .git/hooks/</pre>
*Note:* Do this for every working copy you clone! Make sure the hook is marked as executable after it is installed (scp should take care of this)!
When using a superproject with submodules (see below), you can use this to install the hook for every submodule:
<pre>git submodule foreach 'scp -p -P 29418 .git/hooks/'</pre>
If you cannot use scp (you should, it takes care of permissions automatically!), you can download the hook via HTTP, e.g. using @curl@, @wget@ or a browser, from
164 1 Karsten Dambekalns
h3. Uploading to gerrit
165 18 Karsten Dambekalns
166 2 Karsten Dambekalns
When submitting to gerrit, it's important to realise that each commit in your branch will become a changeset in the upstream codebase, typically with no modification at our end. It is therefore important that these commits follow some simple rules...
168 1 Karsten Dambekalns
First, each commit should be complete. The rule "one change per commit, one commit per change" applies here. Each commit should build and test in its own right. Typically, this means that a change will be in a small number of commits. If, during development, you have created many more than this (for example, you've created a large number of bug fix commits), please use 'git rebase', or cherry pick these commits into a separate tree before uploading them.
169 2 Karsten Dambekalns
170 20 Karsten Dambekalns
Secondly, each commit should have a meaningful revision log message. The internals of git means that we can't easily edit these before pushing them into the tree, so we'd like you to get them right for us! A commit message should be comprised of a single 'subject' line (which must not end with a full stop), followed by a blank line, followed by one or more paragraphs explaining the purpose of the patch. Here is "more information on git revision log conventions":
171 2 Karsten Dambekalns
172 20 Karsten Dambekalns
The usual rules for relating to Forge issues and marking changes (task, bugfix, ...) apply (see FLOW3 CGL). To be able to search for issue numbers in gerrit, please add a "footer" line in the following format for every issue your commit relates to (in addition to any "fixes", "resolves" or "relates to" references):
<pre>Issue: #1234</pre>
175 2 Karsten Dambekalns
Once you have commits in this form, use
176 1 Karsten Dambekalns
<pre>git log -p origin/<branch>..HEAD</pre>
177 11 Karsten Dambekalns
(where <branch> is the upstream branch upon which you are basing this patch) to check that what you're giving us makes sense.
179 1 Karsten Dambekalns
Make sure the Change-Id line is in the commit message(s), if it is not, check the commit-msg hook and amend the commit to get the Change-Id in.
180 11 Karsten Dambekalns
Then, upload them to gerrit using
182 2 Karsten Dambekalns
<pre>git push ssh://<projectname> HEAD:refs/for/<branch></pre>
(again <branch> is the name of the upstream branch that you are pushing the changes into, not the name of any local branch you may have been developing on).
185 1 Karsten Dambekalns
In this case, 'refs/for' is a literal string. So, if you had been developing against master, you can upload your changes with:
<pre>git push ssh://<projectname> HEAD:refs/for/master</pre>
This relies upon the ssh configuration you performed earlier. If it fails to work, please consult the "gerrit troubleshooting notes":
Assuming all has gone well, this will have added the entry to the code review queue. The output from git review will give you a change number - this is a unique reference for this particular set of changes. During review you'll be emailed with any comments anyone makes, and can respond to those comments using the gerrit web interface (see the section on reviewing, below). It's possible that issues with your change may be noticed during the review process, and you may be asked to revise it, or update changes to the tip of the tree.
192 23 Karsten Dambekalns
To make life easier, you can add the review server and the needed refspec to your @.git/config@ file using @pushurl@ and @push@ like this:
<pre>[remote "origin"]
  fetch = +refs/heads/*:refs/remotes/origin/*
  url = git://<projectname>.git
  pushurl = ssh://<projectname>
  push = HEAD:refs/for/master
Then you can use a simple
<pre>git push</pre>
to submit a change to gerrit.
Depending on your setup it may help to include username and port in the @pushurl@ line to be picked up by some tools (like SmartGit).
205 1 Karsten Dambekalns
h3. Revising your change
206 2 Karsten Dambekalns
207 23 Karsten Dambekalns
It's possible that your modifications won't be accepted first time. In this case, you need to revise your changes, and resubmit them to gerrit. Please note that this should always be done by modifying your original changeset, not by submitting a new change that makes the required fixes. Either @git commit --amend@, or @git rebase@ should be used to combine your changes with the original changeset, and then you should push this to gerrit like you did before:
<pre>git push ssh://<projectname> HEAD:refs/for/<branch></pre>
The @Change-Id@ lines in the commit message will be used to match the commit to the existing review entry.
210 2 Karsten Dambekalns
211 23 Karsten Dambekalns
If no @Change-Id@ was present in the entry, things get more complicated. To add an additional patch set to a change, replacing it with an updated version of the same logical modification, send the new commit to the change's ref. For example, to add the commit whose SHA-1 starts with @c0ffee@ as a new patch set for change number 1979, use the push refspec @0ffee:refs/changes/1979@ as below:
<pre>git push ssh://<projectname> c0ffee:refs/changes/1979</pre>
213 2 Karsten Dambekalns
214 23 Karsten Dambekalns
*Note*: This method is considered deprecated in favour of @Change-Id@ lines!
215 1 Karsten Dambekalns
216 23 Karsten Dambekalns
You can obtain the sha1 hash of a commit by using 'git show' (if it is on the tip of your current branch), or 'git log' (if it is in your history). Also make sure to read the  "documentation on  uploading changes": for details.
218 2 Karsten Dambekalns
h3. Updating your change
It's possible that your change may have been made against a tree which is too old for it to apply to the tip. In this case, gerrit will let you know that there is a collision, and request that you update the change to the tip.
You can do this with
<pre>git rebase origin/master <topic></pre>
(assuming your patch is against the 'master' git branch, and lives on the <topic> branch).
225 1 Karsten Dambekalns
And then simply resubmit your change in the same way as if you had been asked to revise it (see notes above)
227 2 Karsten Dambekalns
228 1 Karsten Dambekalns
h3. Submitting by patch
229 2 Karsten Dambekalns
If all of this seems too daunting (and please don't let it put you off) you can still, of course, submit patches by email.
<pre>git diff HEAD</pre>
will give you the set of changes if you don't do local commits. If you make topic branches, and commit things locally, but don't want to go through the whole gerrit process,
<pre>git diff master..<topic></pre>
will give all of the changes between the branch point of you topic branch (assuming you branched from 'master') and the last commit.
You can attach those to issues in the Forge issue trackers as before. Note, however, by doing this you're making someone else take the patch, create a topic branch in their local tree, apply the patch, and push it into gerrit.
237 4 Karsten Dambekalns
*Things would be much more efficient if you pushed into gerrit yourself. Please?*
238 1 Karsten Dambekalns
239 4 Karsten Dambekalns
h4. Committing Patches
241 23 Karsten Dambekalns
Patches sent in or attached to the issue tracker still need to be committed and pushed by someone. To commit a patch, make a new topic branch, and apply the patch to that branch. When committing, use
242 4 Karsten Dambekalns
<pre>git commit --author="A.N. Author <>"</pre>
to acknowledge the patch author (your name will still be there as the committer of the patch). Push the patch into gerrit as usual, and wait for reviews ...
244 1 Karsten Dambekalns
245 2 Karsten Dambekalns
h2. Reviewing changes
246 7 Karsten Dambekalns
247 2 Karsten Dambekalns
We'll now look at how changes that have made it into gerrit can be reviewed. All code review now happens via the interface. You should log in there as detailed above (using your user credentials), and make sure that the email address points to somewhere you'll read regularly.
249 1 Karsten Dambekalns
You'll be presented with a list of patches requiring review or, if someone has asked, patches you've been explicitly requested to review. There are two types of review - Code Review and Verification. Code Review means that you have read through the code, and are satisified that it works properly, follows the tree's style, and generally doesn't suck. Verification means that you have taken a copy of the patch and tested it. We will automate the verification step through our CI server Hudson, but generally both should be perfomed (also) manually.
251 4 Karsten Dambekalns
To perform a code review, go through each of the diffs in the current changeset for the code you have decided to review. You can double click on a line to leave a comment. Once you have completed commenting, click on the 'Review' button that's about 3/4 of the way down the page containing the list of patch sets. You will then be asked to score the patch, with a range from -1 to +1. -1 means that you don't think the code should be applied, +1 means that it is good to apply. You can also leave further, general, comments for the patch submitter.
252 1 Karsten Dambekalns
253 4 Karsten Dambekalns
Note that no matter how many +1 or -1 comments a patch receives, the team members can override these to either permit or forbid submission by giving -2 or +2 on a patch ("gatekeeper" permissions). Also, at least one team member must approve a patch in this way before it can be submitted to the tree.
A +2 means that the patch can be submitted. A -2 means that the patch is rejected. Note that these scores are not additive (2 +1s do not make a +2, 4 +1s don't cancel out a -2). This means that gatekeeper's scores are definitive - it doesn't matter what others may score, although that should probably be used as a guide.
257 2 Karsten Dambekalns
In a specific project certain additional rules may applay as to who is allowed to give final acknowlegdement, so make sure to know what you are doing.
258 1 Karsten Dambekalns
259 4 Karsten Dambekalns
To verify the code, pull the git copy into your local tree, using the git command listed as part of the patch details. For sanity's sake, we'd strongly recommend you do this pull into a topic branch you've created for the purpose. Build it, test it, and report your results. Note that a single -1 to verification can block patch submission, so please use these options wisely. If in doubt, score 0 and leave your comments. Please indicate when verifying which platforms you have tested on, and what testing you performed.
h2. Gatekeeper tasks
When a patch is reviewed and verified a few tasks are still open.
h3. Submitting the patch
Once a patch has both a +2 code review, and has verified OK, it may be submitted. A Submit button will appear on the patch's gerrit page - when clicked, the patch will be pushed into the git tree.
h3. Dealing with conflicts
You may find that attempting to cherry-pick a patch via gerrit results in a conflict. One way to address this is to modify the commit. Start with a current repository, cherry-pick the commit, fix it, and push back to gerrit.
<pre>git fetch
git checkout -b <new-topic-branch>
275 1 Karsten Dambekalns
git cherry-pick <sha1 of contribution></pre>
276 4 Karsten Dambekalns
The cherry-pick will give you a warning like
<pre>Automatic cherry-pick failed.  After resolving the conflicts,
mark the corrected paths with 'git add <paths>' or 'git rm <paths>' and commit the result.
When commiting, use the option '-c 822e735' to retain authorship and message.</pre>
if it fails. At this point, resolve the conflict. You can use @git diff@ to determine what it is. Then, continue:
<pre>git add <file with resolved conflict> [<next file with resolved conflict>]
git commit -c <commit hash from cherry pick></pre>
Confirm you will push the right thing:
284 7 Karsten Dambekalns
<pre>git log -p origin/master..HEAD</pre>
285 1 Karsten Dambekalns
Then, push the change back to the open issue in gerrit.
<pre>git push ssh:// <hash>:refs/changes/<gerrit change number></pre>
288 4 Karsten Dambekalns
h3. Modifying a change in gerrit
The typical gerrit workflow expects that instead of gatekeepers modifying changes in gerrit, they will simply leave review comments and expect the committer to fix their patch to address those comments themselves (the theory being that this educates the committers, and thus reduces the load on the gatekeepers in the long term). However, if you want to modify someone's change, you can do so.
Simply fetch the change from gerrit as if you were going to verify it, making sure that you do that fetch into a topic branch. Then, make your modifications and run
<pre>git commit --amend --author "A.N. Author <>"</pre>
Then push this commit into gerrit as a replacement from the original change (see above for details of exactly how to do this)
h2. Thanks
296 2 Karsten Dambekalns
... to the folks over at "OpenAFS": that made us stumble over gerrit and whose wiki page we shamelessly used as a base for this page.
299 23 Karsten Dambekalns
... to Sebastian Kurf├╝rst, Peter Niederlag, Christopher Hlubek for their help with git, gerrit and redmine.
... Bastian Waidelich for being the brave Windows test user.
302 5 Karsten Dambekalns
303 1 Karsten Dambekalns
... to the TYPO3 Core Team for being open towards a change in SCM flavor.