Git stash使用详解

Save your local modifications to a new stash entry and roll them back to HEAD (in the working tree and in the index).

把修改储藏到stash栈中,把工作区和index区回溯到HEAD的版本。

.git/refs/stash 中,存储的是最后一个 stash 对应的节点指针

在 .git/log/refs/stash 中可以看到我们全部的 stash 记录信息

git stash储藏修改

git stash apply 恢复修改

如下,修改文件后status为modified状态,git stash后把修改藏起来了,status变成无修改状态

使用git stash apply又恢复了

 

 

git stash是git stash push命令省略写法

git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]

          [-u|--include-untracked] [-a|--all] [-m|--message <message>]

          [--] [<pathspec>...]]

如果[<pathspec>...]给定具体的文件路径,git stash只会处理路径下的文件.其他的文件不会被存储,而且也只有这些指定的文件在工作区和index区会恢复成HEAD的版本。

When pathspec is given to git stash push, the new stash entry records the modified states only for the files that match the pathspec. The index entries and working tree files are then rolled back to the state in HEAD only for these files, too, leaving files that do not match the pathspec intact.

--keep-index参数,已经add到index区的修改不会stash

If the --keep-index option is used, all changes already added to the index are left intact.

--include-untracked追踪的文件也会被缓存,当前的工作空间会被恢复为完全清空的状态.如果不使用--include-untracked而是用--all,那么除了未加入管理的文件,被git忽略(ignore)的文件也会被缓存.

If the --include-untracked option is used, all untracked files are also stashed and then cleaned up with git clean, leaving the working directory in a very clean state. If the --all option is used instead then the ignored files are stashed and cleaned in addition to the untracked files.

--patch命令可以让我们选择当前修改和HEAD提交diff部分.

使用-m|--message <message>可以添加一些注释

而git stash save命令不支持对指定文件stash,也可以添加注释,但是不需要指定参数m,直接用 git stash save “注释”

 

 

<stash>为stash@{数字}格式,stash@{0}为最近一次stash

   git stash list

        显示git栈中的所有工作区内容的备份,

WIP,Work In Progess的简称,说明代表了工作区进度。

    git stash clear

        清空git栈

git stash drop + 名称

从堆栈中移除某个指定的stash

使用git stash apply @{x} ,可以将编号x的缓存释放出来,但是该缓存还存在于list中

而 git stash apply,会将当前分支的最后一次缓存的内容释放出来,但是刚才的记录还存在list中

而 git stash pop,也会将当前分支的最后一次缓存的内容释放出来,但是刚才的记录不存在list中

注意:stash堆栈是全局的,切换分支后stash栈内容不会变,因此可以在一个分支中git stash存储修改,然后切换到另一个分支,使用git stash apply来执行相同的修改!

git stash show查看堆栈中最新保存的stash和当前目录的差异。

git stash show stash@{数字}查看指定的stash和当前目录差异。

git stash branch <branchname> [<stash>]

从最新(指定)的stash创建分支。

应用场景:当储藏了部分工作,暂时不去理会,继续在当前分支进行开发,后续想将stash中的内容恢复到当前工作目录时,如果是针对同一个文件的修改(即便不是同行数据),那么可能会发生冲突,恢复失败,这里通过创建新的分支来解决。可以用于解决stash中的内容和当前目录的内容发生冲突的情景。

发生冲突时,需手动解决冲突。

如下,使用最新的stash创建一个新的分支stashBranch,还把最新的stash给删除了。

恢复被错误clear/drop的存储

如果因为失误对存储仓库进行了clear或者drop操作,在一般机制下是不能恢复的.但是可以通过以下指令来获取仍在仓库中的,但是已经不可获取的存储列表

git fsck --unreachable |

grep commit | cut -d\  -f3 |

xargs git log --merges --no-walk --grep=WIP

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值