* added worktree.md * fixed quatation issue --------- Co-authored-by: Anonymous-025 <unknownjesvin>
2.3 KiB
How to Use git worktree Safely
📑 Table of Contents
git worktree allows you to have multiple working directories linked to a single Git repository. This is useful when you need to work on multiple branches simultaneously without switching branches in the same directory.
Step 1: Check Existing Worktrees
To see all active worktrees in your repository, use:
git worktree list
This will output a list of worktrees with their paths and branches.
Step 2: Create a New Worktree
To create a new worktree for a branch, run:
git worktree add <path> <branch>
Example:
git worktree add ../feature-branch feature
This creates a new directory ../feature-branch/ and checks out the feature branch inside it.
If the branch does not exist, add -b to create it:
git worktree add -b new-feature ../new-feature-branch
Step 3: Remove a Worktree
To remove a worktree (detach it from the repository), first remove the directory manually, then prune it:
rm -rf <worktree-path>
git worktree prune
Example:
rm -rf ../feature-branch
git worktree prune
Step 4: Switch Between Worktrees
Simply change directories to the worktree you want to work in:
cd ../feature-branch
You can now work on this branch independently of the main repository directory.
###🔹 Detach a Worktree Without Deleting It
git worktree remove <worktree-path>
Example:
git worktree remove ../feature-branch
Step 5: Use Worktrees for Temporary Fixes
You can use worktrees to quickly fix bugs on a different branch without switching from your main working directory:
git worktree add ../hotfix hotfix-branch
cd ../hotfix
# Apply fix
git commit -am "Fixed urgent bug"
git push origin hotfix-branch
cd ../main-repo
git worktree remove ../hotfix
``