What would I use git-worktree for?
One obvious use is to simultaneously compare the behavior (not source) of different versions - for example different versions of a web site or just a web page.
I tried this out locally.
create a directory
page1
.inside create the directory
src
andgit init
it.in
src
createpage1.html
with a little content and commit it.$ git branch ver0
$ git worktree add ../V0 ver0
in
src
master add more text topage1.html
and commit it.$ git branch sty1
edit
page1.html
in thesty1
branch (add some distinctive CSS style) and add commit it.$ git worktree add ../S1 sty1
You can now use a web browser to open and view these 3 versions simultaneously:
..\page1\src\page1.html
// whatever git has as current..\page1\V0\page1.html
// the initial version..\page1\S1\page1.html
// the experimentally styled version
For me, git worktree is the biggest improvement since a long time. I'm working in enterprise software development. There, it is very common that you have to maintain old versions like what you released 3 years ago. Of course you have a branch for each version so that you can easily switch to it and fix a bug. However, switching is expensive, because in the meantime you completely restructured the repository and maybe build system. If you switch, your IDE will run mad trying to adapt the project settings.
With worktree, you can avoid that constant reconfiguration. Checkout those old branches in separate folders using worktree. For each branch, you got an independent IDE project.
Of course this could have been done in the past by cloning the repo several times and this has been my approach so far. However, that also meant wasting hardrive space and worse needing to fetching the same changes from the repo several times.
I can see some uses for this.
If you have a test suite that runs for a long time, imagine hours, and you start it it effectively blocks that working copy until the tests are completed. Switching branches during those tests would break them in ways that would be hard to understand.
So with git-worktree
I could have a second idea launched for another branch doing work there.
Also, when I switch to some other branch to do some quick investigation my IDE thinks a lot of files suddenly changed and will index all those changes, just to have to re-index them again when I'm switching back.
A third use case would be to do file comparison using other tools than git-diff
, like normal diff
, between two directories instead if two branches.