


This article brings you relevant knowledge about Git branches. It mainly introduces related issues on how to work on multiple branches without switching branches. I hope it will be helpful to everyone.
It is common for us who are developing a certain feature and the boss suddenly jumps out and asks you to make a hotfix for production. Faced with this situation, we who use Git There are usually two solutions:
Commit the unfinished feature hastily, and then switch the branch to hotfix
git stash | git stash pop
Temporarily save the work content, and then switch to hotfix
The second method is much better than the first one, but in the face of the following scenarios, stash is still not very Good solution
The scenario we face
We are running long-term tests on the main branch, switch to hotfix or feature, and test It will be interrupted
The project is very large, frequent index switching, the cost is very high
The old version released a few years ago, the settings and It is different now. IDE restructure adaptation switching will also bring a lot of overhead.
When switching branches, you need to reset the corresponding environment variables, such as dev/qa/prod
Need to switch to a colleague's code to help debug code recurrence issues
Some students thought that wouldn't it be enough to git clone multiple repos? This is a way to solve the above problems, but there are also many problems hidden behind it:
The status of multiple repos is not easy to synchronize, for example, there is no way to quickly cherry-pick, a repo checkout branch, another repo needs to be checked out again
git history/log is repeated. When the project history is very long, the contents of the
.git
folder are very Taking up disk spaceThe same project and multiple repos are difficult to manage
So how can we meet these special scenarios without causing them to appear? What about these above issues?
git-worktree
In fact, this is a function that Git has supported since 2015, but few people know about it. git-worktree is very convenient to use. Enter in the terminal:
git worktree --help
and you can quickly see the help document description:
To explain the function of git-worktree in simple words:
You only need to maintain one repo, and you can work on multiple branches at the same time without affecting each other
There are many commands outlined in red above, but we only use the following four commonly used ones :
git worktree add [-f] [--detach] [--checkout] [--lock] [-b <new-branch>] <path> [<commit-ish>] git worktree list [--porcelain] git worktree remove [-f] <worktree> git worktree prune [-n] [-v] [--expire <expire>]
Before starting the explanation, we need to popularize two Git knowledge points that you may have overlooked:
By default,
git init
Orgit clone
The initialized repo has only oneworktree
, calledmain worktree
in a certain directory Using the Git command, there is either a
.git
folder or a.git
file in the current directory. If there is only a.git
file, the content inside must point to.git
The second sentence of the folder’s
feels quite confusing. Let’s use an example to illustrate it and it will be easy to understand.
If you are learning Spring Cloud, I recommend a free tutorial that has been serialized for many years and continues to be updated: https://blog.didispace.com/spring-cloud-learning/
git worktree add
The current project directory structure is like this (amend-crash-demo
is the root of the repo):
. └── amend-crash-demo 1 directory cd amend-crash-demo` 运行命令 `git worktree add ../feature/feature2 ➜ amend-crash-demo git:(main) git worktree add ../feature/feature2 Preparing worktree (new branch 'feature2') HEAD is now at 82b8711 add main file
Review the directory structure
. ├── amend-crash-demo └── feature └── feature2 3 directories
By default, this command will create a branch named feature2 based on the commit-ish where HEAD is located (of course, you can also specify any commit-ish in the git log). The branch disk location is as shown in the above structure
cd ../feature/feature2/
You will find that the .git
folder does not exist under this branch, but there is a .git
file. Open the file , the content is as follows:
gitdir: /Users/rgyb/Documents/projects/amend-crash-demo/.git/worktrees/feature2
At this point, if you understand the above knowledge point 2, will it be much clearer?
Next, you can do whatever you want on the feature2 branch (add/commit/pull/push) without interfering with the main worktree
一般情况下,项目组都有一定的分支命名规范,比如 feature/JIRAID-Title
, hotfix/JIRAID-Title
, 如果仅仅按照上面命令新建 worktree,分支名称中的 /
会被当成文件目录来处理
git worktree add ../hotfix/hotfix/JIRA234-fix-naming
运行完该命令,文件目录结构是这样的
. ├── amend-crash-demo ├── feature │ └── feature2 └── hotfix └── hotfix └── JIRA234-fix-naming 6 directories
很显然这不是我们想要的,这时我们就需要 -b 参数的支持了,就像 git checkout -b
一样
执行命令:
git worktree add -b "hotfix/JIRA234-fix-naming" ../hotfix/JIRA234-fix-naming
再来看一下目录结构
. ├── amend-crash-demo ├── feature │ └── feature2 └── hotfix ├── JIRA234-fix-naming └── hotfix └── JIRA234-fix-naming 7 directories
进入 JIRA234-fix-naming
目录,默认是在 hotfix/JIRA234-fix-naming
分支上
worktree 建立起来很容易,不加管理,项目目录结构肯定乱糟糟,这是我们不想看到的,所以我们需要清晰的知道某个 repo 都建立了哪些 worktree
git worktree list
所有的worktree 都在共用一个 repo,所以在任意一个 worktree 目录下,都可以执行如下命令来查看 worktree 列表
git worktree list
执行完命令后,可以查看到我们上面创建的所有 worktree 信息, main worktree
也会显示在此处
/Users/rgyb/Documents/projects/amend-crash-demo 82b8711 [main] /Users/rgyb/Documents/projects/chore/chore 8782898 (detached HEAD) /Users/rgyb/Documents/projects/feature/feature2 82b8711 [feature2] /Users/rgyb/Documents/projects/hotfix/hotfix/JIRA234-fix-naming 82b8711 [JIRA234-fix-naming] /Users/rgyb/Documents/projects/hotfix/JIRA234-fix-naming 82b8711 [hotfix/JIRA234-fix-naming]
worktree 的工作做完了,也是要及时删除的,否则也会浪费很多磁盘空间
另外,如果您正在学习Spring Cloud,推荐一个连载多年还在继续更新的免费教程:https://blog.didispace.com/spring-cloud-learning/
git worktree remove
这个命令很简单了,worktree 的名字叫什么,直接就 remove 什么就好了
git worktree remove hotfix/hotfix/JIRA234-fix-naming
此时,分支名弄错的那个 hotfix 就被删掉了
/Users/rgyb/Documents/projects/amend-crash-demo 82b8711 [main] /Users/rgyb/Documents/projects/chore/chore 8782898 (detached HEAD) /Users/rgyb/Documents/projects/feature/feature2 82b8711 [feature2] /Users/rgyb/Documents/projects/hotfix/JIRA234-fix-naming 82b8711 [hotfix/JIRA234-fix-naming]
假设你创建一个 worktree,并在里面有改动,突然间这个worktree 又不需要了,此刻你按照上述命令是不能删掉了,此时就需要 -f
参数来帮忙了
git worktree remove -f hotfix/JIRA234-fix-naming
删除了 worktree,其实在 Git 的文件中,还有很多 administrative 文件是没有用的,为了保持清洁,我们还需要进一步清理
git worktree prune
这个命令就是清洁的兜底操作,可以让我们的工作始终保持整洁
总结
到这里,你应该理解,整个 git-worktree 的使用流程就是下面这四个命令:
git worktree add git worktree list git worktree remove git worktree prune
你也应该明白 git worktree 和 git clone 多个 repo 的区别了。只维护一个 repo,创建多个 worktree,操作间行云流水
我的实践:通常使用 git worktree,我会统一目录结构,比如 feature 目录下存放所有 feature 的worktree,hotfix 目录下存放所有 hotfix 的 worktree,这样整个磁盘目录结构不至于因为创建多个 worktree 而变得混乱
在磁盘管理上我有些强迫症,理想情况下,某个 repo 的 worktree 最好放在这个 repo 的文件目录里面,但这就会导致 Git track 新创建的 worktree 下的所有文件,为了避免 Git track worktree 的内容,来来回回修改 gitignore 文件肯定是不合适的!
那么如何解决呢?点击下方卡片,关注“日拱一兵”,正在连载Git的高级技巧!
灵魂追问
可以删除 main worktree 吗?为什么
反复创建和删除worktree, repo/.git/wortree 目录的变化你能理解吗?
推荐学习:《Git教程》
The above is the detailed content of Teach you how to work on multiple branches at the same time without switching Git branches (detailed examples). For more information, please follow other related articles on the PHP Chinese website!

