CodeBlanks Logo Icon CodeBlanks

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.

Practice 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.

Starter code

terminal
// Ana çalışma dizinini tanımla

// Özellik çalışma dizinini tanımla

// İki yolu yazdır

Sample solution

terminal
const mainPath = "main-repo";
const featurePath = "feature-worktree";
console.log(mainPath);
console.log(featurePath);

Expected output

main-repo
feature-worktree

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the main advantage of using Git worktree?

  • Locking all files in the remote repository
  • Working on different branches in separate directories simultaneously Correct answer
  • Creating an independent Git repository for each branch
  • Automatically deleting commit history

Explanation: Worktree allows parallel work on different branches in multiple working directories of the same repository.

2 Identify the Worktree Command

Question
git ____
Answer
git worktree

Explanation: <code>git worktree</code> is the main command used to manage working tree operations.

3 Worktree Concepts

Term Meaning
Worktree Separate working directory
Ortak geçmiş Shared commit history
Paralel çalışma Working on multiple branches
İndeks The worktree's staging area

All lessons in Advanced