Git - Stash 存储操作

假设您正在为您的产品实施一项新功能。 您的代码正在进行中,突然客户升级来了。 因此,您必须将新功能工作搁置几个小时。 您不能提交部分代码,也不能丢弃您的更改。 所以你需要一些临时空间,在那里你可以存储你的部分更改并在以后提交它。

在 Git 中,stash 操作获取您修改后的跟踪文件,暂存更改,并将它们保存在您可以随时重新应用的未完成更改堆栈中。

[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