Git and GitHub are key tools in modern software development. Git is a distributed version control system, and GitHub is a Git-based code hosting platform. Git's core features include version control and branch management, while GitHub provides collaboration and project management tools. When using Git, developers can track file changes and work together; when using GitHub, teams can collaborate through PullRequests and Issues.

GitHubiscrucialforsoftwaredevelopmentduetoitscomprehensiveecosystemforcodemanagementandcollaboration.Itoffersversioncontrol,communitysupport,andtoolslikeGitHubActionsandPages.Startbymasteringbasicslikecreatingarepository,usingbranches,andautomatingwo

Git and GitHub are essential tools for modern developers. 1. Use Git for version control: create branches for parallel development, merge branches, and roll back errors. 2. Use GitHub for team collaboration: code review through PullRequest to resolve merge conflicts. 3. Practical tips and best practices: submit regularly, submit messages clearly, use .gitignore, and back up the code base regularly.

Git and GitHub are not the same thing: Git is a distributed version control system, and GitHub is an online platform based on Git. Git helps developers manage code versions and achieve collaboration through branching, merge and other functions; GitHub provides code hosting, review, problem management and social interaction functions, enhancing Git's collaboration capabilities.

After installing Git, in order to use more efficiently, the following settings are required: Set user information (name and mailbox) Select text editor Set external merge tool Generate SSH key settings Ignore file mode

Resolve: When Git download speed is slow, you can take the following steps: Check the network connection and try to switch the connection method. Optimize Git configuration: Increase the POST buffer size (git config --global http.postBuffer 524288000), and reduce the low-speed limit (git config --global http.lowSpeedLimit 1000). Use a Git proxy (such as git-proxy or git-lfs-proxy). Try using a different Git client (such as Sourcetree or Github Desktop). Check for fire protection

Causes of slow Git downloads include poor network connections, Git server problems, large files or large submissions, Git configuration issues, insufficient computer resources, and other factors such as malware. Workarounds include improving network connectivity, adjusting firewall settings, avoiding downloading unnecessary files or submissions, optimizing Git configuration, providing adequate computer resources, and scanning and removing malware.

How to update local Git code? Use git fetch to pull the latest changes from the remote repository. Merge remote changes to the local branch using git merge origin/<remote branch name>. Resolve conflicts arising from mergers. Use git commit -m "Merge branch <Remote branch name>" to submit merge changes and apply updates.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software