git stash

git-stash - 储藏工作区修改

一、基本参数

1、push [-p|--patch] [-k|--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [-m|--message <message>] [--] [<pathspec>…​]

git stash不带参数默认是push,储藏工作区修改。

需要注意未添加push的话有些参数(比如-m添加消息描述)不能使用

(1)不被跟踪的文件默认不会被储藏,可以使用-u参数储藏不被跟踪的文件。

(2)-k只储藏没有被add的文件

(3)-a 被忽略的文件以及没有被跟踪的文件也会被储藏

2、git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]

git stash pop和git stash apply都用于恢复之前储藏的数据,二者区别是pop以出栈的形式删除一次入栈记录

3、save [-p|--patch] [-k|--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [<message>]

save参数已被弃用,推荐使用stash push,二者差别是save 不能携带路径参数并且提交message不可为空

4、drop [-q|--quiet] [<stash>] 删除某一条储藏记录

5、clear 删除所有储藏记录

二、应用场景

1、本地工作时去要更新远程分支代码,为防止冲突,先储藏本地数据,拉下远程分支代码后,再尝试恢复之前储藏的代码

$ git pull
 ...
file foobar not up to date, cannot merge.
$ git stash
$ git pull
$ git stash pop

2、本地工作时需要修改另外一个分支的紧急需求,但是不想通过commit去提交一个半成品代码,那就在本地线储藏,然后切换分支紧急处理需求,紧急需求解决后再切换原来的分支恢复之前储存的代码继续coding

# ... hack hack hack ...
$ git stash
$ edit emergency fix
$ git commit -a -m "Fix in a hurry"
$ git stash pop
# ... continue hacking ...

3、本地工作时有几种不同的编码思路需要尝试时,先储藏备份当前代码,然后一条分支一条分支去尝试

# ... hack hack hack ...
$ git add --patch foo            # add just first part to the index
$ git stash push --keep-index    # save all other changes to the stash
$ edit/build/test first part
$ git commit -m 'First part'     # commit fully tested change
$ git stash pop                  # prepare to work on all other changes
# ... repeat above five steps until one commit remains ...
$ edit/build/test remaining parts
$ git commit foo -m 'Remaining parts'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值