CodeBlanks Logo Icon CodeBlanks

worktree add

Learn to add a new tree with git worktree add.

git worktree add

git worktree add adds a new working directory to an existing Git repository. The first argument is the path of the new directory, and the second argument is the branch to use there. This lets you start working on another branch without changing your current working directory.

git worktree add ../feature feature

# The feature branch is checked out in ../feature

The Relationship Between Branch and Path

When adding a worktree, you can specify both the directory path and branch name. For example, ../hotfix is the new working directory, while hotfix is the branch to use. If the specified branch does not exist, you can create a new one with the -b option.

git worktree add ../hotfix hotfix

git worktree add -b new-feature ../new-feature

The first command uses an existing branch, while the second creates a new branch and attaches it to the worktree.

Practice Build a New Worktree Command

Assign a Git command that adds a new worktree to the command variable. Use ../testing as the new directory and testing as the branch. Print the command with console.log.

Starter code

terminal
// Worktree ekleme komutunu tanımla
const command = "";

// Komutu yazdır

Sample solution

terminal
const command = "git worktree add ../testing testing";
console.log(command);

Expected output

git worktree add ../testing testing

Questions and answers

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

1 What does the first argument specify when adding a worktree?

  • The path of the new working directory Correct answer
  • The remote repository URL
  • The commit message to create
  • The name of the branch to delete

Explanation: In <code>git worktree add</code>, the first argument specifies the path where the new worktree will be created.

2 Add a New Worktree

Question
git worktree ____ ../feature feature
Answer
git worktree add ../feature feature

Explanation: <code>add</code> creates a new worktree at the specified path and uses the specified branch.

3 Worktree Add Arguments

Term Meaning
../feature New directory path
feature Branch to use
-b Creates a new branch
add Adds a working tree

All lessons in Advanced