Git & GitHub Interview Question #125

What is git worktree?

Advanced GitHub Actions, Repository Internals, Submodules, LFS & Diagnostic Tools Senior Advanced

Quick Interview Answer

git worktree allows multiple working trees to be attached to one Git repository, making it possible to check out different branches in separate directories at the same time.

Detailed Explanation

git worktree allows multiple working trees to be attached to one Git repository, making it possible to check out different branches in separate directories at the same time.

This is useful when you need to keep a feature branch open while quickly working on a hotfix or reviewing another branch. It avoids repeatedly stashing or switching one working directory.

Code Example

# Add another working tree
git worktree add ../project-hotfix hotfix

# List working trees
git worktree list

# Remove when finished
git worktree remove ../project-hotfix