在开发的我的项目中有很多分支的时候,咱们总是须要频繁的切换分支来比对代码,bug定位……
然而,每次checkout之前,git总是让咱们做commit,这种打乱咱们开发节奏的操作,咱们是不会承受的,这里我来举荐一个超好用的命令 git stash

命令定义

git stash会把所有未提交的批改(包含暂存的和非暂存的)都保存起来,用于后续复原当前工作目录。

来看一个案例:

目前的情况是我批改了两个文件:

git statusOn branch masterChanges not staged for commit:  (use "git add <file>..." to update what will be committed)  (use "git restore <file>..." to discard changes in working directory)        modified:   file1.tsx        modified:   file2.tsx

而后我执行checkout操作:

git checkout develop error: Your local changes to the following files would be overwritten by checkout:        file1.tsx        file2.tsxPlease commit your changes or stash them before you switch branches. 

能够看到,很恶心心的提醒,咱们要commit……,but,stash是什么?

二话不说,间接git stash

git stash Saved working directory and index state WIP on……

这时候咱们执行git status

git statusOn branch masternothing to commit, working tree clean

哇哦,所有都喧扰了~!
而后咱们就能够开开心心的checkout任何分支了!

如何召回暂存的数据

很简略,一条命令

git stash pop 

相当于弹出暂存区中的批改记录。

Tips

  1. stash命令是本地的,不会被push到近程
  2. 倡议增加stash的时候指定名称

    git stash save "操作名称"
  3. 查看stash记录

    git stash list
  4. git stash pop 和git stash apply区别
    前者会在取出批改后删除list中的stash记录,后者不会删除list中的记录。