一、基本动作
Git 中的分支实际上仅是一个包含所指对象校验和(40 个字符长度SHA-1 字串)
的文件,所以创建和销毁一个分支就变得非常廉价。
Git 是如何知道你当前在哪个分支上工作的呢?其实答案也很简单,它保存着一个
名为HEAD 的特别指针。
创建:git branch iss53
删除:git branch -d iss53
切换:git checkout iss53
创建并切换:git checkout -b iss53
查看哪些分支已被并入当前分支:git branch --merge 或者相反:git branch --no-merged
二、切换分支必须遵循的状态
只有在以下状态下才可以切换分支:
1、当前分支中的所有文件都已经提交到本地版本库。
2、把当前的还未提交的文件进行储藏,保证工作目录是干净的之后,才可以切换分支。
储藏的主要命令是:
git status # On branch test # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: b.txt # modified: mx # no changes added to commit (use "git add" and/or "git commit -a")
再执行git stash之后
git stash Saved working directory and index state WIP on test: 32211b0 add b.txt HEAD is now at 32211b0 add b.txt git status # On branch test nothing to commit (working directory clean)
要查看现有的储藏,你可以使用git stash list:
git stash list stash@{0}: WIP on test: 32211b0 add b.txt
你可以重新应用你刚刚实施的储藏,所采用的命令就是之前在原始的stash 命令的帮助输出里提
示的:git stash apply。如果你想应用更早的储藏,你可以通过名字指定它,像这样:git
stash apply stash@2。如果你不指明,Git 默认使用最近的储藏并尝试应用它。
git stash apply # On branch test # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: b.txt # modified: mx # no changes added to commit (use "git add" and/or "git commit -a") git stash list stash@{0}: WIP on test: 32211b0 add b.txt
可以看出:
apply 选项只尝试应用储藏的工作——储藏的内容仍然在栈上。要移除它,你可以运行
git stash drop,加上你希望移除的储藏的名字:
git stash drop stash@{0} Dropped stash@{0} (2bb9fe1993cf55843152025ae2bd79d5f7d8974c)
你也可以运行git stash pop 来重新应用储藏,同时立刻将其从堆栈中移走。