worktree¶
For git-worktree(1).
Overview¶
Manage git worktrees using GitWorktreeManager (collection-level)
and GitWorktreeCmd (per-worktree operations).
Example¶
from libvcs.cmd.git import Git
git = Git(path='/path/to/repo')
# List all worktrees
worktrees = git.worktrees.ls()
# Add a new worktree
git.worktrees.add(path='/path/to/worktree', branch='feature-branch')
# Get a specific worktree and operate on it
wt = git.worktrees.get(worktree_path='/path/to/worktree')
wt.lock(reason='Do not delete')
wt.unlock()
wt.remove()
# Prune stale worktrees
git.worktrees.prune()
API Reference¶
Bases:
objectTraverse and manage git worktrees with ORM-like filtering via QueryList.
Wrap some of git-worktree(1), manager.
Examples
>>> GitWorktreeManager(path=tmp_path) <GitWorktreeManager path=...>
>>> GitWorktreeManager(path=tmp_path).run('list') 'fatal: not a git repository (or any of the parent directories): .git'
>>> len(GitWorktreeManager(path=example_git_repo.path).run('list')) > 0 True
Run a command against a git repository’s worktrees.
Wraps git worktree.
Examples
>>> len(GitWorktreeManager(path=example_git_repo.path).run('list')) > 0 True
Add a new worktree.
- Parameters:
path (StrPath) – Path for the new worktree.
commit_ish (str | None) – Commit/branch to checkout in the new worktree.
force (bool | None) – Force creation even if path already exists.
checkout (bool | None) – Checkout commit after creating worktree.
reason (str | None) – Reason for locking (requires lock=True).
new_branch_force (str | None) – Force create a new branch (-B).
guess_remote (bool | None) – Guess remote tracking branch.
log_in_real_time (bool)
- Return type:
Examples
>>> GitWorktreeManager(path=example_git_repo.path).add( ... path='/tmp/test-worktree-add', commit_ish='HEAD' ... ) "Preparing worktree (detached HEAD ...)..."
Prune worktree information.
Examples
>>> GitWorktreeManager(path=example_git_repo.path).prune() ''
>>> GitWorktreeManager(path=example_git_repo.path).prune(dry_run=True) ''
List worktrees (raw output).
Examples
>>> len(GitWorktreeManager(path=example_git_repo.path)._ls()) >= 1 True
List worktrees.
Returns a QueryList of GitWorktreeCmd objects.
- Parameters:
- Return type:
Examples
>>> worktrees = GitWorktreeManager(path=example_git_repo.path).ls() >>> len(worktrees) >= 1 True
Get worktree via filter lookup.
Examples
>>> worktrees = GitWorktreeManager(path=example_git_repo.path).ls() >>> if len(worktrees) > 0: ... wt = GitWorktreeManager(path=example_git_repo.path).get( ... worktree_path=worktrees[0].worktree_path ... ) ... wt is not None ... else: ... True True
- Parameters:
- Return type:
Bases:
objectRun git commands targeting a specific worktree.
Lite, typed, pythonic wrapper for a git-worktree(1) entry.
Examples
>>> GitWorktreeCmd( ... path=example_git_repo.path, ... worktree_path='/tmp/example-worktree', ... ) <GitWorktreeCmd path=... worktree_path=/tmp/example-worktree>
Run command against a git worktree.
Wraps git worktree.
Remove this worktree.
Examples
>>> GitWorktreeCmd( ... path=example_git_repo.path, ... worktree_path='/tmp/nonexistent-worktree', ... ).remove() "fatal: '/tmp/nonexistent-worktree' is not a working tree"
Lock this worktree.
Examples
>>> GitWorktreeCmd( ... path=example_git_repo.path, ... worktree_path='/tmp/nonexistent-worktree', ... ).lock() "fatal: '/tmp/nonexistent-worktree' is not a working tree"
Unlock this worktree.
Examples
>>> GitWorktreeCmd( ... path=example_git_repo.path, ... worktree_path='/tmp/nonexistent-worktree', ... ).unlock() "fatal: '/tmp/nonexistent-worktree' is not a working tree"
Move this worktree to a new location.
Examples
>>> GitWorktreeCmd( ... path=example_git_repo.path, ... worktree_path='/tmp/nonexistent-worktree', ... ).move('/tmp/new-worktree') "fatal: '/tmp/nonexistent-worktree' is not a working tree"
Repair this worktree.
Examples
>>> result = GitWorktreeCmd( ... path=example_git_repo.path, ... worktree_path='/tmp/nonexistent-worktree', ... ).repair() >>> result == '' or 'error' in result True