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:
- Make your changes
- Go to you repository, right click on it and select Git Commit
- 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.
- Click OK and you should now see your changes in Gerrit awaiting review
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
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
git fetch origin git checkout origin/master git log git git fetch ssh:@REF@ && git cherry-pick FETCH_HEAD
git fetch origin git rebase origin/master git log git
fatal: Unpack error, check server logThis 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 badWhen 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 AmendPress 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 CommitsFirst, 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. Now, do a rebase from your checkout to the base commit:
$ git rebase -i a6510e4fb5064ff4dfb08ac5ff1fe528089e70beNext do a git log:
$ git logNow 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 --amendThen you should be able to push and abandon the old commit.