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¶
- class libvcs.cmd.git.GitWorktreeManager(*, path, cmd=None)[source]¶
Bases:
objectTraverse and manage git worktrees with ORM-like filtering via QueryList.
Wrap some of git-worktree(1), manager.
- Parameters:
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
- __init__(*, path, cmd=None)[source]¶
Wrap some of git-worktree(1), manager.
- Parameters:
- Return type:
None
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
-
path:
Path¶ Directory to check out
- run(command=None, local_flags=None, *, log_in_real_time=False, check_returncode=None, **kwargs)[source]¶
Run a command against a git repository’s worktrees.
Wraps git worktree.
- Return type:
- Parameters:
Examples
>>> len(GitWorktreeManager(path=example_git_repo.path).run('list')) > 0 True
- add(path, *, commit_ish=None, force=None, detach=None, checkout=None, lock=None, reason=None, new_branch=None, new_branch_force=None, orphan=None, track=None, guess_remote=None, log_in_real_time=False, check_returncode=None)[source]¶
Add a new worktree.
- Parameters:
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)
check_returncode (bool | None)
- Return type:
Examples
>>> GitWorktreeManager(path=example_git_repo.path).add( ... path='/tmp/test-worktree-add', commit_ish='HEAD' ... ) "Preparing worktree (detached HEAD ...)..."
- prune(*, dry_run=None, verbose=None, expire=None, log_in_real_time=False, check_returncode=None)[source]¶
Prune worktree information.
- Parameters:
- Return type:
Examples
>>> GitWorktreeManager(path=example_git_repo.path).prune() ''
>>> GitWorktreeManager(path=example_git_repo.path).prune(dry_run=True) ''
- _ls(*, verbose=None)[source]¶
List worktrees (raw output).
Examples
>>> len(GitWorktreeManager(path=example_git_repo.path)._ls()) >= 1 True
- ls(*, verbose=None)[source]¶
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(*args, **kwargs)[source]¶
Get worktree via filter lookup.
- Return type:
- Parameters:
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
- class libvcs.cmd.git.GitWorktreeCmd(*, path, cmd=None, worktree_path, head=None, branch=None, locked=False, prunable=False)[source]¶
Bases:
objectRun git commands targeting a specific worktree.
Lite, typed, pythonic wrapper for a git-worktree(1) entry.
- Parameters:
Examples
>>> GitWorktreeCmd( ... path=example_git_repo.path, ... worktree_path='/tmp/example-worktree', ... ) <GitWorktreeCmd path=... worktree_path=/tmp/example-worktree>
- __init__(*, path, cmd=None, worktree_path, head=None, branch=None, locked=False, prunable=False)[source]¶
Lite, typed, pythonic wrapper for a git-worktree(1) entry.
- Parameters:
- Return type:
None
Examples
>>> GitWorktreeCmd( ... path=example_git_repo.path, ... worktree_path='/tmp/example-worktree', ... ) <GitWorktreeCmd path=... worktree_path=/tmp/example-worktree>
-
path:
Path¶ Directory to check out
- run(command=None, local_flags=None, *, log_in_real_time=False, check_returncode=None, **kwargs)[source]¶
Run command against a git worktree.
Wraps git worktree.
- remove(*, force=False, log_in_real_time=False, check_returncode=None)[source]¶
Remove this worktree.
- Parameters:
- Return type:
Examples
>>> GitWorktreeCmd( ... path=example_git_repo.path, ... worktree_path='/tmp/nonexistent-worktree', ... ).remove() "fatal: '/tmp/nonexistent-worktree' is not a working tree"
- lock(*, reason=None, log_in_real_time=False, check_returncode=None)[source]¶
Lock this worktree.
- Parameters:
- Return type:
Examples
>>> GitWorktreeCmd( ... path=example_git_repo.path, ... worktree_path='/tmp/nonexistent-worktree', ... ).lock() "fatal: '/tmp/nonexistent-worktree' is not a working tree"
- unlock(*, log_in_real_time=False, check_returncode=None)[source]¶
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(new_path, *, force=False, log_in_real_time=False, check_returncode=None)[source]¶
Move this worktree to a new location.
- Parameters:
- Return type:
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"