Get freedom to try things out alongside the security of having a new, linked clone of your repository if your experiment goes wrong.

Git is designed in part to enable experimentation. Once you know that your work is safely being tracked and safe states exist for you to fall back upon if something goes horribly wrong, you’re not afraid to try new ideas. Part of the price of innovation, though, is that you’re likely to make a mess along the way. Files get renamed, moved, removed, changed, and cut into pieces. New files are introduced. Temporary files that you don’t intend to track take up residence in your working directory.

In short, your workspace becomes a house of cards, balancing precariously between “it’s almost working!” and “oh no, what have I done?”. So what happens when you need to get your repository back to a known state for an afternoon so that you can get some real work done? The classic commands git branch and  git stash come immediately to mind, but neither is designed to deal, one way or another, with untracked files, and changed file paths and other major shifts can make it confusing to just stash your work away for later. The answer is Git worktree.

What is a Git worktree

A Git worktree is a linked copy of your Git repository, allowing you to have multiple branches checked out at a time. A worktree has a separate path from your main working copy, but it can be in a different state and on a different branch. The advantage of a new worktree in Git is that you can make a change unrelated to your current task, commit the change, and then merge it at a later date, all without disturbing your current work environment.

#git

Experiment on Your Code Freely with Git Worktree
1.10 GEEK