git stash 命令
总的来说,git stash命令的作用就是将目前还不想提交的但是已经修改的内容进行保存至堆栈中,后续可以在某个分支上恢复出堆栈中的内容。
在开发过程中,本地修改还未提交,或者暂时不能提交到仓库的时候,这个时候工作任务需要切换到其他分支,或者需要合并主线最新代码到自己的分支。
这个时候如果直接切换分支,则本地修改的代码就回丢失。
这个时候就可以使用 git stash 进行本地缓存,将修改的代码缓存到本地
# 直接保存
git stash
# 保存的时候添加备注
git stash save “你要给本次提交添加的备注信息”
# 查看堆栈中缓存的修改
git stash list
# 弹出堆栈中保存的一次修改,并复原到工作目录,堆栈中保存的信息被删除
# index可选,默认0
git stash pop [index]
# 将堆栈中index下标的一次记录复原至工作区,堆栈中的记录不删除
# index 可选,默认0
git stash apply [index]
git stash drop 误用,将堆栈中的信息清空怎么办
运行drop后,清空后的堆栈会将每一条修改记录id返回,这个时候只需要运行如下命令就可将修改复原至工作区
git stash apply id
安全起见,慎用drop命令
完整命令参数如下
GIT-STASH(1) Git Manual GIT-STASH(1)
NAME
git-stash - Stash the changes in a dirty working directory away
SYNOPSIS
git stash list [<options>]
git stash show [<stash>]
git stash drop [-q|--quiet] [<stash>]
git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
git stash branch <branchname> [<stash>]
git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [-m|--message <message>]
[--] [<pathspec>...]]
git stash clear
git stash create [<message>]
git stash store [-m|--message <message>] [-q|--quiet] <commit>
DESCRIPTION
Use git stash when you want to record the current state of the working
directory and the index, but want to go back to a clean working
directory. The command saves your local modifications away and reverts
the working directory to match the HEAD commit.