When working on software, it is quite common to work on multiple branches in parallel. But sometimes, you have a long build job running, and you can't checkout a different branch to work on something else in the meantime, as that could impact the build.
This happens to me all the time when working on the freedesktop SDK project, as builds can take a significant amount of time. Until recently I'd just get a new Git clone, and work in there. This was annoying, because suddenly I had two clones to keep up to date.
Recently, Federico mentioned Git worktrees and I figured I'd give it a try.
Here's how I now use them for the Freedesktop SDK project:
-
I have the primary clone at
~/Projects/desktop/freedesktop-sdk/18.08
.That's because the 18.08 branch is currently the primary branch on which we work.
-
I have a Git alias:
[alias] workon = "!sh -c \"git worktree add --guess-remote -b $1 ${PWD%$(git rev-parse --abbrev-ref HEAD)}$1\" -"
-
In any workspace, I can decide to work on a new branch with:
$ git workon branch-name
That will create a working tree at
~/Projects/desktop/freedesktop-sdk/branch-name
.
Since we usually name our branches as user-name/branch-name
, that currently
gives me the following trees:
freedesktop-sdk/
18.08/
benbrewer/bootstrap-from-git/
bochecha/gnome-updates/
bochecha/libglvnd-1.1.0/
bochecha/meson/
bochecha/smaller-llvm/
bochecha/updates/
ikerperez/parted-from-git/
This is amazing, now I can work on a branch while another branch is building, but I only have a single repository to manage.