Git large rebase made simple, easy and painless

Git clutter

The large Git rebase

At my work we use a simple and clean workflow, for getting our code into production. We use Pull Requests for our branches, and rebase them so our history is nice and tight. This works very well 95% of the time. Branches are sufficiently small, turnaround is fast, and we have no problems. But, sometimes we still end up with:

The large, breaking commit left a little too long

Inevitably, we sometimes have a large Pull Request that is just too cumbersome for a very fast turnaround time. So it is left to sit. For a couple of weeks. Other branches are in the meantime happily trotting along, and they sometimes end up having many conflicting changes. This leads to:

The large git rebase nightmare

Suddenly, we have a branch that has 13 different rebase conflicts, some of them even affecting the same branch multiple times. It is extremely painful and wasteful to keep this branch rebased to master. So we look longignly at that wonderfully simple command:

We know we should not use it, and we will pay the price on all other branches. So maybe we freeze everything and have a ‘merge day’. Very frustrating for everybody. Here is another way:

The merge-squash

Merge step

What we can do instead, is create a ‘provision’ branch, based on the current master. We then select the (very conflicting) branches to be provisioned and put into master, and merge them into the provision branch. The merge conflicts will have to be dealt with, of course, but that should not be too much of a problem.

Squash step

After we have merged in all the branches to be provisioned, we can then easily merge squash them to a single commit, like so (It is a good idea to do this on a backup provision branch the first couple of times, since we will be rewriting history with no safety net):

  1. First, get the commit tags of your current head, and the time of the latest master commit. A great tool for this is GitX. In our case, this could be 0bc47b1 (provision HEAD) and 4154fcb (latest master commit)
  2. Rewind the provision branch to latest master commit:
  3. Now merge squash the branch with the latest HEAD from above:

    All of the merged changes from all of the provisioned branches will now be one single edit right after 4154fcb. Commit this edit:
  4. You have now painlessly merged the changes to one commit that other work can easily be integrated with. Yay!


Ruby Map Array to Hash (Also Rails)

Ruby Map Array to Hash


Sometimes you need to map array value to a hash structure. It could be that you have a set of keys, and you want to map some values related to those keys to a hash in order to return it from a function. Or something else, what do I know. But I do know a nice, clean way to do it:

As they say: The below example is a little basic – but it should still show the gist of it.

And now we have mapped our array to a hash.

With the lookup table in this code, we could have of course just extracted a subset of our lookup_table. But what if it wasn’t such a convenient lookup_table? What if it was methods defined on another class that we want to extract values from? Or database values? For this case, the above little trick of initialising a Hash can come quite in handy once in a while.

Cancan Strong Parameters playing nice together

Cancan and Strong Parameters does not play nice together straight out of the box. However, liuckily it is not hard to get them to do so.

Strong Parameters

The new Strong Parameters gem is included by default in Rails 4, and available as an optional gem for Rails 3 here. Strong Parameters is a big step forward from the now-all-but-deprecated attr_accessible method. (For more information on the issues with attr_accessible, see this Railscast.)

The main benefit of Strong Parameters is that they are built with controller actions in mind, This means that Rails now recognises that different controllers and people should have access to different fields on our models.

CanCan and ActiveModel::ForbiddenAttributes

Cancan strong parameters

Here comes, then, the problem: CanCan’s load_and_authorize_resource method clashes with Strong Parameters, because of this line in CanCan:

There is nothing wrong with this line – it is a wonderful line, that, unfortunately, clashes with Strong Parameters. This is because it uses the [Model Class].new, without using permitted parameters. Therefore, you get an ActiveModel::ForbiddenAttributes error right at the beginning of you controller action.
Luckily, there is a solution:

The solution – CanCanCan

The solution to the clash between CanCan and Strong Parameters comes in the form of the wonderful community-driven continuation of Ryan’s amazing work, in the form of the Gem CanCanCan, which has the same public api as CanCan, and among other things adds the possibility of adding a private method, defining permitted params, like so:

By naming convention, this method will be used for the create controller action.

Read more on this and other ways of playing nice with Strong Parameters on the CanCanCan Wiki here.

Workarounds for good old CanCan

If you do not have the option of switching to CanCanCan, some other workarounds are described here

Get mv to also move hidden files in OSX and Linux

The Pain

I love my terminal. I find it to be a more efficient tool for me to use when doing almost anything on my filesystem. However one thing has always bugged me: The inability to get mv to also move hidden files.

The solution

It turns out there is actually quite a simple way to get mv to also move hidden files that will work in both Mac OSX and Linux.  You simply change the shell options right before you use the move command, like so:

The above command will set the shell options to also include dotfiles in glob arguments (like the star argument we used above to move the files), and move the mv command (that we execute right after by chaining it with &&) will also include the dotfiles and everything else. So easy.

Rspec expect to change record value

