git stash 使用

今天在开发过程中遇到一个问题。
我在dev分支开发这。但是还没开发完新的功能。又有人要求我去改bug。我需要切换到bug分支。但是本地的开发到一半。也不能提交怎么半?

这时候你需要 git stash

先来实际模拟下

## 查看当前状态。我新增了一个txt文件
 MINGW64 /e/.css/testss (dev)
$ git st
On branch dev
Your branch is ahead of 'origin/dev' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   "\344\275\240\345\245\275.txt"

## 此时我需要去修改bug。我就stash下
 MINGW64 /e/.css/testss (dev)
$ git stash
Saved working directory and index state WIP on dev: 274a87a Merge branch 'bug' into dev
## 查看当前分支状态 clean,此时就切换到bug分支该bug。
 MINGW64 /e/.css/testss (dev)
$ git st
On branch dev
Your branch is ahead of 'origin/dev' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
## 改完bug回来后。 使用 git stash pop 将之前写的恢复。继续开发新功能
 MINGW64 /e/.css/testss (dev)
$ git stash pop
On branch dev
Your branch is ahead of 'origin/dev' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   "\344\275\240\345\245\275.txt"

Dropped refs/{0} (0987e1d06acecddd4f82138a6f2e3c8a0512c409)

 MINGW64 /e/.css/testss (dev)
$ git st
On branch dev
Your branch is ahead of 'origin/dev' by 2 commits.
  (use "git push" to publish your local commits)

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   "\344\275\240\345\245\275.txt"

常用git stash命令:

(1)git stash save "save message" : 执行存储时,添加备注,方便查找,只有git stash 也要可以的,但查找时不方便识别。

(2)git stash list :查看stash了哪些存储

(3)git stash show :显示做了哪些改动,默认show第一个存储,如果要显示其他存贮,后面加{$num},比如第二个 git stash show {1}

(4)git stash show -p : 显示第一个存储的改动,如果想显示其他存存储,命令:git stash show {$num} -p ,比如第二个:git stash show {1} -p

(5)git stash apply :应用某个存储,但不会把存储从存储列表中删除,默认使用第一个存储,即{0},如果要使用其他个,git stash apply {$num} , 比如第二个:git stash apply {1}

(6)git stash pop :命令恢复之前缓存的工作目录,将缓存堆栈中的对应stash删除,并将对应修改应用到当前的工作目录下,默认为第一个stash,即{0},如果要应用并删除其他stash,命令:git stash pop {$num} ,比如应用并删除第二个:git stash pop {1}

(7)git stash drop {\(num} :丢弃{\)num}存储,从列表中删除这个存储

(8)git stash clear :删除所有缓存的stash

相关推荐