commit: add a function to create a commit on top of a ref

Add a way to safely create a commit which builds on top of the current
commit in a given reference.