I want my Ruby on Rails Rspec tests to be as clean and simple as possible. If there is a command in rspec that can test something in one simple line, then I do not want to use two lines.

Here is a real life example I came across today:

This will work, but it also uses two expect calls. I dont like this, since if the first one fails, we wont get to the second. It feels like we are testing two things at once. Luckily, there is a very clean way to test the Rspec expects to change the record value in one go:

Rspec expect to change record value, one liner

Now we are testing both the precondition (trashed == true) and the result (trashed == false) in one line. The important thing to note here is that the change with curly brackets is a lambda, that will get called before and after the lambda given as expect clause.

Multiple values

What about testing multiple values this way? Say we have two records, and we expect both of their values to change:

Now we are explicitly saying: “Both spe1 and spe2 should change their trashed? value from true to false”. Me like.


Easy way to copy one git branch to another local branch

At my work we use a couple of branches for deployment with CodeShip. When we push one of our ‘build’ branches, Codeship automatically starts a new build on it, and deploys if all tests pass. One of these is our QA branch. For this reason it is beneficial for us to have a quick workflow to make one of these branches behave like a dev branch we want to test-deploy. I think I have now found the easiest, fastest Git command for doing so:

The Git command to easily copy one git branch to another local branch

It is basically just ‘reverting’ the branch to another branch (in this case a remote one):

NOTE: This will OVERWRITE the branch you are copying to

If the branch you wish to copy is not remote, remove the origin/ part

Dependency Injection makes your Ruby and Rails methods easier to test

I’ve read a lot about Dependency Injection in Ruby and Rails lately, and thought I would do a quick post to showcase how Dependency Injection can really help you with even small methods. Such as testing the common current_user method in the ApplicationController:

Here is a quite simple method to authorize the current user by an Auth-Token variable, for API authentication.

Now if we want to test this method directly, we have no option but to try and override the request headers in our test. This gets real messy real fast.

The above code doesn’t even work, and I am left to mess with the internals of the request, just because I want to get a bloody Auth token. Why? There must be a better way. There is! Dependency Injection to the rescue. The only real dependency here (apart from the Sessions::Tracker), is the request headers Auth Token. So let’s inject that dependency instead:

Now the test becomes a piece of cake:

Of course we will also have to write tests for when sign in is succesful, but this should give you an idea of the power that is Dependency Injection

FIXED gem install debugger -v ’1.6.2′ No source for ruby

The problem: gem install debugger

I recently has to setup Ruby on a new machine, and got this error running gem install debugger -v ’1.6.2′:

The reason: debugger-ruby_core_source gem

The important line to note here is this:

Even though we are installing debugger -v ’1.6.2′, the error is in installing a requirement, debugger-ruby_core_source.

The fix: gem install debugger-ruby_core_source

It is actually quite easy to fix this error. Just run the below command, installing the dependencies, and retry:

Linking WordPress and Google Plus

Linking WordPress and Google Plus is extremely easy once you know how, and can add wonderful value to your blog, by linking your readers closer to you, the author. I know sign off on my blog posts with my friendly face next to them, and hopefully this serves as a way to assure my readers that I sign off on what I write. No auto-generated content here, my friends!

Anyway Linking WP and Google is so easy. Let’s do it:

Linking WordPress and Google Plus in Four Easy Steps

Prerequisites: Almost none, but I am assuming you know how to install plugins in your wordpress blog, and already have a Google Plus profile.

Step one

Install and activate the plugin “Google Plus Authorship”

Screen Shot 2013-06-07 at 11.31.55 AM

Step two

Find your Google Plus profile URL. You will find it by going to, signing in, and then clicking the little arrow next to your picture. Out will pop a small menu where you can click “Show profile”. Clicking this will take you to your profile, and you can get your profile URL from the address bar. Mine is

Screen Shot 2013-06-07 at 11.38.52 AM

Step three

Go to your profile page (Users -> Your profile). There are two places to enter Google plus profile URL here. One under “Contact Info”, the other under “Google Plus profile information”. Add your profile URL to both.

Screen Shot 2013-06-07 at 11.54.59 AM Screen Shot 2013-06-07 at 11.55.05 AM

Step four

Now we need to add a link from your Google Plus profile to your blog. On your profile page (that you now know the URL for) click “About”

Screen Shot 2013-06-07 at 11.45.19 AM


On the “About” page, find the “Links” section, and click to edit it.

Screen Shot 2013-06-07 at 11.46.29 AM


Where it says “contributor to”, add a new custom link, linking to the front page of your blog.

Screen Shot 2013-06-07 at 11.47.59 AM

All done

Presto! You have now successfully linked your WordPress and Google Plus, giving your blog that extra personal feel. If you would like to test it out, go to, and enter the URL of any one of your blog posts. You should see your pretty face next to it. Enjoy!

Screen Shot 2013-06-07 at 11.53.16 AM