DEV Community

Cover image for Git Dragon-Slaying Trick: Stop Repeatedly Cloning Repositories—Try This Instead! 🚀🚀🚀
Huang BingFeng
Huang BingFeng

Posted on

Git Dragon-Slaying Trick: Stop Repeatedly Cloning Repositories—Try This Instead! 🚀🚀🚀

Why You Need Git Worktree

A real-world example:

Imagine you are a frontend developer working on a new feature using Vue CLI. Suddenly, an issue occurs in production, and you have to pause your current work, use git stash to temporarily save your changes, and switch to another branch to fix the issue. After completing the fix, you restore your stashed changes and restart Vue CLI, only to find that your development flow has been disrupted. 🤷🏻‍♂️

Or, perhaps you've considered cloning multiple repositories on your computer for parallel development. However, each repository is an independent copy and cannot directly communicate with the others. To sync changes, you might need to push your code to a remote repository and then pull the updates into another copy. Not to mention, git stash requires commit, push, pull, and other steps, making the process tedious and inefficient.

Is there a better solution? 🤔

The answer is yes: Git Worktree was designed to solve these issues! 🥳

Interestingly, Guido van Rossum, the creator of Python, mentioned in 2021 that he was unaware of Git Worktree before and praised this feature:

screenshot

Official Documentation: https://git-scm.com/docs/git-worktree

Git Worktree, introduced in Git 2.5, allows you to create multiple working trees within the same repository, each checking out a different branch. These working trees share the same .git directory (i.e., the object database), avoiding redundant storage of the same Git objects (such as commits, trees, and blobs). The .git directory in the new repository is essentially a reference to the main repository’s .git directory.

This means you can work on different branches in separate folders simultaneously, eliminating the need for frequent branch switching. Additionally, since the new working directory is based on existing .git data, it saves time and disk space while making branch switching and code retrieval much faster.

worktree

How to Use Git Worktree

1. Create a New Worktree

git worktree add <path> <branch>
Enter fullscreen mode Exit fullscreen mode
  • Creates a new worktree and checks out the specified branch into it. For example:
git worktree add ../my-feature-branch feature-branch
Enter fullscreen mode Exit fullscreen mode
  • This will create a new worktree at ../my-feature-branch and check out the feature-branch into it.

2. Remove a Worktree

git worktree remove <path>
Enter fullscreen mode Exit fullscreen mode
  • Removes the specified worktree. For example:
git worktree remove ../my-feature-branch
Enter fullscreen mode Exit fullscreen mode
  • This deletes the ../my-feature-branch worktree. Note that uncommitted changes in the worktree will not be lost, and the repository itself remains intact.

3. List All Worktrees

git worktree list
Enter fullscreen mode Exit fullscreen mode
  • Displays all worktrees in the current repository and their statuses.

4. Clean Up Orphaned Worktrees

git worktree prune
Enter fullscreen mode Exit fullscreen mode
  • Deletes all removed worktrees, freeing up disk space.

5. Check Worktree Status

git worktree status
Enter fullscreen mode Exit fullscreen mode
  • Shows the current worktree status, including the active branches and file modifications.

Visual Management in VSCode

While command-line operations are straightforward, many developers prefer a graphical interface. In VSCode, you may want to easily search for and switch between different work directories without memorizing multiple commands.

To address this, I developed a VSCode extension. After over a year of iterations and improvements based on GitHub Issues, it is now stable and ready to use, offering a clear and intuitive interface.

Feel free to use it with confidence.

preview

Summary

Git Worktree eliminates the hassle of frequent branch switching and redundant repository cloning, allowing you to manage multiple worktrees efficiently within a single repository. Whether using command-line tools or a VSCode extension, you can significantly improve development efficiency and streamline your workflow. 🚀🥳

Top comments (0)