Saturday, October 01, 2011

gitfall#1: Falling off a branch

Ever had a merge fail with a fatal: git write-tree failed to write a tree message out of the blue?

It sounds terrifying, but when I got the root cause is quite mundane: file name conflicts in the merging commits that git is not smart enough to figure out without help. And when you fixup your merge, you are left with a commit that's lost one of its parents ("falling off a branch").

If you do much file reorganisation in a project with branches, it turns out this can be quite common (had it a few times on a recent project).

In an attempt to understand exactly what was going on, I put together the steps needed to reproduce and recover from the error. I've tidied these up and made it a full "tutorial/demo" script. You can find it in a repo called gitfalls - in the expectation that there are many more git curiosities and idiosynchrasies worth a similar treatment. Enjoy!

The script not only shows how to create the error, but two ways of resolving it and the "lost parent branch" issue:
  1. Merge again after fixing the first failed commit. Duh!
  2. Going a bit deeper and using git commit-tree to manufacture a new merge commit with the correct parentage

Lessons learned form all of this? Perhaps:
  1. Avoid reorganising folder structures using folder names that once were used by files (or vice versa)
  2. If you must do such a reorganisation, immediately merge or cherry-pick to other active branches if you can. This avoids laying a trap for a co-worker to hit later on.

Hope you enjoy the script, and if you have any others to contribute please be my guest!

Blogarhythm: Fall Out - The Police