devStandard/docs/learning/e-git/6-git worktree.md
2025-03-29 14:35:49 +08:00

59 lines
1.3 KiB
Markdown

# git worktree
> 同一仓库创建多个工作树
> 例如当前正在 dev 分支下开发, 有很多代码未提交, 同时正运行 server, 突然有一个紧急 bug 需要修复
> 原来的步骤需要停止 server, git stash, 切换到 fix 分支
> 现在只需要新建一个新的工作树
* Git 2.5 版本后新增的功能
* 官方文档: [https://git-scm.com/docs/git-worktree/zh_HANS-CN](https://git-scm.com/docs/git-worktree/zh_HANS-CN)
## 创建新的工作树
```sh
git worktree add <路径> <分支>
# eg: 切换到已有分支
git worktree add ../my-feature-branch feature-branch
```
## 移除工作树
```sh
git worktree remove <路径>
```
## 列出所有工作树
```sh
git worktree list
```
## 清理孤立的工作树
```sh
git worktree prune
```
## 查看当前的工作树状态
```sh
git worktree status
```
## 更多参数
```sh
git worktree --help
======
git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]]
[--orphan] [(-b | -B) <new-branch>] <path> [<commit-ish>]
git worktree list [-v | --porcelain [-z]]
git worktree lock [--reason <string>] <worktree>
git worktree move <worktree> <new-path>
git worktree prune [-n] [-v] [--expire <expire>]
git worktree remove [-f] <worktree>
git worktree repair [<path>...]
git worktree unlock <worktree>
===
```