In our work, we often need to develop requirements and fix bugs at the same time, so we need to switch branches back and forth frequently.

A slightly more lazy approach would be to clone several repositories, such as A and B, with A for requirements development and B for Bugfixes. This takes the pain out of constantly switching branches, but it’s still a bit cumbersome. For example, the code pushed by A needs to be pulled from B. Git worktree: Git worktree

What can Git Worktree do?

It allows a Git repository to have multiple working directories. It actually creates an additional working directory connected to the repository. Each created working directory is a pseudo-repository with the same structure as a regular Git repository. Its.git file actually references the main repository’s.git file.

Create a new working directory from a specified branch that can synchronize its status with that of the main repository. Code pulled from the main repository can be seen in the new working directory; New working directory submission, merge, push code, in the main repository does not need to pull.

How does it work?

Create working directories for existing branches

git worktree add <folder_path> <source_branch>
Copy the code

Instead of creating a new branch, you create a working directory for the specified branch

Migrate the new branch from the specified source branch and create a working directory

git worktree add -b <new_branch_name> <folder_path> <source_branch>
Copy the code


: New branch name (based on)


: Specifies the directory location of the new workspace

: source branch

View existing workspaces

Git worktree List or Git worktree List -- PorcelainCopy the code

Deleting a Workspace

First, delete the working directory (referring to the folder) after the work in the workspace is completed. Then, execute the following command:

git worktree prune
Copy the code