Git: Multiple workspaces with git worktree
Your currently in the middle of refactoring a big feature. Suddenly, you receive a bug report that impacts the production version of the app. Naturally you should attend to the bug fix, but this would require temporarily switching git branches.
You could:
- Stash your work and switch branches.
- Commit your work and switch branches.
- Clone a separate directory for the bug fix.
- Use
git worktree
to create a different work directory.
We covered git stash before in a earlier article.
So this time, let's focus on git worktree
and its benefits.
What is Git worktree
The git worktree
command is a powerful tool that allows developers to create multiple working directories for a single Git repository. This can be incredibly useful for a variety of reasons, and offers several key advantages over traditional Git workflow.
Advantages of Git worktree
Switch branches easily
One of the biggest advantages of using the git worktree
command is the ability to easily switch between different branches of a project. With traditional Git workflow, switching between branches requires the developer to either checkout the branch and make changes in the same working directory, or create a new clone of the repository.
However, with git worktree
, the developer can simply create a new working directory for the branch they want to work on. This allows them to switch between branches quickly and easily, without the need to clone the repository or risk overwriting changes in the main working directory.
Work on branches simultaneously
Another advantage of git worktree
is the ability to work on multiple branches simultaneously. This can be especially useful for tasks like testing and debugging, where the developer may need to compare changes across multiple branches. With git worktree
, the developer can create separate working directories for each branch and easily switch between them as needed.
In addition to these benefits, git worktree
also offers better support for large and complex projects. By allowing developers to create multiple working directories, it helps to keep the main working directory clean and organized, and prevents the repository from becoming bloated with unnecessary files.
How to use Git worktree
git worktree
is available in the latest version of Git.
To create a new working directory for a branch, use the following syntax:
git worktree add <new_working_directory> <branch_name>
For example, to create a new working directory called feature1
for the feature1
branch, you would use the following command:
git worktree add ../feature1_folder feature1
To switch between working directories, use the cd
command to navigate to the desired directory, just as you would with any other directory on your system.
For example, to switch to the ../feature1_folder
working directory created in the previous example, you would use the following command:
cd ../feature1_folder
To remove a working directory, use the git worktree remove
command, followed by the path to the working directory you want to remove.
For example, to remove the feature1_folder
working directory, you would use the following command:
git worktree remove ../feature1_folder
Conclusion
Overall, the git worktree
command is a valuable tool for any developer working on large and complex projects. It offers the ability to switch between branches easily, work on multiple branches simultaneously, and better support for large and complex projects. So, it is definitely worth considering if you want to improve your Git workflow.
Finally, I highly recommend visiting git-worktree documentation page for a full rundown on its full capabilities
Webmentions
Want to respond? Reply, like, reply or bookmark on Twitter :)