git小技巧之 Git stash

不羁的风 -
git小技巧之 Git stash
在开发的项目中有很多分支的时候,我们总是需要频繁的切换分支来比对代码,bug定位……
但是,每次checkout之前,git总是让我们做commit,这种打乱我们开发节奏的操作,我们是不会接受的,这里我来推荐一个超好用的命令 git stash命令定义

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

来看一个案例:

目前的状况是我修改了两个文件:

git status

On branch master
Changes 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.tsx
Please 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 status
On branch master
nothing to commit, working tree clean

哇哦,一切都清净了~!
然后我们就可以开开心心的checkout任何分支了!

如何召回暂存的数据

很简单,一条命令

git stash pop 

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

Tipsstash命令是本地的,不会被push到远程

建议添加stash的时候指定名称

git stash save "操作名称"

查看stash记录

git stash list
git stash pop 和git stash apply区别
前者会在取出修改后删除list中的stash记录,后者不会删除list中的记录。
特别申明:本文内容来源网络,版权归原作者所有,如有侵权请立即与我们联系(cy198701067573@163.com),我们将及时处理。

Tags 标签

git工程化

扩展阅读

加个好友,技术交流

1628738909466805.jpg