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

在 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

Git存储操作 推荐阅读

使用GPT-4开发游戏

CSS overflow

优惠券