为了演示这一功能,你可以进入你的项目,在一些文件上进行工作,有可能还暂存其中一个变更。如果你运行 git status,你可以看到你的中间状态:

$ git status# On branch master# Changes to be committed:# (use "git reset HEAD ..." to unstage)## modified: index.html## Changes not staged for commit:# (use "git add ..." to update what will be committed)## modified: lib/simplegit.rb#

现在你想切换分支,但是你还不想提交你正在进行中的工作;所以你储藏这些变更。为了往堆栈推送一个新的储藏,只要运行 git stash

$ git stashSaved working directory and index state \"WIP on master: 049d078 added the index file"
HEAD is now at 049d078 added the index file(To restore them type "git stash apply")

你的工作目录就干净了:

$ git status# On branch master
nothing to commit (working directory clean)

这时,你可以方便地切换到其他分支工作;你的变更都保存在栈上。要查看现有的储藏,你可以使用 git stash list

$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051... Revert "added file_size"
stash@{2}: WIP on master: 21d80a5... added number to log

在这个案例中,之前已经进行了两次储藏,所以你可以访问到三个不同的储藏。你可以重新应用你刚刚实施的储藏,所采用的命令就是之前在原始的 stash 命令的帮助输出里提示的:git stash apply。如果你想应用更早的储藏,你可以通过名字指定它,像这样:git stash apply stash@{2}。如果你不指明,Git 默认使用最近的储藏并尝试应用它:

$ git stash apply# On branch master# Changes not staged for commit:# (use "git add ..." to update what will be committed)## modified: index.html# modified: lib/simplegit.rb#

你可以看到 Git 重新修改了你所储藏的那些当时尚未提交的文件。在这个案例里,你尝试应用储藏的工作目录是干净的,并且属于同一分支;但是一个干净的工作目录和应用到相同的分支上并不是应用储藏的必要条件。你可以在其中一个分支上保留一份储藏,随后切换到另外一个分支,再重新应用这些变更。在工作目录里包含已修改和未提交的文件时,你也可以应用储藏——Git 会给出归并冲突如果有任何变更无法干净地被应用。

对文件的变更被重新应用,但是被暂存的文件没有重新被暂存。想那样的话,你必须在运行 git stash apply 命令时带上一个 --index 的选项来告诉命令重新应用被暂存的变更。如果你是这么做的,你应该已经回到你原来的位置:

$ git stash apply --index# On branch master# Changes to be committed:# (use "git reset HEAD ..." to unstage)## modified: index.html## Changes not staged for commit:# (use "git add ..." to update what will be committed)## modified: lib/simplegit.rb#

apply 选项只尝试应用储藏的工作——储藏的内容仍然在栈上。要移除它,你可以运行 git stash drop,加上你希望移除的储藏的名字:

$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051... Revert "added file_size"
stash@{2}: WIP on master: 21d80a5... added number to log
$ git stash drop stash@{0}Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)

你也可以运行 git stash pop 来重新应用储藏,同时立刻将其从堆栈中移走。

!爆享折扣!

▼▼▼原价129今日拼团仅需 ¥99人专享首单限时优惠 ¥19.9!!!但!仅限前100个名额???

1e0ff81f59285f19f04058d9ee38361b.png

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