git worktrees

Git worktrees are one of the less used git features. I recently started using it and I found it very helpful while doing code reviews or working with multiple branches.

Git worktrees are a feature in Git that allow you to have multiple working directories associated with a single Git repository. Each working directory, known as a “worktree,” is a separate copy of your project’s files where you can make changes independently of the others. Worktrees are especially helpful when you need to work on multiple branches or versions of your code simultaneously.

Imagine you’re faced with a common scenario in software development: you need to perform a code review, but you have some unfinished work on your local branch. In such situations, the traditional approach would involve using git stash to temporarily store your changes, checking out the branch you want to review, and then, once the review is complete, returning to your branch and applying git stash pop to restore your changes. This method is effective but can become cumbersome, especially when you find yourself needing to repeat it frequently.

To streamline this process, Git offers a valuable feature known as ‘Git worktrees’. This feature allows you to create additional working directories linked to the same repository. These ‘worktrees’ function as separate copies of your project, enabling you to work on different branches or tasks concurrently without the need to stash and pop changes repeatedly.

For instance, if you need to switch to a different branch for a code review, you can effortlessly create a new worktree with the following command

So for instance, if you need to checkout a branch then you could do something like this:

git worktree add <path-to-new-directory> <branch-or-commit>

This command establishes a dedicated working directory for the target branch or commit, allowing you to focus on your code review without disrupting your ongoing work. Git worktrees empower you to efficiently manage multiple tasks within a single Git repository, making your development workflow smoother and more organized.

You could do it for any number of branches. You can list your worktrees with:

git worktree list

For instance, if I am working on develop branch and I want to do a code review on feature/login branch. I can do a git add login feature/login. This command will create login directory within my current directory and checkout the feature/login branch inside that. I can just cd to login do my review and navigate out of the login directory when I am done.

If you want to remove a worktree then you do so by running:

git worktree remove <worktree-name>

Where is the name of directory you passed to the git worktree add command.

Git worktrees are a valuable feature for managing and organizing your Git projects efficiently. They empower you to work on multiple tasks in parallel, simplifying the development process and reducing the need for repetitive cloning. Whether you’re working on different branches or tackling various aspects of your project, Git worktrees can enhance your productivity and streamline your workflow.