Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1478837
  • 博文数量: 228
  • 博客积分: 1698
  • 博客等级: 上尉
  • 技术积分: 3241
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-24 21:49
个人简介

Linux

文章分类

全部博文(228)

文章存档

2017年(1)

2016年(43)

2015年(102)

2014年(44)

2013年(5)

2012年(30)

2011年(3)

分类: LINUX

2016-01-12 23:29:22

准备一个现场,工作区修改一个文件,新增一个文件(添加到暂存区)

点击(此处)折叠或打开

  1. [root@S-LAB-52 demo]# git status -s
  2. A a/b/c/hello_test.txt
  3.  M welcome.txt
保存工作现场
点击(此处)折叠或打开
  1. [root@S-LAB-52 demo]# git stash
  2. Saved working directory and index state WIP on (no branch): 6ca2d9e add new welcome.txt
  3. HEAD is now at 6ca2d9e add new welcome.txt
保存现场后,再查看当前的状态

点击(此处)折叠或打开

  1. [root@S-LAB-52 demo]# git status -s
  2. [root@S-LAB-52 demo]# git status 
  3. Not currently on any branch.
  4. nothing to commit (working directory clean)
下面我们看一下如何恢复?

查看保存的进度
点击(此处)折叠或打开
  1. [root@S-LAB-52 demo]# git stash list
  2. stash@{0}: WIP on (no branch): 6ca2d9e add new welcome.txt
恢复进度
点击(此处)折叠或打开
  1. [root@S-LAB-52 demo]# git stash pop
  2. Not currently on any branch.
  3. # Changes to be committed:
  4. (use "git reset HEAD ..." to unstage)
  5. #
  6. #    new file: a/b/c/hello_test.txt
  7. #
  8. # Changed but not updated:
  9. (use "git add ..." to update what will be committed)
  10. (use "git checkout -- ..." to discard changes in working directory)
  11. #
  12. #    modified: welcome.txt
  13. #
  14. Dropped refs/stash@{0} (8054d86ce601e186f106a9567162e284cb1eace7)
  15. [root@S-LAB-52 demo]# git status -s
  16. A a/b/c/hello_test.txt
  17.  M welcome.txt
可以看到git status -s的显示,进度已经找回来了。

下面对找回的状态进行一些操作,看是否是正常的?

1. 提交暂存区的内容
点击(此处)折叠或打开
  1. [root@S-LAB-52 demo]# git status -s
  2. A a/b/c/hello_test.txt
  3.  M welcome.txt 
  4. [root@S-LAB-52 demo]# git commit -"commit new file a/b/c/hello_test.txt"
  5. [detached HEAD 77b95e6] commit new file a/b/c/hello_test.txt
  6.  0 files changed, 0 insertions(+), 0 deletions(-)
  7.  create mode 100644 a/b/c/hello_test.txt
  8. [root@S-LAB-52 demo]# 
  9. [root@S-LAB-52 demo]# git status -s
  10.  M welcome.txt
早期,hello_test.txt已经加入了暂存区,而welcome.txt的修改还在工作区。直接提交hello_test.txt,然后查看,只剩下工作区的welcome.txt的修改了。

2. 回退提交的内容

点击(此处)折叠或打开

  1. [root@S-LAB-52 demo]# git reset --soft HEAD^
  2. [root@S-LAB-52 demo]# git status -s
  3. A a/b/c/hello_test.txt
  4.  M welcome.txt
--soft只回退引用的指向,不影响工作区和暂存区,所以回退之后查看status,工作区和暂存区分别是M和A

3. 提交welcome.txt增加到暂存区
点击(此处)折叠或打开
  1. [root@S-LAB-52 demo]# git add welcome.txt 
  2. [root@S-LAB-52 demo]# git status -s
  3. A a/b/c/hello_test.txt
  4. M welcome.txt
可以看到M的位置由第2列跑到了第1列,第1列表示暂存区和版本库之间的差异,第2列表示工作区和暂存区之间的差异

4. 将hello_test.txt从暂存区撤销
点击(此处)折叠或打开
  1. [root@S-LAB-52 demo]# git reset HEAD a/b/c/hello_test.txt
  2. [root@S-LAB-52 demo]# git status -s
  3. M welcome.txt
  4. ?? a/b/c/hello_test.txt
用到了git reset的第一种用法,即指定paths。用HEAD指向的目录树中文件覆盖暂存区中的hello_test.txt,
此时暂存区中的hello_test.txt与HEAD指向的版本库中相同,而工作区中仍保留了早期的hello_test.txt的状态,因此前面提示???

5. 将welcome.txt从暂存区撤销
点击(此处)折叠或打开
  1. [root@S-LAB-52 demo]# git reset
  2. Unstaged changes after reset:
  3. M    welcome.txt
  4. [root@S-LAB-52 demo]# git status -s
  5.  M welcome.txt
  6. ?? a/b/c/hello_test.txt
git reset与git reset HEAD相同,用HEAD指向的目录树重置暂存区,因此提交到暂存区的welcome.txt被撤销了。welcome.txt前面的M变到了第2列

6. 删除本地工作区的所有修改
点击(此处)折叠或打开
  1. [root@S-LAB-52 demo]# git status -s
  2.  M welcome.txt
  3. ?? a/b/c/hello_test.txt
  4. [root@S-LAB-52 demo]# git checkout -- welcome.txt
  5. [root@S-LAB-52 demo]# git status -s
  6. ?? a/b/c/hello_test.txt
我们一步一步来,先删除welcome.txt,用到checkout -- file_path的方式,表示用暂存区的file_path覆盖工作区的file_path。
再来删除hello_test.txt,由于删除本地多余的目录和文件的clean命令比较危险,先看一下哪些文件和目录会被删除,然后再删除

点击(此处)折叠或打开

  1. [root@S-LAB-52 demo]# git clean -nd
  2. Would remove a/b/c/hello_test.txt
  3. [root@S-LAB-52 demo]# git clean -fd
  4. Removing a/b/c/hello_test.txt
  5. [root@S-LAB-52 demo]# git status -s
-nd 确认哪些将会被删除,-fd 执行删除的动作,然后查看当前状态,工作区已经干净!

下面再来看一下git stash的具体用法:
git stash:  保存当前的工作进度
git stash list:显示进度列表,git stash可以多次保存工作栈,可以在恢复时进行选择

git stash pop [--index] []
当不用任何参数时,会恢复最新保存的工作进度,并将恢复的工作进度从存储的工作进度列表中删除
当提供参数时(git stash list显示的列表),则从该中恢复,恢复后也从进度列表中删除
--index指出除了恢复工作区的文件外,同时尝试恢复暂存区
 
git stash [save  [--patch]  [-k | -- [no-]  keep-index ] [-q | --quiet]  []
--patch会显示工作区和HEAD的差异
-k或--keep-index参数,保存进度后不会将暂存区重置,而默认会将暂存区和工作区强制重置
当保存工作进度时,需要使用指定的说明,需要使用以下格式:git stash save "message......."

git stash apply  [--index]  []
除了不删除恢复的进度外,其余和git stash pop相同
git stash drop []
删除存储的进度,默认删除最新的进度
git stash clear
删除所有存储的进度
git stash branch []
基于进度创建分支

注意,没有被版本控制系统跟踪的文件不能保存进度
阅读(1987) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~