今日已更新 205 条资讯 | 累计 23788 条内容
关于我们

Git Worktrees Are Great

Nolan Miller 2026年07月25日 02:31 0 次阅读 来源:Dev.to

I want to tell you about a tool I've been using for a few months now, that I can't believe that I ever went without. Recently, I've been more productive, experimental and I've learned more about my development environment just from using this tool. Alright, I'm done burying the lead. The tool is git worktrees . Worktrees is a feature built into git that allows you to have multiple working trees of a repository at once. That means you can have multiple branches checked out, with incomplete changes on each one. Instead of having one repository where your manipulation happens, you can have infinite copies (or, as many copies as you can hold on a hard drive)! Often, I am in the middle of working on a feature and I get an email about an urgent bug or a small change that needs made immediately. Before, I would have to either create a "wip:" commit or stash my changes, and I don't really like doing either one of them. Now, as long as my files are saved, I can safely close my editor, create a new worktree and leave all of my uncomitted changes waiting for me to return. How to set up your project for git worktrees In a normal project, your directory might look something like this: MyProject/ ├── .git/ <-- Git metadata in the project dir ├── bin/ ├── obj/ └── Program.cs After following just a few steps, our projects will look more like this: MyProject/ ├── .git/ <-- Git metadata ├── feature-x/ <-- Worktree #1 ├── bin/ ├── obj/ └── Program.cs └── bugfix/ <-- Worktree #2 ├── bin/ ├── obj/ └── Program.cs A full copy of the project's files. In order to set a project up this way, it's best to start in an empty directory, with your project hosted on a remote git server. First, create a directory for your project. mkdir MyProject && cd MyProject Once inside, we're going to clone a bare repository. A bare repository doesn't contain a working tree or any of the files of the project, just the git metadata. We pull that and put it in the .git directory by running the following command.

本文内容来源于互联网,版权归原作者所有
查看原文