The introduction

In your development work, you’ve probably all come across the need for something you’re developing that doesn’t want to move, but needs a full working directory to test or do some other thing. Typically, you can branch out, save the current workspace state with Git Stash, and then cut back when you’re done. However, if two or more workspaces are required to do something at the same time, the above solution will not work. Of course you can git several full repository replicas at this point, but it’s a hassle, and repository synchronization is a problem. Is there a better way? I didn’t know it until this time, when I discovered a new way to kill Git’s dragon — Git worktrees. Today insect insect introduces this new little-known ten thousand people need to slay dragon secret book to everybody.

An overview of the

Git Worktree is not technically a new feature, it has been around for several years and was introduced with version 2.5 released in July 2015. A Worktree is a collection of workspaces (directories, trees) linked to a unified repository. A Git repository can support multiple working trees, one for each branch. We create a main working tree in Git with “git init” or “git clone”.

In the same way, we use a Git Worktree to create a workspace (and workspace) with different directories, which we call a Linked Working Tree. Git repositories have one main work tree (bare library) and zero or more linked work trees. Unlike isolated directories that are rebuilt, linked worktrees are organically related to the main repository directly as branches, and any change committed to a linked worktree is inside the repository. After the link working tree is used up, it can be deleted directly through git worktree remove.

Basic usage

add

Creating a work tree is as simple and efficient as creating a new branch. Run the following format to create the working tree:

git worktree add .. / Worktree branch (commits ID)

The command will be displayed in the… / worktree, create a complete set of branch workspaces. This directory can be specified arbitrarily, but it is best to leave it outside the main warehouse directory to avoid contaminating the warehouse. You can then check out branches in that directory, push upstream, and so on.

If the branch does not exist, you can use the -b operation, which allows you to create a new branch and associate that new branch with the working tree.

list

The List function lists the details of each work tree. The main worktree is listed first, and then each linked worktree is listed. The output details include whether the work tree is bare, the work tree corresponds to the Git Commit hash, and the corresponding branch (if not, the detached HEAD).

The list function has a –porcelain option to list more complete hash and branching information

lock

If the working tree directory is on a portable device or network share, lock it with LOCK to prevent its management files from being automatically deleted. This also prevents it from being moved or deleted.

move

Move the work tree to the new directory. Note that you cannot move the main work tree that contains submodules or linked work trees. For example, we implement:

git worktree move alpha .. /a

Changes to prior operations:

prune

Understand the worktree information in $GIT_DIR/worktrees.

remove

As mentioned earlier, the user deletes the work tree. Note: This command can only delete clean work trees (there are no untraced files, nor can any changes be made to the traced files). Dirty work trees or trees with submodules need to be removed using –force. The main work tree could not be deleted.

unlock

To lock the working tree, unlock, unlock, unlock it can be prune, move or remove and other operations.

The role of the work tree

So what can you do with a work tree? Here’s what the work tree does.

Run tests while working on another branch

In a large project with very good test coverage, running a set of test suites can take a long time, and rather than wasting time waiting, it is better to run multiple test suites in parallel with several work trees. Many IDEs allow multiple projects to be opened at once, but each project must be in its own directory. You can clone two completely independent repositories with Git, but the working tree is more perfect:

Work trees are implemented using hard links, so they are light and fast.

Changes can be shared between working trees (as long as they commit them to the local repository). For clones, you must first push them to a remote library and synchronize them in Git Pull.

If by accident a copy of Clone commits the wrong changes, you must migrate the changes manually (if it is simple) or using a patch. To use the working tree, just use git cherry-pick and git reset to fix errors.

It is common practice to keep an additional work tree for running tests. But one of the limitations of working trees is that a branch can only pull one working tree. You can solve this problem by creating a temporary branch, as shown below:

git checkout -b TEMP/branch feature/branch

The TEMP prefix is used here to emphasize that the branch is temporary. When committing changes on the original branch, you can easily use the Git Merge Feature/Branch to synchronize branches.

, ### Compare multiple versions

Sometimes you need to compare two versions of a project, but you can’t do that with diff alone. You may need to compare the two versions one by one, or even run the version at the same time. Or a complex feature is halfway written, and it is not clear where to make a marker comparison from, and the work tree can be pulled from a previous version or any Commit commit.

Work on other branches without affecting the current working copy

Maybe we need to work on another branch, but the current working directory is a mess and you can’t use Git Stash to save state. Depending on the project, switching branches can also have undesirable side effects (for example, causing the IDE to regenerate the index). At this point, you can only use the work tree.

Quickly validate projects using a work tree

Everyone has experienced build failures, perhaps because your partner forgot to submit some required configuration, or the.gitgnore Settings were too broad, etc. The code runs just fine on its own computer, but it hangs up on you because it may be missing some files. At this point, you can use the working tree to pull a clean copy and test to verify that all the required includes have been added. You can only work reliably if you start by creating a new work tree.

limit

You cannot have multiple working trees corresponding to the same branch

As mentioned earlier, this is a limitation of the working tree. Just pull a temporary branch.

Does not apply to submodules

Repositories that currently use submodules cannot take advantage of the work tree.

conclusion

This article introduced you to a very useful Git feature called the work tree. The function is concise and easy to use, is absolutely a person see people love, people see people with five-star good function. Hesitate what, first use first benefit!

Author: CCSearchit


Links:
https://www.jianshu.com/p/c5c…


Source: Jane Books

The last

Finally, we continue to use our official account: “terminal R&D department”. At present, every day we will recommend a quality technology-related article, mainly to share JAVA related technology and interview skills, learning JAVA don’t get lost.