Git Worktree

Git's git worktree command is a powerful feature that allows developers to work with multiple working trees concurrently, each representing a different branch or commit. This functionality simplifies parallel development, testing, and reviewing without the need for separate clones of a repository. We'll explore various ways to use the git worktree command, providing you with a comprehensive guide to enhance your Git workflow.

1. Creating a New Worktree:

To create a new worktree, use the following command:

bash
git worktree add -b <branch> <path> <start-point>
  • Replace <branch> with the name of the new branch.
  • Replace <path> with the path to the new worktree.
  • Replace <start-point> with the branch or commit you want to base the new worktree on.

For example:

bash
git worktree add -b feature-branch ../feature-branch master

This creates a new worktree in the ../feature-branch directory, based on the master branch.

2. Listing Worktrees:

To list all existing worktrees, you can use:

bash
git worktree list

This provides an overview of the existing worktrees, their paths, and the associated branches.

3. Removing a Worktree:

To remove a worktree, you can use:

bash
git worktree remove <path>

Replace <path> with the path to the worktree you want to remove.

For example:

bash
git worktree remove ../feature-branch
4. Checking Out a Branch in an Existing Worktree:

If you have an existing worktree and want to switch to a different branch, use:

bash
git worktree add -b <new-branch> <path> <start-point>
  • Replace <new-branch> with the name of the new branch.
  • Replace <path> with the path to the existing worktree.
  • Replace <start-point> with the branch or commit you want to base the new branch on.

For example:

bash
git worktree add -b bug-fix ../feature-branch hotfix-branch

This creates a new branch bug-fix in the existing worktree, based on the hotfix-branch.

5. Checking Out a Specific Commit:

To create a detached worktree based on a specific commit, use:

bash
git worktree add -b <branch> <path> <commit>
  • Replace <branch> with the name of the new branch.
  • Replace <path> with the path to the worktree.
  • Replace <commit> with the commit hash.

For example:

bash
git worktree add -b experiment ../experiment-branch abc123

This creates a new branch experiment in the existing worktree, based on the commit with hash abc123.

6. Pruning Stale Worktrees:

To remove worktrees that are no longer needed or have been manually deleted, you can use:

bash
git worktree prune

This command removes stale references to worktrees.

7. Customizing Worktree Locations:

By default, worktrees are created in the same directory as the main repository. You can customize the location by using the --worktree option:

bash
git worktree add --worktree=<custom-path> -b <branch> <path> <start-point>

Replace <custom-path> with the custom path where you want to create the worktree.

8. Working with Existing Branch in a Worktree:

If you want to work with an existing branch in a worktree without creating a new branch, you can use:

bash
git worktree add --detach <path> <start-point>

Replace <path> with the path to the worktree, and <start-point> with the branch or commit you want to base the worktree on.

9. Viewing Worktree Configuration:

To view the configuration related to worktrees, you can use:

bash
git config --get-regexp worktree

This provides information about various worktree-related configurations.

10. Linking Worktrees with Main Repository:

Changes made in a worktree are reflected in the main repository, and vice versa. If you want to link an existing worktree to the main repository, you can use:

bash
git worktree lock <path>

Replace <path> with the path to the worktree.

11. Unlocking a Worktree:

To unlock a previously locked worktree, you can use:

bash
git worktree unlock <path>

Replace <path> with the path to the worktree.

12. Renaming Worktree:

If you want to rename an existing worktree, you can use:

bash
git worktree move <old-path> <new-path>

Replace <old-path> with the current path of the worktree, and <new-path> with the desired new path.