Using Joe Armstrong's method for re-writing from scratch with git-worktrees

Joe Armstrong used a simple principle when programming: "Throw away the first implementation, you didn't actually understand the problem when you wrote it."1. In my own work, I've developed a way to start from scratch while keeping the multiple histories of the project linked together. These are the steps:

Create the following folder structure to get started.

For example, here we are developing the automerge-clj project:

mkdir -p [work-dir]/automerge-clj/v1/
cd !$
clojure -Tnew lib :name me.vedang/automerge-clj
cd automerge-clj
git init

The clojure new command creates another automerge-clj directory inside our v1 directory. This is going to be the master repo that will drive our prototyping.

Create an Initial commit of the bare-minimum skeleton.

This gives us a good starting point to come back to.

Now we make as many commits in automerge-clj/v1/automerge-clj as we want.

Once we feel the need to start a new prototype

We use git worktree to create a new "place" to start from:

cd [work-dir]/automerge-clj/v1/automerge-clj
git worktree add ../../v2
cd ../../v2 # Here, git will have automatically created a new branch for us, called v2.

We need to create an orphaned branch at the starting point of the repo in order to start from scratch again. We can also do a git reset --hard with the rev-list command to start from a previous commit.

To start from scratch, discarding even the initial commit:

git checkout --orphan auto-v2
git rm -rf .

To reset the current branch to a previous commit

git reset --hard $(git rev-list HEAD | tail -n 1)

Caveat: Note that it's v1/automerge-clj/deps.edn (for example) vs just v2/deps.edn. This is a gotcha but I don't bother working around it. I find I get used to it almost immediately.

Cleaning up old versions

When done with prototypes, or when you want to clean up space, you can just:

# from the "main" v1/project directory
git worktree list
git worktree remove path


1 See: Joe Armstrong on Writing Code for the context of the quote

Published On: Mon, 20 Jun 2022. Last Updated On: Thu, 23 May 2024.