What are git worktrees, and why should I use them?
Git worktrees have been around since 2015, but it wasn't until recently they became popular. Learn what they are, how to use them, and why you might. The post What are git worktrees, and why should I use them? appeared first on The GitHub Blog .
It seems like the latest hotness in git these days is the concept of worktrees. Which… is kind of funny because they’ve been around since 2015. But, nevertheless, they are cool, and you might be wondering why you’d use them, how they differ from branches, and why they are suddenly so popular. Let’s talk about it! Context switching with branches and stashing Let’s say you lived in a worktree-less world, and were working on a ticket, and suddenly an urgent bug came to you and you had to switch contexts. First, you might stash your work: git stash "wip feature login" Then you’d switch to your main branch and update: git checkout main git pull origin main Then make a bugfix branch: git checkout -b hotfix-bug Then you’d fix everything, commit, and push the branch: git add . git commit -m "fix broken submit button" git push origin hotfix-bug Then after merging a pull request, you might return back to your computer and pull main and remove the bug branch: git checkout main git pull origin main git branch -d hotfix-bug And then you could go back to the feature you were working on: git checkout feature-login git stash pop Phew. Where were we? The mental overhead of switching around, reloading files, reinstalling node_modules based on whatever changed, and so on, is a lot. The context switching burden is heavy. Now, this is a basic example, but sometimes developers would work around this kind of chaos with doing some more complicated git stash commands, or even multiple clones of the same repo (I’m guilty of that one). Until… worktrees! Context switching with worktrees With worktrees, you never leave your branch and you never stash, and your editor context for your original feature stays untouched. git worktree add ../hotfix-workspace -b hotfix-bug main This instantly creates a sibling folder called hotfix-workspace , and bases it on main, and checks out a new branch called hotfix-bug . Now you can open that folder in a new editor window (or cd into it) and fix the bug. Your
本文内容来源于互联网,版权归原作者所有
查看原文