Git worktrees, a powerful feature that allows you to have multiple working directories for a single Git repository. With worktrees, you can check out multiple branches simultaneously, making it easier to work on different features or test changes side-by-side without needing to clone the entire repository multiple times.
A worktree in Git is an additional working directory associated with a single repository. This directory is a fully functioning Git repository on its own, complete with its own branch, staging area, and working directory. Git worktrees allow you to work on multiple branches simultaneously in the same repository, making them ideal for handling various features, testing, and deployments without switching branches repeatedly.
.git
directory, making them lightweight.Using worktrees offers several benefits:
.git
directory, they are faster and consume less storage than multiple clones.To create a new worktree, you can use the git worktree add
command. Let’s go through the steps:
First, navigate to your Git repository if you aren’t already in it.
cd /path/to/your/repo
Use the git worktree add
command to create a worktree and specify the directory path and the branch you want to check out.
git worktree add /path/to/new-worktree branch_name
If the branch doesn’t exist yet, Git will create it automatically in the new worktree.
git worktree add ../feature-worktree feature-branch
../feature-worktree
: The directory for the new worktree.feature-branch
: The branch checked out in this new worktree.
Preparing worktree (checking out 'feature-branch')
HEAD is now at [commit hash] [commit message]
In this example, a new folder named feature-worktree
is created in the parent directory, with feature-branch
checked out.
Switching between worktrees is straightforward. Since each worktree has its own directory and branch, you can simply navigate to the directory of the worktree you want to work in:
cd /path/to/feature-worktree
Each worktree functions as a standalone Git repository, so commands like git status
, git add
, and git commit
will work independently within each worktree.
To remove a worktree, you need to detach it from the main repository. Be sure you’re done with any changes in that worktree, as they may be lost.
1. Remove the Worktree Directory: Use the git worktree remove
command along with the directory path.
git worktree remove /path/to/feature-worktree
2. Delete the Directory (Optional): Git will no longer track the directory as a worktree, but you may also delete it if you no longer need it.
rm -rf /path/to/feature-worktree
Explanation: Removing a worktree unregisters it from the main repository. After deletion, you can no longer use it as a Git workspace.
You can create a worktree for a specific commit (detached HEAD) rather than a branch:
git worktree add /path/to/detached-worktree [commit_hash]
This creates a worktree pointing to the specified commit, allowing you to explore the project state at that specific point in time.
To change the branch within an existing worktree, navigate to the worktree and check out a new branch:
cd /path/to/worktree
git checkout another-branch
To view all active worktrees associated with your repository, use:
git worktree list
/path/to/repo [main]
/path/to/feature-worktree [feature-branch]
This displays all active worktrees and the branches they currently have checked out.
Here’s a summary of common worktree commands for easy reference:
Command | Description |
---|---|
git worktree add | Adds a new worktree with a specified branch. |
git worktree list | Lists all active worktrees. |
git worktree remove | Removes the specified worktree. |
git checkout | Switches branches within a worktree. |
git branch -d | Deletes a branch (only if it’s not currently checked out in any worktree). |
feature-x-worktree
.git worktree list
to avoid accidental deletion or data loss.Git worktrees provide a versatile, efficient way to manage multiple working directories within a single repository, empowering developers to work on multiple branches simultaneously without duplication. With worktrees, you can streamline feature development, testing, and deployment processes. Happy Coding!❤️