天天看点

Git 分支利器不得不说的事

一、基本动作

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、把当前的还未提交的文件进行储藏,保证工作目录是干净的之后,才可以切换分支。

储藏的主要命令是:

1

2

3

4

5

6

7

8

9

10

<code>git status</code>

<code># On branch test</code>

<code># Changes not staged for commit:</code>

<code>#   (use "git add &lt;file&gt;..." to update what will be committed)</code>

<code>#   (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)</code>

<code>#</code>

<code>#   modified:   b.txt</code>

<code>#   modified:   mx</code>

<code>no changes added to commit (use </code><code>"git add"</code> <code>and</code><code>/or</code> <code>"git commit -a"</code><code>)</code>

再执行git stash之后

<code>git stash</code>

<code>Saved working directory and index state WIP on </code><code>test</code><code>: 32211b0 add b.txt</code>

<code>HEAD is now at 32211b0 add b.txt</code>

<code>nothing to commit (working directory clean)</code>

要查看现有的储藏,你可以使用git stash list:

<code>git stash list</code>

<code>stash@{0}: WIP on </code><code>test</code><code>: 32211b0 add b.txt</code>

你可以重新应用你刚刚实施的储藏,所采用的命令就是之前在原始的stash 命令的帮助输出里提

示的:git stash apply。如果你想应用更早的储藏,你可以通过名字指定它,像这样:git

stash apply stash@2。如果你不指明,Git 默认使用最近的储藏并尝试应用它。

11

12

<code>git stash apply</code>

可以看出:

apply 选项只尝试应用储藏的工作——储藏的内容仍然在栈上。要移除它,你可以运行

git stash drop,加上你希望移除的储藏的名字:

<code>git stash drop stash@{0}</code>

<code>Dropped stash@{0} (2bb9fe1993cf55843152025ae2bd79d5f7d8974c)</code>

你也可以运行git stash pop 来重新应用储藏,同时立刻将其从堆栈中移走。

本文转自shayang8851CTO博客,原文链接:http://blog.51cto.com/janephp/1301515,如需转载请自行联系原作者