Git
git worktree
Learn multiple working trees with git worktree.
What is git worktree?
git worktree lets you create multiple working directories connected to the same Git repository. You can work on another branch without leaving your current changes behind. Each worktree has separate files, but they share Git history and repository data.
git worktree
git worktree list
For example, you can develop a feature in one branch while fixing a bug in another worktree.
How Are Worktrees Shared?
When you create a worktree, Git uses the main repository's shared history and object data. However, each worktree has its own working files and index. This means uncommitted changes in one directory do not directly modify the working files in another.
main-repo/
feature-worktree/
# Both directories share the repository
You cannot have the same branch checked out in two different worktrees simultaneously; assign a different branch to each worktree.
Exercise Describe Two Working Trees
Define variables representing two different working directories for the Git repository below. Let mainPath represent the main directory and featurePath represent the separate working directory for the feature branch. Finally, print both on separate lines using console.log.
Show example solution
const mainPath = "main-repo";
const featurePath = "feature-worktree";
console.log(mainPath);
console.log(featurePath);
Test yourself
-
What is the main advantage of using Git worktree?
- Locking all files in the remote repository
- Working on different branches in separate directories simultaneously
- Creating an independent Git repository for each branch
- Automatically deleting commit history
Show answer
Working on different branches in separate directories simultaneously — Worktree allows parallel work on different branches in multiple working directories of the same repository.
-
Identify the Worktree Command
git ____
Show answer
git worktree
-
Worktree Concepts
- Worktree
- Separate working directory
- Ortak geçmiş
- Shared commit history
- Paralel çalışma
- Working on multiple branches
- İndeks
- The worktree's staging area
Next lesson: worktree add