Checkout support.

This is based on #125 with some changes because the reference resolution was wrong.

It allows both references and commits to be checked out.