git stash储藏未完成的工作
2013-07-25 11:03
295 查看
有时候,你还没完成工作,不想提交,但又有新任务,必须切换到另一个分支,就用stash
“‘储藏”“可以获取你工作目录的中间状态——也就是你修改过的被追踪的文件和暂存的变更——并将它保存到一个未完结变更的堆栈中,随时可以重新应用。
Dark@Dark-laptop:~/box_4.2$ git stash
会提示:
No local changes to save
表明stash
正文
运行 git status,你可以看到你的中间状态:
现在你想切换分支,但是你还不想提交你正在进行中的工作;所以你储藏这些变更。为了往堆栈推送一个新的储藏,只要运行 git stash:
你的工作目录就干净了:
这时,你可以方便地切换到其他分支工作;你的变更都保存在栈上。要查看现有的储藏,你可以使用 git stash list:
在这个案例中,之前已经进行了两次储藏,所以你可以访问到三个不同的储藏。你可以重新应用你刚刚实施的储藏,所采用的命令就是之前在原始的 stash 命令的帮助输出里提
示的:git stash apply。如果你想应用更早的储藏,你可以通过名字指定它,像这样:git stash apply stash@2。如果你不指明,Git 默认使用最近的储藏并尝试应用它:
你可以看到 Git 重新修改了你所储藏的那些当时尚未提交的文件。在这个案例里,你尝试应用储藏的工作目录是干净的,并且属于同一分支;但是一个干净的工作目录和应用到相
同的分支上并不是应用储藏的必要条件。你可以在其中一个分支上保留一份储藏,随后切换到另外一个分支,再重新应用这些变更。在工作目录里包含已修改和未提交的文件时,你也可以应用储藏——Git 会给出归并冲突如果有任何变更无法干净地被应用。
对文件的变更被重新应用,但是被暂存的文件没有重新被暂存。想那样的话,你必须在运行 git stash apply 命令时带上一个 --index 的选项来告诉命令重新应用被暂存的变更。
如果你是这么做的,你应该已经回到你原来的位置:
apply 选项只尝试应用储藏的工作——储藏的内容仍然在栈上。要移除它,你可以运行git stash drop,加上你希望移除的储藏的名字:
你也可以运行 git stash pop 来重新应用储藏,同时立刻将其从堆栈中移走。
你会碰到一个归并冲突并且必须去化解它。如果你想用更方便的方法来重新检验你储藏的变更,你可以运行 git stash branch,这会创建一个新的分支,检出你储藏工作时的所处的
提交,重新应用你的工作,如果成功,将会丢弃储藏
“‘储藏”“可以获取你工作目录的中间状态——也就是你修改过的被追踪的文件和暂存的变更——并将它保存到一个未完结变更的堆栈中,随时可以重新应用。
注意事项
需注意上面那句话所对作用范围的定义,如果你的文件没add,即没有“被跟踪”和“暂存”,stash是不会帮你保存的。例如,我增加了两个文件Readme,readme,我并没有git add这两个新文件,然后输入以下命令Dark@Dark-laptop:~/box_4.2$ git stash
会提示:
No local changes to save
表明stash
正文
运行 git status,你可以看到你的中间状态:$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: index.html # # Changed but not updated: # (use "git add <file>..." to update what will be committed) # # modified: lib/simplegit.rb
现在你想切换分支,但是你还不想提交你正在进行中的工作;所以你储藏这些变更。为了往堆栈推送一个新的储藏,只要运行 git stash:
$ git stash Saved 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 # Changed but not updated: # (use "git add <file>..." 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 <file>..." to unstage) # # modified: index.html # # Changed but not updated: # (use "git add <file>..." 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 来重新应用储藏,同时立刻将其从堆栈中移走。
捷径——从储藏中创建分支
如果你储藏了一些工作,暂时不去理会,然后继续在你储藏工作的分支上工作,你在重新应用工作时可能会碰到一些问题。如果尝试应用的变更是针对一个你那之后修改过的文件,你会碰到一个归并冲突并且必须去化解它。如果你想用更方便的方法来重新检验你储藏的变更,你可以运行 git stash branch,这会创建一个新的分支,检出你储藏工作时的所处的
提交,重新应用你的工作,如果成功,将会丢弃储藏
$ git stash branch testchanges Switched to a new branch "testchanges" # On branch testchanges # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # modified: index.html # # Changed but not updated: # (use "git add <file>..." to update what will be committed) # # modified: lib/simplegit.rb # Dropped refs/stash@{0} (f0dfc4d5dc332d1cee34a634182e168c4efc3359)这是一个很棒的捷径来恢复储藏的工作然后在新的分支上继续当时的工作。
相关文章推荐
- 使用git stash储藏你的工作
- git stash 缓存当前工作
- Git工作区储藏兼谈分支管理中的一个小问题
- git暂存工作区stash
- git stash 缓存当前工作
- GIT常用技能系列【Stash:将你的工作代码暂存起来】
- git stash命令:保存工作现场,恢复工作现场 的使用方法
- git入门(2)-Git stash保存当前的工作现场
- git入门(2)-Git stash保存当前的工作现场
- git stash暂存当前正在进行的工作
- git stash妙用-保存工作现场
- git命令之git stash(暂时保留工作进度)
- git stash命令保存工作区和暂存区的改变
- Git flow基本工作流程介绍
- Git 团队开发工作流
- 玩转git,让git成为个人工作备份利器(即使是电脑小白也推荐学习)
- Git工作区和暂存区
- Git Stash用法
- Git 分支-利用分支进行开发的工作流程
- 7.4 Git 工具 - 签署工作