Git - Stash Operation

假设您正在为您的产品实施新功能.您的代码正在进行中,并且突然出现客户升级.因此,您必须将新功能工作搁置几个小时.您无法提交部分代码,也无法丢弃您的更改.因此,您需要一些临时空间,您可以在其中存储部分更改,然后再提交它.

在Git中,存储操作会获取已修改的跟踪文件,分阶段更改并将其保存在一堆未完成的更改,您可以随时重新申请.

[jerry@CentOS project]$ git status -s
M string.c
?? string

现在,您希望切换分支以进行客户升级,但您不想提交您已经处理的内容;所以你会隐藏变化.要将新存储推送到堆栈,请运行 git stash 命令.

[jerry@CentOS project]$ git stash
Saved working directory and index state WIP on master: e86f062 Added my_strcpy function
HEAD is now at e86f062 Added my_strcpy function

现在,您的工作目录是干净的,所有更改都保存在堆栈中.让我们用 git status 命令验证它.

 
 [jerry @ CentOS project] $ git status -s 
 ?? string

现在您可以安全地切换分支并在其他地方工作.我们可以使用 git stash list 命令查看隐藏的更改列表.

[jerry@CentOS project]$ git stash list
stash@{0}: WIP on master: e86f062 Added my_strcpy function

假设您已解决客户升级问题,那么您就是回到你的新功能上寻找半完成代码,只需执行 git stash pop 命令,从堆栈中删除更改并将它们放在当前工作目录中.

 
 [jerry @ CentOS project] $ git status -s 
 ?? string 
 [jerry @ CentOS project] $ git stash pop

以上命令将产生以下结果:

# On branch master
# Changed but not updated:
# (use "git add ..." to update what will be committed)
# (use "git checkout -- ..." to discard changes in working directory)
#
#
modified: string.c
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
#
string
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (36f79dfedae4ac20e2e8558830154bd6315e72d4)

[jerry@CentOS project]$ git status -s
M string.c
?? string