📜  Git-隐藏操作

📅  最后修改于: 2020-11-01 03:55:35             🧑  作者: Mango


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

在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