This post discus about various aspects of uploading code changes (review) in Gerrit.

Create Changes

To create new changes for review, push to the project’s magical refs/for/’branch’ ref using any Git client tool:

git push ssh://sshusername@hostname:29418/projectname HEAD:refs/for/branch

Each new commit uploaded by the git push client will be converted into a change record on the server. Other users (e.g. project owners) who have configured Gerrit to notify them of new changes will be automatically sent an email message when the push is completed.

To include a short tag associated with all of the changes in the same group, such as the local topic branch name, append it after the destination branch name. In this example the short topic tag driver/i42 will be saved on each change this push creates or updates:

git push ssh://john.doe@git.example.com:29418/kernel/common HEAD:refs/for/experimental%topic=driver/i42

Review labels can be applied to the change by using the label (or l) option in the reference:

git push ssh://john.doe@git.example.com:29418/kernel/common HEAD:refs/for/experimental%l=Verified+1

The l=’label[score]’ option may be specified more than once to apply multiple review labels. The value is optional. If not specified, it defaults to +1 (if the label range allows it).

Specific reviewers can be requested and/or additional ‘carbon copies’ of the notification message may be sent by including the reviewer (or r) and cc options.

git push tr:kernel/common HEAD:refs/for/experimental%r=a@a.com,cc=b@o.com

The r=’email’ and cc=’email’ options may be specified as many times as necessary to cover all interested parties. Gerrit will automatically avoid sending duplicate email notifications, such as if one of the specified reviewers or CC addresses had also requested to receive all new change notifications.

If you are frequently sending changes to the same parties and/or branches, consider adding a custom remote block to your project’s .git/config file:

$ cat .git/config
…
[remote "exp"]
url = tr:kernel/common
push = HEAD:refs/for/experimental%r=a@a.com,cc=b@o.com
$ git push exp

Replace Changes

To add an additional patch set to a change, ensure Change-Id lines were created in the original commit messages, and just use git push URL HEAD:refs/for/…​ as described above. Gerrit Code Review will automatically match the commits back to their original changes by taking advantage of the Change-Id lines.

If Change-Id lines are not present in the commit messages, consider amending the message and copying the line from the change’s page on the web, and then using git push as described above. If Change-Id lines are not available, then the user must use the manual mapping technique described below.

Manual Replacement Mapping

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 c0ffee:refs/changes/1979 as below:

git push ssh://sshusername@hostname:29418/projectname c0ffee:refs/changes/1979

This form can be combined together with refs/for/’branchname’ (above) to simultaneously create new changes and replace changes during one network transaction.

For example, consider the following sequence of events:

$ git commit -m A                    ; # create 3 commits
$ git commit -m B
$ git commit -m C
$ git push ... HEAD:refs/for/master  ; # upload for review
... A is 1500 ...
... B is 1501 ...
... C is 1502 ...
$ git rebase -i HEAD~3               ; # edit "A", insert D before B
                                     ; # now series is A'-D-B'-C'
$ git push ...
    HEAD:refs/for/master
    HEAD~3:refs/changes/1500
    HEAD~1:refs/changes/1501
    HEAD~0:refs/changes/1502         ; # upload replacements

At the final step during the push Gerrit will attach A’ as a new patch set on change 1500; B’ as a new patch set on change 1501; C’ as a new patch set on 1502; and D will be created as a new change. Ensuring D is created as a new change requires passing the refspec HEAD:refs/for/branchname, otherwise Gerrit will ignore D and won’t do anything with it. For this reason it is a good idea to always include the create change refspec when uploading replacements.

Bypass Review

Changes (and annotated tags) can be pushed directly into a repository, bypassing the review process. This is primarily useful for a project owner to create new branches, create annotated tags for releases, or to force-update a branch whose history needed to be rewritten.

Gerrit restricts direct pushes that bypass review to:

  • refs/heads/*: any branch can be updated, created, deleted, or rewritten by the pusher.
  • refs/tags/*: annotated tag objects pointing to any other type of Git object can be created.

Auto-Merge during Push

Changes can be directly submitted on push. This is primarily useful for teams that don’t want to do code review but want to use Gerrit’s submit strategies to handle contention on busy branches. Using %submit creates a change and submits it immediately, if the caller has Submit permission on refs/for/<ref> (e.g. on refs/for/refs/heads/master).

git push ssh://john.doe@git.example.com:29418/kernel/common HEAD:refs/for/master%submit

On auto-merge of a change neither labels nor submit rules are checked. If the merge fails the change stays open, but when pushing a new patch set the merge can be reattempted by using %submit again.

Selecting Merge Base

By default new changes are opened only for new unique commits that have never before been seen by the Gerrit server. Clients may override that behavior and force new changes to be created by setting the merge base SHA-1 using the ‘%base’ argument:

git push ssh://john.doe@git.example.com:29418/kernel/common HEAD:refs/for/master%base=$(git rev-parse origin/master)

It is also possible to specify more than one ‘%base’ argument. This may be useful when pushing a merge commit. Note that the ‘%’ character has only to be provided once, for the first ‘%base’ argument:

git push ssh://john.doe@git.example.com:29418/kernel/common HEAD:refs/for/master%base=commit-id1,base=commit-id2

Reference :- Gerrit Code Review – Uploading Changes