DEV Community

Keyur Ramoliya
Keyur Ramoliya

Posted on

πŸš€ Git - Boost Your Productivity with Git worktrees 🌟

Are you tired of constantly switching between branches while working on your Git projects? Git Worktrees to the rescue! 🌳

Git Worktrees are a fantastic feature that allows you to have multiple working directories (working trees) linked to a single Git repository. 🌐 This is especially handy when you need to juggle multiple branches simultaneously. Here's how to make the most of it:

1️⃣ Create a New Worktree:

Start by creating a new worktree for the branch you want to work on with this simple command:

   git worktree add <path> <branch-name>
Enter fullscreen mode Exit fullscreen mode

Replace <path> with the location where you want to set up your new worktree (e.g., ../my-feature-branch) and <branch-name> with the name of your target branch.

Example:

   git worktree add ../my-feature-branch feature-branch
Enter fullscreen mode Exit fullscreen mode

2️⃣ Navigate to the New Worktree:

Move to the newly created worktree:

   cd ../my-feature-branch
Enter fullscreen mode Exit fullscreen mode

3️⃣ Work on the Branch:

Now, you can work on your branch independently in this new worktree. Any changes you make here won't affect your main working tree.

4️⃣ List and Manage Worktrees:

To see all your active worktrees, use:

   git worktree list
Enter fullscreen mode Exit fullscreen mode

When you're done with a worktree, you can remove it by either manually deleting the associated directory or using:

   git worktree remove <path>
Enter fullscreen mode Exit fullscreen mode

Example:

   git worktree remove ../my-feature-branch
Enter fullscreen mode Exit fullscreen mode

Using Git Worktrees is a game-changer for simultaneous branch work, whether it's reviewing, testing, or making changes across different branches. 🌈 Say goodbye to the hassle of multiple clones and keep your workspace organized and efficient!

Top comments (0)