You probably had the case: you’re in the middle of a feature, a bug pops-up and you need to start working on it. Or maybe you’re in the middle of a loooong build and you need to do a quick one in another branch. Or maybe you found an unrelated bug and you want to fix it without polluting your current branch. Whatever the case, you might have thought of using a second clone of a repository. Git has a tool for that.

It’s called worktree. Git worktree is a simple way to get a second working directory with another branch, without having to clone the same repo multiple time.

You create a new worktree using git worktree add ../my_second_worktree the_other_branch. This generates a new folder with the_other_branch checked out, on which you can now work independently, and use all the regular git commands.

You can get a quick look at all the worktrees you created with

$ git worktree list
/Users/charlesfeval/git/my_repo                   bd502b1 [user/chfeval/240614_that_feature]
/Users/charlesfeval/git/my_repo--other_worktree   15d0988 [user/chfeval/240616_fix_versionin]
/Users/charlesfeval/git/my_repo--bugfix_2  9c26708 [user/chfeval/240617_fix_rockin]

And finally when you’re done with your worktree, get rid of it using git worktree remove ../my_second_worktree

Here’s the official Git doc.

usage: git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]]
                        [-b <new-branch>] <path> [<commit-ish>]
   or: git worktree list [-v | --porcelain [-z]]
   or: git worktree lock [--reason <string>] <worktree>
   or: git worktree move <worktree> <new-path>
   or: git worktree prune [-n] [-v] [--expire <expire>]
   or: git worktree remove [-f] <worktree>
   or: git worktree repair [<path>...]
   or: git worktree unlock <worktree>