Git常用操作之stash操作

      当你突然遇到紧急情况需要修改bug但手上的开发任务还没有完全完成时,你希望Git可以把当前的工作内容先保存起来,让你能拉出一条bug修复分支,在完成bug修复后再回到之前没完成的工作状态。聪明的Git早就帮你想到了这个问题的解决方案--stash命令。

      1.当你在dev分支上有修改文件

$ git status
# On branch dev
# Your branch and 'origin/dev' have diverged,
# and have 1 and 1 different commit each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   3.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

  2.使用git stash来保存工作空间

$ git stash
warning: LF will be replaced by CRLF in 3.txt.
The file will have its original line endings in your working directory.
Saved working directory and index state WIP on dev: e416f28 add something new in 2.txt
HEAD is now at e416f28 add something new in 2.txt

   可以看到Git提示你工作空间和暂存区都已经保存了,并且是保存在e416f28这个版本上,还指明了HEAD的位置。

      3.查看Git状态

$ git status
# On branch dev
# Your branch and 'origin/dev' have diverged,
# and have 1 and 1 different commit each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
nothing to commit, working directory clean

  工作空间和暂存区都是干净的。你现在就可以进行你的bug修复工作了。

   4.使用git stash list来查看保存的工作空间

$ git stash list
stash@{0}: WIP on dev: e416f28 add something new in 2.txt

  可以看到stash中只保存了一个,也就是之前我们保存的e416f28。

      5.恢复现场

$ git stash pop
# On branch dev
# Your branch and 'origin/dev' have diverged,
# and have 1 and 1 different commit each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   3.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

  查看stash list中发现已经没有保存的空间了,所以证明pop动作是把工作空间给恢复,然后在list中删掉了这个空间。

      6.指定恢复工作空间

$ git stash list
stash@{0}: WIP on dev: e416f28 add something new in 2.txt
stash@{1}: WIP on dev: e416f28 add something new in 2.txt

$ git stash apply stash@{1}
# On branch dev
# Your branch and 'origin/dev' have diverged,
# and have 1 and 1 different commit each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   3.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

  在stash中保存了多个现场,可以使用apply来指定恢复那个现场。但是在list中仍然是保存了那个现场的。这点是与pop操作不同的。

  

posted on 2017-11-01 10:31  取个名字真的很难  阅读(1133)  评论(0编辑  收藏  举报

导航