Git for a long time allowed to specify where to find the administrative area of the repository (the .git
directory) with the git --git-dir=<path> <command>
, or the GIT_DIR
environment variable, making it possible to work from the detached working directory.
To be able to reliably use multiple working directories sharing a single repository, we had to wait until version 2.5 of Git. With it, you can create a new linked work tree by using git worktree add <path> <branch>
, allowing us to have more than one branch checked out. For convenience, if you omit the <branch>
argument, then a new branch will be created based on the name of the created worktree.
This mechanism can be used instead of git stash
if you need to switch to a different branch (for example, to urgently fix a security bug), but your current working directory, and possibly also the staging area, is in a state of high disarray. Instead of disturbing it, you create a temporary linked working tree to make a fix, and remove it when done.
This is an evolving area—consult the Git documentation for more information.