git.worktree

Synopsis

git.worktree <repo> action: <enum(add|list|remove)> [name: value]...

Description

add creates a linked working tree in a directory that does not exist or is empty. The registration is written to .git/worktrees/<name>/ with the files HEAD, ORIG_HEAD, commondir, gitdir, index and logs/HEAD, as git worktree add writes them, and the new directory receives a .git file that points at the registration. The reflogs of HEAD and of a new branch are written as core.logAllRefUpdates asks, and not written when it is false. <name> is the last component of path; when the name is taken, the smallest number that makes it unique is appended (wt, wt1, wt2).

The branch is branch. Without branch and start_point it is the last component of path exactly as written, as in git worktree add; a component that is not a valid branch name (a space, ~, ^, :, .., a leading . or a trailing .lock) fails with E_GIT_REF (invalid_name), so pass branch. Without branch, a start_point that names a local branch checks that branch out, and any other start_point is checked out detached: HEAD holds the commit and the record has detached: true. An existing branch is checked out and must not be checked out in another working tree. A missing branch is created at start_point, or at HEAD, with the log message branch: Created from <start_point>. The files of the commit are written to the new directory and recorded in its index. Content filter drivers are not run.

list returns the main working tree first and then the linked working trees sorted by path, with the lock reason and whether the directory is missing (prunable). It gives the same result from any working tree of the repository.

remove deletes a linked working tree and its registration. A working tree with modified, staged or untracked files that are not ignored is removed only with force: true. Ignored files do not block the removal. The branch is not deleted.

Parameters

Parameter Type Default Description
repo path required Repository or path inside it.
action enum(add|list|remove) required Operation on working trees.
path path none Directory of the working tree to add or remove.
branch text none Branch checked out in the new working tree.
start_point text none Revision the new branch starts from.
force bool false Remove a working tree that has uncommitted changes.
checkpoint bool false Create a checkpoint of the changes before remove.
expect_state text none State token from an earlier read; nothing changes when the repository state differs.

Input and output

Form Input Output
default none git_worktree, effect

Effect

Property Value
Category change
Effect local
Reversibility reversible
Implementation builtin
Reversibility conditions Remove moves the working tree directory into the journal before unregistering it.
Action Effect Reversibility Note
list read read

Examples

Add a working tree with a new branch.

git.worktree "/home/user/repo" action: add path: "/home/user/repo-hotfix" branch: "hotfix/1.2" start_point: "v1.2.0"
git_worktree path: /home/user/repo-hotfix branch: hotfix/1.2 main: false

List the working trees of a repository.

git.worktree "/home/user/repo" action: list
git_worktree path: /home/user/repo branch: main main: true
git_worktree path: /home/user/repo-hotfix branch: hotfix/1.2 main: false prunable: false

Remove a working tree that still has local changes.

git.worktree "/home/user/repo" action: remove path: "/home/user/repo-hotfix" force: true

Errors

Code Meaning Typical fix
E_MISSING_PARAM action or, with add and remove, path is missing. Pass path
E_GIT_WORKTREE The directory exists and is not empty (path_exists), a registration names it (already_registered), the path is the main working tree (main_worktree), no working tree is registered there (not_found), or the working tree is locked (locked). git.worktree "/home/user/repo" action: list; remove the locked file of the registration
E_GIT_REF The branch is checked out in another working tree (checked_out_elsewhere), start_point is given for an existing branch (branch_exists), or the branch name is invalid (invalid_name). Pass another branch
E_GIT_REVISION start_point, or HEAD of a repository without commits, names no commit. Pass an existing revision as start_point
E_GIT_DIRTY The working tree to remove has modified, staged or untracked files (local_changes). git.worktree "/home/user/repo" action: remove path: "/home/user/repo-hotfix" force: true
E_GIT_UNSUPPORTED The commit contains an unsafe path (unsafe_path). Check out another revision
E_GIT_LOCKED The lock file of the new branch exists. Wait for the other process or remove the stale lock file
E_STALE The branch, the directory or the registration appeared between planning and writing. Run the command again

git.switch, git.branch, git.checkpoint, undo.

Source file on GitHub: docs/commands/git.worktree.md