I've started at a new contract very recently and it's the first time I've come across Gerrit.  Gerrit is a code review tool that allows you to hook into GIT and prevents any code being added into your repository unless it has been peer-reviewed.  

If you are anything like me, when it comes to GIT I use a GUI and when using Gerrit will need to do a lot of re-basing, cherry picking and last commit amends. I've been using Git on Github and BitBucket for several years using SourceTree and I've never had any issues.  

When it comes to doing GIT amends it's not quite up to the job.  I tried using GIT GUI which had limited support for what I needed but in the end I used a combo of learning a bit more of the command line stuff combined with TortiseGit.

 I've Failed A Code Review... Now What ?

As long as you haven't got any new changes then the process is very simple:

  1. Make your changes
  2. Go to you repository, right click on it and select Git Commit

Git_amends_screenshot1

  1. If you have previously done a commit, click 'Amend Last Change'.  Note that in your commit message you can see your Change-Id (this is how Gerrit tracks your changes).  Assuming this is a change, I prefer to click 'Show diff' button, this will then allow you to add in any new changes etc...  by including/excluding the files using the bottom window.

Git_amends_screenshot2

  1. Click OK and you should now see your changes in Gerrit awaiting review

Git_amends_screenshot3

Push Your Changes

(git show-ref can also be handy if you get a 'src refspec master does not match any when pushing commits in git' error)

Viewing Previous Commits

Undoing your last push after you fail a code review

(after you do this you can do whatever but you need to add the Change-Id Gerrit inserts into your commit message to keep it hooked up)

Updating last commit message

Cherry Picking

Rebasing

fatal: Unpack error, check the server log

This means your commit might have gotten a bit too big and you need to turn the Gerrit data package optimization off:

When Shit goes bad

When you're switching between different code branches and reviewing other code you may get a lot of conflicts.  The easiest way I've found is to copy your changes out of the project and then do a hard reset and try again.

Create a branch locally machine and switch in this branch

How To Get Out of Git Amend

Press i to start adding text. Save and exit by pressing esc and :wq

What To Do If You Forget To Go A GIT Amend And Create Two Commits

First, check out the latest commit of the two. Next, do a git log and find the first commit before your mistake and grab the commit ID. This will be used as a base commit so we can squash the two commits into one.

git_gerrit_mistake

Now, do a rebase from your checkout to the base commit:

Next do a git log:

Now remove one of the duplicate change ID's, I recommend using the one at the top and it's committed message. Save it - press ESC, then : then wp) Do a final git amend:

Then you should be able to push and abandon the old commit.

Conclusion

Did this help you solve you Gerrit issues ? Have you got other tips.. if so leave a comment in the section below to help the community out