git stash

如何使用git stash(暂存)命令呢?

查看git stash相关命令

在mac terminal输入git help stash

git stash list                    #显示出所有的暫存清单
git stash show                    #显示所暂存的一些修改 
git stash drop stash@{number}     #丢弃暂存的修改
git stash stash@{number}          #取出某一次的暫存修改
git stash branch <branchname>     #暂存到某一分支
git stash [save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
                [-u|--include-untracked] [-a|--all] [<message>]]
                                  #暂存修改
git stash clear                   #清除所有暂存
git stash create [<message>]      #创建暂存信息
git stash store [-m|--message <message>] [-q|--quiet] <commit>

示例如下:

创建c.txt文件,并暂存,暂存信息为“add c.txt”

1
2
3
4
5
6
    ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$vim c.txt
    ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git add .
    ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git stash save "add c.txt"
    Saved working directory and index state On master: add c.txt
    HEAD is now at 5636eac add new file
    

显示暂存列表信息

1
2
3
4
    ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git stash list
    stash@{0}: On master: add c.txt
    stash@{1}: On master: mm
    stash@{2}: WIP on master: 5636eac add new file

显示最近一次暂存的一些修改

1
2
3
   ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git stash show
     c.txt | 1 +
     1 file changed, 1 insertion(+)

把stash@{2}的暂停版本更新到本地版本

1
2
3
4
5
6
7

  ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git stash apply stash@{2} 
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)

	new file:   b.txt

丢弃stash@{2}的暂停版本

1
2
   ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git stash drop stash@{2}
    Dropped stash@{2} (b6aaa50f4b36fb264b677bc4b35e51bf0601a3c0)

显示最新的暂存列表信息

1
2
3
4
  ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git stash list
    stash@{0}: On master: add c.txt
    stash@{1}: On master: mm
    ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$

把stash@{1},stash@{0}暂存版本恢复到本地

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git stash apply stash@{1}
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)

	new file:   b.txt

    ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git stash apply stash@{0}
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)

	new file:   b.txt
	new file:   c.txt

观察是不是先前暂停版本的文件

1
2
    ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$ls
    a.text b.txt  c.txt

显示暂存列表信息

1
2
3
  ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git stash list
    stash@{0}: On master: add c.txt
    stash@{1}: On master: mm

清除暂存列表信息

1
2
3
4
5
    ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git stash clear
    #暂存列表已经被清空
    ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$git stash list
    ksnowlv@ksnowlvdeMacBook-Pro~/Music/gittest$