worktree
The git worktree
command allows you to manage multiple working directories attached to a single Git repository. This is useful for working on different branches simultaneously without having to switch between them in a single directory.
Detailed Explanation
Creating a Worktree: You can create a new worktree linked to a specific branch or commit. This creates a new directory where you can work on that branch or commit independently of the main working directory.
Listing Worktrees: You can list all existing worktrees associated with the repository.
Removing a Worktree: You can remove a worktree when it is no longer needed.
Pruning Worktrees: You can prune worktrees to remove references to directories that no longer exist.
Examples
Creating a New Worktree for a Branch:
git worktree add ../new-worktree-branch feature-branchThis command creates a new worktree in the
../new-worktree-branch
directory and checks out thefeature-branch
in that directory.Creating a New Worktree for a Specific Commit:
git worktree add ../new-worktree-commit <commit-hash>This command creates a new worktree in the
../new-worktree-commit
directory and checks out the specified commit.Listing All Worktrees:
git worktree listThis command lists all worktrees associated with the repository, showing their paths and the branches or commits they are checked out to.
Removing a Worktree:
git worktree remove ../new-worktree-branchThis command removes the worktree located at
../new-worktree-branch
. Note that you need to manually delete the directory if it still exists.
These commands help you manage multiple working directories efficiently, allowing you to work on different branches or commits simultaneously.