worktree¶
For git-worktree(1).
Overview¶
Manage git worktrees using GitWorktreeManager (collection-level)
and GitWorktreeCmd (per-worktree operations).
Examples¶
Add a worktree checked out at HEAD, then look it up — the main worktree
always exists, so the list grows to two:
>>> from libvcs.cmd.git import Git
>>> git = Git(path=example_git_repo.path)
>>> git.worktrees.add(path=tmp_path / 'example-worktree', commit_ish='HEAD')
'HEAD is now at ...'
>>> worktrees = git.worktrees.ls()
>>> len(worktrees) >= 2
True
>>> wt = git.worktrees.get(worktree_path=worktrees[0].worktree_path)
>>> wt.worktree_path == worktrees[0].worktree_path
True
Prune stale worktree metadata:
>>> from libvcs.cmd.git import Git
>>> git = Git(path=example_git_repo.path)
>>> git.worktrees.prune()
''
API Reference¶
Wrap some of git-worktree(1), manager.
Examples
>>> GitWorktreeManager(path=tmp_path) <GitWorktreeManager path=...>
>>> GitWorktreeManager(path=tmp_path).run('list', trim=True) '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
QueryListofGitWorktreeCmdobjects.- 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(trim=True) "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(trim=True) "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(trim=True) "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', trim=True) "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