Git Worktrees
Table of Contents
Official documentation
https://git-scm.com/docs/git-worktree
Summary
Worktrees are beneficial in a git workflow for several reasons:
- The
.git
folder is outside of the folder where the user works (A default clone has the repository objects in the “.git” folder, which is inside the work folder). In a separate worktree, there is less chance to accidentally modify or delete something in “.git”, which usually causes the repository to be corrupted and unrecoverable. - With worktrees, the user can have multiple branches checked out at once (A default clone has only one branch checked out in the only work folder). See Branching Strategies for examples of how this enables advanced workflows.
Examples
List Worktrees
git worktree list
Add a Worktree
- Navigate to the repository.
cd MyProject.git
- Create a “work in progress” branch.
git branch wip semver/minor
- Add a worktree.
git worktree add ../MyProject.wip wip
- Navigate to the worktree.
cd ../MyProject.wip
Remove a Worktree
Note: When you remove a worktree, untracked changes files (any created there that are not committed) are deleted without opportunity for recovery.
- Confirm that there are no uncommited changes.
cd MyProject.wip
git status
- Navigate to the reposiory.
cd ../MyProject.git
- Remove the worktree.
git worktree remove ../MyProject.wip
Next
Use Notepad++ as Your Text Editor in Git Bash