关于git:git小技巧之-Git-stash

1次阅读

共计 971 个字符,预计需要花费 3 分钟才能阅读完成。

在开发的我的项目中有很多分支的时候,咱们总是须要频繁的切换分支来比对代码,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 

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

Tips

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

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

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