Worktrees represent an alternative location to checkout a branch into. Rather
than checking out a branch in your main working tree (which changes the
branch you are currently on and forces you to stash any existing work), you
can instead check that branch out into a separate linked worktree with its
own working tree. Practically, a worktree is just a separate folder that a
branch is checked out into, with some extra git metadata that links it back
to the main working tree.
git_worktree_list() returns a data frame of information about the worktrees
linked to the main working tree.
git_worktree_exists() lets you check whether or not a worktree by the name
of name exists for this repo.
git_worktree_path() returns the file path to the worktree.
git_worktree_add() creates a new worktree called name in the folder
pointed to by path, and checks branch out into it.
git_worktree_remove() removes a worktree. It does so by deleting the folder
provided as the path to git_worktree_add(), and then cleaning up some git
metadata in the main working tree that linked the main working tree to the
removed worktree. The branch checked out by the worktree is not deleted.
Note that this is just a wrapper around git_worktree_prune() that sets some
desirable defaults for aggressive removal.
git_worktree_prune() is more cautious than git_worktree_remove(). It
refuses to prune valid or locked worktrees by default, and also refuses
the delete the working tree of the worktree by default (i.e. the folder at
path). It is automatically run by git itself on periodic intervals to prune
outdated worktrees. For interactive usage, you typically want
git_worktree_remove() instead. git_worktree_is_prunable() lets you check
if a worktree is prunable with the given options.
git_worktree_lock(), git_worktree_unlock(), and
git_worktree_is_locked() help you manage whether or not a worktree is
locked. When a worktree is locked, it is not automatically cleaned up by
git_worktree_prune() (and git itself) on periodic intervals, even when it
looks invalid. This is typically only useful when your worktree is on a
hard drive that isn't always connected (which can make it look invalid when
disconnected, typically making it a candidate for automatic pruning).
git_worktree_is_valid() checks whether a worktree is valid or not. A
valid worktree requires both the git data structures inside the main
working tree and this worktree to be present.