天天看點

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,如需轉載請自行聯系原作者