DEV Community

IHesamI
IHesamI

Posted on

Save your life with Git Worktree

Hello, today I would like to discuss an issue that I encountered in my recent project.

Lately, I have embarked on a project that has been in development for several years and relies on outdated tools. Consequently, my primary task is to modernize the project and occasionally address issues within the old codebase. The most challenging aspect for me is dealing with the node_modules directory, as I am required to reinstall all the packages whenever I switch between branches.

worktree is the ultimate solution for tackling such issues. By utilizing this command, you can effortlessly link a specific directory to a designated repository. For instance, if you want to create a branch named new-version and this branch requires a dedicated directory, which can be easily achieved by this command. Let's delve into its practical implementation:

git worktree add new-version
Enter fullscreen mode Exit fullscreen mode

The provided code snippet will generate a new directory and branch named new-version. Additionally, it will duplicate the contents of the HEAD branch into the new-version directory.

It is also feasible to create a directory for the branches that already exist:

git worktree add path_to_new_directory existing_branch_name
Enter fullscreen mode Exit fullscreen mode

This command will create a directory in the provided path from the chosen branch.

Thank you for taking the time to read this post. For further details regarding this command, please refer to the official document available at the following link.

Top comments (0)