# 本地分支操作 主要学习内容: 1. 分支显示、创建和切换 2. 分支暂存 3. 分支删除 4. 分支合并 使用命令 1. [git branch](https://git-scm.com/docs/git-branch) 2. [git checkout](https://git-scm.com/docs/git-checkout) 3. [git stash](https://git-scm.com/docs/git-stash) 4. [git merge](https://git-scm.com/docs/git-merge) ## 分支显示、创建和切换 显示所有分支以及当前所处分支 ``` $ git branch ``` 创建新分支testing ``` $ git branch testing ``` 切换到新分支testing ``` $ git checkout testing ``` 创建新分支`aa`并自动切换 ``` $ git checkout -b aa ``` ## 分支暂存 在切换分支之前最好先保存当前在暂存区域的内容,使用`git stash`命令将其保存在一个*脏*目录中 暂存当前工作状态 ``` $ git stash ``` 可进行多次暂存操作,查看暂存列表 ``` $ git stash list ``` 重新释放最新的暂存内容 ``` $ git stash apply ``` 指定暂存节点进行释放 ``` $ git stash apply 节点名 ``` 清除所有的暂存节点 ``` $ git stash clear ``` ## 分支删除 删除指定分支 ``` $ git branch -d 分支名 ``` ## 分支合并 合并指定分支到当前分支 ``` $ git merge 分支名 ``` 在分支合并出现冲突时可以使用命令回复到之前为合并状态 ``` $ git merge --abort ``` 不合并其他分支的提交历史 ``` # 仅添加文件,不进行提交 $ git merge --squash # 添加文件并进行提交 $ git merge --no-squash ``` 使用参数`--merged`和`--no-merged`过滤已合并和未合并到当前分支的分支 ## 相关阅读 * [3.1 Git 分支 - 分支简介](https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%AE%80%E4%BB%8B) * [3.2 Git 分支 - 分支的新建与合并](https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%88%86%E6%94%AF%E7%9A%84%E6%96%B0%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6) * [7.3 Git 工具 - 储藏与清理](https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E5%82%A8%E8%97%8F%E4%B8%8E%E6%B8%85%E7%90%86#r_git_stashing)