- 创建分支:使用
git branch命令创建新的分支。例如,创建一个名为feature/new-feature的分支:
git branch feature/new-feature
- 切换分支:使用
git checkout命令切换到指定的分支。例如,切换到feature/new-feature分支:
git checkout feature/new-feature
- 查看分支:使用
git branch命令查看本地所有分支。例如,查看所有分支:
git branch
- 合并分支:使用
git merge命令将一个分支合并到另一个分支。例如,将feature/new-feature分支合并到master分支:
git merge feature/new-feature
- 删除分支:使用
git branch -d命令删除本地分支。例如,删除feature/new-feature分支:
git branch -d feature/new-feature
- 推送分支到远程仓库:使用
git push命令将本地分支推送到远程仓库。例如,将feature/new-feature分支推送到origin远程仓库:
git push origin feature/new-feature
- 拉取远程分支到本地:使用
git pull命令将远程分支拉取到本地。例如,将origin/feature/new-feature分支拉取到本地:
git pull origin feature/new-feature
以下是一些常见的分支操作示例:
- 创建一个新的功能分支:
git branch feature/new-feature git checkout feature/new-feature
- 在功能分支上开发新功能:
// 开发新功能 git add . git commit -m "Add new feature"
- 将功能分支合并到主分支:
git checkout master git merge feature/new-feature
- 删除一个功能分支:
git branch -d feature/new-feature
在使用 Git 分支时,需要注意以下几点:
- 分支名称应具有描述性,以便于识别。
- 在切换分支之前,应确保工作区已处于干净状态。
- 合并分支时,应注意可能出现的冲突。