git stash

git-stash - 储藏工作区修改

一、基本参数

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

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

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

git stash_第1张图片

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

git stash_第2张图片

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

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

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

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

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

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

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

git stash_第3张图片

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'

你可能感兴趣的:(git)