Using Git worktrees

Switching between git branches can be cumbersome. They usually require a full build and there are lots of things that can go wrong. The past 10 years I regularly have 10 workspaces open at the same time, but they were on different bnd/git workspaces.

Of course it is possible to have different git workspaces on the same repository. However, that is easy to get confused. Recently I learned about git worktrees. They share one Git repository, well the .git/ directory, but allow you to get the files in a different directory.

 master $ git worktree add ../branch feature/branch

Git tracks these worktrees and will warn you when you want to checkout the same branch in different places. I find it extremely useful.

2 Likes

Peter, this is a fantastic tip, has saved me a lot of grief.

Only drawback is that many git GUIs (including Eclipse) don’t know about this :frowning: However, the command line works fine.