Git worktrees, a feature that allows users to manage multiple working directories from a single Git repository. Worktrees enable you to have multiple branches checked out simultaneously, allowing for efficient multi-tasking and smoother development workflows.
A Git worktree is an additional working directory associated with a single repository. Unlike traditional setups where you would create a new clone for each workspace, a worktree allows you to manage multiple branches independently within one repository, making it highly efficient.
.git
directory of the repository.Git worktrees offer several key benefits, such as:
Imagine you’re working on a large project and need to fix a bug on the main
branch while still working on a new feature in feature-branch
. With worktrees, you can check out main
in a separate directory to handle the bug fix without leaving your feature development work.
To create a worktree, use the git worktree add
command, followed by the path to the new worktree directory and the branch you want to check out.
git worktree add /path/to/new-worktree branch_name
git worktree add ../feature-branch-worktree feature-branch
feature-branch-worktree
.feature-branch
is checked out within this new directory.
Preparing worktree (checking out 'feature-branch')
HEAD is now at
This output confirms that the feature-branch
has been checked out in the new worktree directory.
To list all active worktrees, use:
git worktree list
/path/to/repo [main]
/path/to/feature-branch-worktree [feature-branch]
This output displays all active worktrees and the branches currently checked out in each.
Here are the main commands you’ll use for managing worktrees:
git worktree add
git worktree list
git worktree remove
Detach a worktree (i.e., working on a detached HEAD):
git worktree add
Sometimes, you may want to inspect a specific commit without creating a new branch. You can create a worktree for a detached HEAD by checking out a specific commit.
git worktree add /path/to/detached-worktree
git worktree add ../v1.0-checkout abcd123
In this example, a worktree named v1.0-checkout
is created, checked out to the commit with hash abcd123
. This is useful for testing or reviewing specific commit states.
Explanation: Detached HEAD means you’re not on any branch, just on a snapshot of a particular commit. This lets you inspect the state of the project at that commit without creating a new branch.
To switch between worktrees, simply navigate to the directory of the worktree you want to work in:
cd /path/to/worktree
Since each worktree has its own branch checked out, commands like git status
, git add
, and git commit
work independently in each worktree.
To remove a worktree, use the git worktree remove
command:
git worktree remove /path/to/worktree
This command removes the worktree from Git’s tracking but does not delete the directory. You can delete the directory if you no longer need it.
git worktree remove ../feature-branch-worktree
rm -rf ../feature-branch-worktree
/path/to/feature-branch-worktree removed
Many developers wonder how worktrees differ from cloning. Here’s a comparison:
Feature | Worktrees | Cloning |
---|---|---|
Repository Setup | Shares the main repository’s .git directory | Each clone has its own .git directory |
Storage | Lightweight, no duplicate repository storage | Requires full duplicate of the repository |
Branch Management | Multiple branches in parallel | Each clone typically has its own branch |
Switching Costs | Fast and isolated | Switching requires separate directories |
Worktrees are ideal when working on multiple branches within the same repository. Cloning is better when you need complete isolation or different Git configurations.
feature-x-worktree
.git worktree remove
to clean up unused worktrees and keep your environment organized.git worktree list
regularly to monitor active worktrees.Cherry-picking in Git is an incredibly useful tool for selectively applying changes across branches without merging or rebasing. It's beneficial for applying bug fixes or minor improvements, especially in large projects with multiple branches. While powerful, cherry-picking should be used carefully to avoid confusion and unnecessary conflicts. Happy Coding!❤️