In the development work, it is estimated that everyone has encountered such a demand: the things under development do not want to move, but need a complete working directory to test or do some other things. Generally speaking, you can pull a branch, use git stash to save the current workspace state, and then cut back when you’re done. But if you need two or more workspaces to do things at the same time, the above scheme will not work. At this time, of course, several complete warehouse copies of GIT clone can be used, but it is also troublesome and troublesome, and the synchronization of the warehouse is also a problem. Is there a better way? I didn’t know before. I knew that I had discovered a new dragon killing technology of GIT – git WorkTrees work tree function. Today, insect will introduce you to this new little-known secret script that thousands of people need to kill dragons.
Git worktree is not a new function in a strict sense. It has been launched for several years and was introduced in version 2.5 released in July 2015. Worktree is a multi workspace (directory, tree) linked to a unified warehouse. A git warehouse can support multiple work trees corresponding to different branches. In git, we create a (main) working tree (main working tree) through “git init” or “git clone”.
Similarly, we use git worktree to create a workspace (tree) with different directories (and workspace), which we call “linked working tree”. The GIT repository has a main work tree (bare Library) and zero or more linked work trees. Different from the reconstructed isolated directory, the linked work tree is organically related to the direct line branch of the main warehouse. The change submission of any linked work tree is within the warehouse. After the linked work tree is used up, it can be deleted directly through git worktree remove.
Creating a work tree is as simple and efficient as creating a new branch. Create a work tree by running the following format:
Git worktree add.. / work tree directory branch (commits ID)
This command will create a complete set of branch workspaces under the.. / work tree directory. The directory can be specified arbitrarily, but it is better to be outside the main warehouse directory to avoid polluting the warehouse. Then you can check out branches in this directory, push them upstream, and so on.
If the branch does not exist, you can use – B operation to create a new branch and associate the new branch with the work tree.
The list function lists the details of each work tree. First list the main work tree, then list each linked work tree. The output details include whether the work tree is a bare tree, the GIT commit hash corresponding to the work tree, and the corresponding branch (if not, it is a separate head).
The list function has a — porcelain option, which can list more complete hash values and branch information
If the work tree directory is located 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 the work tree to a new directory. Note that you cannot move the main or linked work tree that contains submodules. For example, we perform:
git worktree move alpha ../a
Change before operation:
Clear $Git_ Work tree information in dir / WorkTrees.
As mentioned earlier, the user deletes the work tree. Note: this command can only delete the clean work tree (there are no untracked files, and no modifications can be made to the tracked files). Dirty work trees or trees with sub modules need to be deleted by — force. The main work tree cannot be deleted.
Unlock the working tree. After unlocking, you can prune, move or remove it.
Role of work tree
So what can you do with a work tree? Here is the function of the work tree.
Run the test while working on another branch
In large projects with very good test coverage, it may take a long time to run a group of test suites. Instead of wasting time, it is better to pull several work trees and run multiple test suites in parallel. Many ides allow you to open multiple projects at once, but each project must be in its own directory. We can git clone two completely independent repositories, but the work tree is more perfect:
Work trees are implemented using hard links, so they are lightweight and fast.
Changes can be shared between work trees as long as they commit them to the local repository. For cloning, you must first push it to the remote library and synchronize it in Git pull.
If you accidentally commit a copy of the clone and commit changes incorrectly, you must migrate the changes manually (if very simple) or using patches. Using the work tree, you only need to use git cherry pick and git reset to fix the error.
It is common practice to reserve an additional work tree for running tests. However, one limitation of the work tree is that a branch can only pull one work tree. You can solve this problem by creating temporary branches, as follows:
git checkout -b TEMP/branch feature/branch
The temp prefix is used here to emphasize that the branch is temporary. When submitting changes on the original branch, you can easily use git merge feature / branch to synchronize the branch.
, ### compare multiple versions
Sometimes, you need to compare two versions of a project, but you can’t do it 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 function is just half written, and it is impossible to determine where to compare tags. At this time, you can pull the work tree through the previous version or any commit submission.
Work on other branches without affecting the current working copy
Maybe we need to work on other branches, but the current working directory is chaotic and we can’t use git stash to save the state. Depending on the project, switching branches may also have adverse side effects (for example, causing the IDE to rebuild the index). This can only be solved by using the work tree.
Quickly validate projects using the work tree
Everyone may have experienced a build failure, perhaps because your partner forgot to submit some necessary configurations or the. Gitgnore settings are too broad, etc. The code can run well on its computer. If you hang up, it may be due to the lack of some files. At this time, you can use the work tree to pull a clean copy for testing to verify that all the required contents have been added. You can work reliably only by creating a new work tree.
Multiple work trees cannot correspond to the same branch
As mentioned earlier, this is a limitation of the work tree. Just pull a temporary branch.
Not applicable to sub modules
Repositories that currently use submodules cannot take advantage of the work tree.
This paper introduces a very practical git function – work tree. This function is simple and easy to use. It is definitely a five-star function that people love and use. What are you hesitating about? Use first and benefit first!
Author: zhongchong soqi ccsearchit
A little buddy love articles, and can finally give a praise. Oh, finally, as usual, Amway’s official account: “terminal research and development department”, now recommends a quality technology related article every day, mainly sharing Java related technology and interview skills, learning Java is not lost.