4:git对文件的撤销,回退等处理

git对文件的撤销,回退等处理

1:概念了解

工作区:working Directory,就是指我们所创建的项目文件

版本库:repository,就是指工作区域隐藏的目录.git

版本库里面有暂存区:stage
版本库里面有自动创建的分支master
指向master的一个指针HEAD

git add:把文件的修改添加到暂存区
git commit:把文件的修改从暂存区提交到当前分支(master)

2:编辑文件

输入:第四天,我吃了一个西瓜

3:查看文件内容

$ cat test.txt
我今天吃了一个苹果
第二天,我吃了一个梨
第三天,我吃了一个香蕉
第四天,我吃了一个西瓜

4:查看文件修改

$ git diff HEAD -- test.txt
diff --git a/test.txt b/test.txt
index 3306a19..ffee473 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
 我今天吃了一个苹果
 第二天,我吃了一个梨
 第三天,我吃了一个香蕉
+第四天,我吃了一个西瓜
warning: LF will be replaced by CRLF in test.txt.
The file will have its original line endings in your working directory.

5:查看文件状态:

$ git status
On branch master
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:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

6:从工作区丢弃修改的内容

$ git checkout -- test.txt

7:查看文件状态信息

$ git status
On branch master
nothing to commit, working directory clean

8:查看文件修改信息

$ git diff

由于修改内容,已被撤销,所以没有返回任何东西

9:查看文件内容

$ cat test.txt
我今天吃了一个苹果
第二天,我吃了一个梨
第三天,我吃了一个香蕉

10:继续修改内容

输入:第四天,我吃了一个栗子

11:查看文件状态信息

$ git status
On branch master
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:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

12:查看内容:

$ cat test.txt
我今天吃了一个苹果
第二天,我吃了一个梨
第三天,我吃了一个香蕉
第四天,我吃了一个栗子

13:查看修改内容

$ git diff HEAD -- test.txt
diff --git a/test.txt b/test.txt
index 3306a19..9ea84fd 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
 我今天吃了一个苹果
 第二天,我吃了一个梨
 第三天,我吃了一个香蕉
+第四天,我吃了一个栗子
\ No newline at end of file

14:把文件提交到暂存区

git add test.txt

15:查看文件状态信息

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   test.txt

16:撤销暂存区修改

$ git reset HEAD test.txt
Unstaged changes after reset:
M       test.txt

17:查看文件状态信息

$ git status
On branch master
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:   test.txt

18:撤销工作区修改

$ git checkout -- test.txt

19:查看文件状态信息

$ git status
On branch master
nothing to commit, working directory clean

20:编辑修改文件

输入:第四天,我吃了一个芒果

21:查看文件内容

$ cat test.txt
我今天吃了一个苹果
第二天,我吃了一个梨
第三天,我吃了一个香蕉
第四天,我吃了一个芒果

22:查看文件修改

$ git diff HEAD -- test.txt
diff --git a/test.txt b/test.txt
index 3306a19..44fa88f 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
 我今天吃了一个苹果
 第二天,我吃了一个梨
 第三天,我吃了一个香蕉
+第四天,我吃了一个芒果

23:查看文件状态:

$ git status
On branch master
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:   test.txt

no changes added to commit (use "git add" and/or "git commit -a")

24:保存文件到暂存区:

git add test.txt

25:保存文件到版本库

$ git commit -m "我今天吃了一个芒果"
[master fceeff4] 我今天吃了一个芒果
 1 file changed, 1 insertion(+)

26:版本回退到上一个版本

$ git reset --hard HEAD^
HEAD is now at 9f65338 我今天吃了一个香蕉

27:查询文件状态信息

$ git status
On branch master
nothing to commit, working directory clean

28:查询文件内容

$ cat test.txt
我今天吃了一个苹果
第二天,我吃了一个梨
第三天,我吃了一个香蕉

29:查看文件修改信息

$ git diff

由于修改内容,已被撤销,所以没有返回任何东西

30:查询历史提交

$ git log
commit 9f65338fd6e482f7ca9825ac452043784455d413
Author: zhangyanbing <zhangyanbing@benmu-health.com>
Date:   Thu May 5 16:10:50 2016 +0800

    我今天吃了一个香蕉

commit 6cc4f1cf18c9858311b622136f0b43ea17805415
Author: zhangyanbing <zhangyanbing@benmu-health.com>
Date:   Thu May 5 16:04:23 2016 +0800

    我今天吃了一个梨

commit b3d952f7914444238f70410a97f15f50200e63af
Author: zhangyanbing <zhangyanbing@benmu-health.com>
Date:   Thu May 5 15:07:28 2016 +0800

    第一次提交

$ git log --pretty=oneline
9f65338fd6e482f7ca9825ac452043784455d413 我今天吃了一个香蕉
6cc4f1cf18c9858311b622136f0b43ea17805415 我今天吃了一个梨
b3d952f7914444238f70410a97f15f50200e63af 第一次提交

31:版本直接指向第一次提交

$ git reset --hard b3d952
HEAD is now at b3d952f 第一次提交

我还会回来的。。。。

32:查看文件内容

$ cat test.txt
我今天吃了一个苹果

33:查看历史记录

$ git log
commit b3d952f7914444238f70410a97f15f50200e63af
Author: zhangyanbing <zhangyanbing@benmu-health.com>
Date:   Thu May 5 15:07:28 2016 +0800

    第一次提交

34:查看每一次修改的记录

$ git reflog
b3d952f HEAD@{0}: reset: moving to b3d952
9f65338 HEAD@{1}: reset: moving to HEAD^
fceeff4 HEAD@{2}: commit: 我今天吃了一个芒果
9f65338 HEAD@{3}: commit: 我今天吃了一个香蕉
6cc4f1c HEAD@{4}: commit: 我今天吃了一个梨
b3d952f HEAD@{5}: commit (initial): 第一次提交

35:版本回退到最后提交的那个版本

$ git reset --hard fceeff4
HEAD is now at fceeff4 我今天吃了一个芒果

36:查看文件内容

$ cat test.txt
我今天吃了一个苹果
第二天,我吃了一个梨
第三天,我吃了一个香蕉
第四天,我吃了一个芒果

我胡汉三又回来了。。。

posted on 2016-05-05 16:52  借个火点烟  阅读(303)  评论(0编辑  收藏  举报