158 lines
3.5 KiB
Markdown
158 lines
3.5 KiB
Markdown
在 Git 中同时处理多个分支的需求很常见,以下是几种高效的方法和技巧:
|
||
|
||
## 1. 使用 Git Worktree(推荐)
|
||
|
||
Git Worktree 允许你在同一个仓库中同时检出多个分支到不同的目录。
|
||
|
||
```bash
|
||
# 创建新的工作树(目录)
|
||
git worktree add ../feature-branch feature/xxx
|
||
git worktree add ../hotfix-branch hotfix/yyy
|
||
|
||
# 查看所有工作树
|
||
git worktree list
|
||
|
||
# 删除工作树
|
||
git worktree remove ../feature-branch
|
||
```
|
||
|
||
**优点**:
|
||
- 隔离的工作环境
|
||
- 避免频繁切换分支
|
||
- 可以同时编译不同分支的代码
|
||
|
||
## 2. 多目录克隆
|
||
|
||
```bash
|
||
# 克隆到不同目录
|
||
git clone /path/to/repo repo-main
|
||
git clone /path/to/repo repo-feature
|
||
git clone /path/to/repo repo-dev
|
||
|
||
# 在每个目录中检出不同分支
|
||
cd repo-main && git checkout main
|
||
cd repo-feature && git checkout feature/xxx
|
||
cd repo-dev && git checkout develop
|
||
```
|
||
|
||
## 3. 快速分支切换技巧
|
||
|
||
### 使用分支别名
|
||
```bash
|
||
# 设置分支别名(在.git/config中添加)
|
||
[alias]
|
||
co-main = checkout main
|
||
co-feature = checkout feature/xxx
|
||
co-dev = checkout develop
|
||
```
|
||
|
||
### 智能补全
|
||
```bash
|
||
# 使用Tab补全分支名
|
||
git checkout feat<Tab> # 自动补全为 feature/xxx
|
||
|
||
# 只匹配远程分支
|
||
git checkout origin/<Tab>
|
||
```
|
||
|
||
## 4. 批量分支操作
|
||
|
||
### 查看所有分支(包括远程)
|
||
```bash
|
||
# 查看本地和远程分支
|
||
git branch -a
|
||
|
||
# 查看已合并的分支
|
||
git branch --merged
|
||
|
||
# 查看未合并的分支
|
||
git branch --no-merged
|
||
```
|
||
|
||
### 批量删除分支
|
||
```bash
|
||
# 删除已合并的分支(谨慎使用)
|
||
git branch --merged | grep -v "\*" | xargs git branch -d
|
||
|
||
# 删除匹配特定模式的分支
|
||
git branch | grep "feature/" | xargs git branch -D
|
||
```
|
||
|
||
## 5. 暂存当前工作
|
||
|
||
当需要临时切换分支时,使用 stash:
|
||
|
||
```bash
|
||
# 保存当前修改
|
||
git stash push -m "临时保存"
|
||
|
||
# 切换到其他分支工作
|
||
git checkout other-branch
|
||
|
||
# 返回原分支并恢复
|
||
git checkout original-branch
|
||
git stash pop
|
||
```
|
||
|
||
## 6. 并行开发工作流
|
||
|
||
### 场景:同时开发多个功能
|
||
```bash
|
||
# 工作树1:主要功能开发
|
||
git worktree add ../feature-a feature/a
|
||
cd ../feature-a
|
||
|
||
# 工作树2:修复bug
|
||
git worktree add ../hotfix-b hotfix/b
|
||
cd ../hotfix-b
|
||
|
||
# 工作树3:代码审查
|
||
git worktree add ../review-c review/c
|
||
cd ../review-c
|
||
```
|
||
|
||
### 自动化脚本示例
|
||
```bash
|
||
#!/bin/bash
|
||
# setup-workspaces.sh
|
||
|
||
REPO_DIR="/path/to/repo"
|
||
BRANCHES=("main" "develop" "feature/x" "feature/y")
|
||
|
||
for branch in "${BRANCHES[@]}"; do
|
||
dir_name=$(echo $branch | sed 's/\//-/g')
|
||
git worktree add "../$dir_name" "$branch"
|
||
done
|
||
```
|
||
|
||
## 7. 实用配置建议
|
||
|
||
### Git 配置优化
|
||
```bash
|
||
# 显示上游分支信息
|
||
git config --global branch.autoSetupMerge always
|
||
|
||
# 更详细的分支状态
|
||
git config --global format.pretty fuller
|
||
|
||
# 推送时自动设置上游分支
|
||
git config --global push.autoSetupRemote true
|
||
```
|
||
|
||
### Shell 别名
|
||
```bash
|
||
# 添加到 ~/.bashrc 或 ~/.zshrc
|
||
alias gws='git worktree list'
|
||
alias gwa='git worktree add'
|
||
alias gwr='git worktree remove'
|
||
alias gb='git branch --sort=-committerdate' # 按时间排序分支
|
||
```
|
||
|
||
## 最佳实践
|
||
|
||
1. **主分支保护**:使用 worktree 避免在主要开发分支上直接修改
|
||
2. **定期清理**:删除不再需要的 worktree 和已合并的分支
|
||
3. **命名规范**:工作树目录名与分支名对应,便于识别
|
||
4. **资源管理**:注意多个工作树可能占用较多磁盘空间
|
||
|
||
这些技巧可以显著提高在多分支环境下的开发效率,特别是 Git Worktree 功能,它是处理并行开发任务的最佳选择。 |