Worktree
Git worktrees make branches directories at the root of the repository. Instead of switching branches, just switch directories.
Make a directory
mkdir <repository-name>
Clone a repository without checking out a branch
Clone the repository from inside the repository directory:
cd <repository-name>
After that clone the repository without worktrees: (The --bare
flag specifies
to only clone the essential git files)
git clone --bare <git-remote-url>
<git-remote-url>
can be an HTTPS or SSH URL.
Add a worktree
Only add worktrees from the bare repository, so navigate to the bare repository:
cd <repository-name>/.git/
Now add a worktree to the root of the repository:
git worktree add ../<branch-name>
Now the directory structure should look something like this (as an example this repository has been used):
digital-garden
├── digital-garden/.git/
└── main/
To switch to branch main
, simply cd main
.
Add a worktree with a new branch
Make sure to be in the bare repository when managing worktrees:
git worktree add -b <branch-name> ../<worktree-name>
<branch-name>
is the name of the new branch.<worktree-name>
is the name of directory (worktree).
Add a worktree based on a remote branch
git worktree add <worktree-name> <remote-branch-name>
When the worktree commit log does not line up with the
remote commit log for that specific branch, it is probably because
<remote-branch-name>
was not the correct remote branch name when executing the
git worktree add
command.
Remove a worktree
Remove worktrees from the bare repository:
cd <repository-name>/.git/
Remove a worktree by executing the following command:
git worktree remove <worktree-name>
See also
- For a full reference to worktrees see the official Git worktree documentation.