Some thoughts:
1. Code examples are the biggest problem to me in the tools that I tried. There's many little things about them, that need to look right - line numbers, display on kindle, line breaks
2. At the beginning, I've used Git and vim for writing in Markdown. However, at some point I realised that I wasn't efficient - I switched to Scrivener [2] and dropped the idea of using Git. In my case, it made me much more productive.
3. Writing an ebook is different than writing code. You probably don't need much of the history.
4. Research is for me the biggest part of writing, experimenting with different ideas, collecting code samples. It's good to have a proper tool to support it. In my case, Scrivener [2] was a huge improvement.
5. I use a combination of Scrivener, Leanpub, Dropbox and getdpd [3] for the whole project. Scrivener for research and notes organisation, Leanpub for generating the result files (PDF, mobi, epub), Dropbox for syncing those two. Getdpd for the selling part.
[1] http://rails-refactoring.com/