Absolute Beginners Guide To Using Gerrit With Git

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 CommitGit_amends_screenshot1
  3. 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
  4. Click OK and you should now see your changes in Gerrit awaiting review

Git_amends_screenshot3

Useful Command Lines Tools

Push Your Changes

git push origin head:refs/for/master

(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

git log

Undoing your last push after you fail a code review

git reset --soft HEAD~1

(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

git commit --amend

Cherry Picking

git fetch origin
git checkout origin/master
git log git 
git fetch ssh:@REF@ && git cherry-pick FETCH_HEAD

Rebasing

git fetch origin
git rebase origin/master
git log git 

fatal: Unpack error, check server log

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

git push --no-thin origin head:refs/for/master

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.

git reset HEAD --hard  (reset changes)
git clean -fd (clean up unneeded files)

Create a branch locally machine and switch in this branch

git checkout -b [name_of_your_new_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:

$ git rebase -i a6510e4fb5064ff4dfb08ac5ff1fe528089e70be

Next do a git log:

$ git log

Now remove one of the duplicate change ID’s, I recommend using the one at the top and it’s commit message. Save it (esc, then : then wp)

Do a final git amend:

$ git commit --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

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *