Git关于文件的状态

一、自述

  在粗略学习完Git教程,特别是对分支管理这一章节的学习,有点力不从心,感觉学习知识优点杂,所有特地在写下这篇文章,弄清楚这其中涉及的知识点。

二、git status的使用

1、将learngit文件夹变为仓库后,第一次使用git status查看仓库状态

1 $ git status
2 On branch master  一个主分支master
3 
4 No commits yet  还没有提交过
5 
6 nothing to commit (create/copy files and use "git add" to track)  没有提交的内容(创建/复制文件并使用“git add”进行跟踪)

2、创建文件readme.txt,写入内容Hello world!,第二次使用git status查看仓库状态

 1 $ git status
 2 On branch master
 3 
 4 No commits yet
 5 
 6 Untracked files:  没有追踪的文件
 7   (use "git add <file>..." to include in what will be committed)  使用git add <file>去添加它,并提交
 8 
 9         readme.txt  文件名
10 
11 nothing added to commit but untracked files present (use "git add" to track)  
没有添加的内容供提交,但是目前没有添加的文件可以使用git add来追踪

 此时此刻,Git没有对readme.txt文件进行管理的权限(没有进行add,也就是追踪,记录到暂存区)

3、添加文件到暂存区,第三次使用git status来查看仓库状态

1 $ git status
2 On branch master
3 
4 No commits yet
5 
6 Changes to be committed:  已暂存、已提交(暂存区)
7   (use "git rm --cached <file>..." to unstage)  使用“git rm--cached<file>…”来取消存储
8 
9         new file:   readme.txt

4、修改readme.txt,增加一行Hello Git!,第四次使用git status查看仓库状态

 1 $ git status
 2 On branch master
 3 
 4 No commits yet
 5 
 6 Changes to be committed:     保存的内容为Hello world!
 7   (use "git rm --cached <file>..." to unstage)
 8 
 9         new file:   readme.txt
10 
11 Changes not staged for commit:
12   (use "git add <file>..." to update what will be committed)    更新提交
13   (use "git checkout -- <file>..." to discard changes in working directory)  可以丢弃工作区的修改
14 
15         modified:   readme.txt  修改过的文件

5、添加文件到暂存区,第五次使用git status来查看仓库状态

1 $ git status
2 On branch master
3 
4 No commits yet
5 
6 Changes to be committed:
7   (use "git rm --cached <file>..." to unstage)
8 
9         new file:   readme.txt

6、提交文件到本地库,第六次使用git status来查看仓库状态

1 $ git status
2 On branch master
3 nothing to commit, working tree clean  工作区为空(工作区文件内容与版本库一致)

三、撤销修改

为了方便学习,重新配置仓库,对仓库进行初始化(readme.txt文件内容只有一句,Hello world!,没有add,也没有commit)

1、对文件进行add,然后增加内容Hello Git!,查看状态

 1 $ git status
 2 On branch master
 3 
 4 No commits yet
 5 
 6 Changes to be committed:
 7   (use "git rm --cached <file>..." to unstage)
 8 
 9         new file:   readme.txt
10 
11 Changes not staged for commit:
12   (use "git add <file>..." to update what will be committed)
13   (use "git checkout -- <file>..." to discard changes in working directory)  可以看见这句话

使用命令git checkout -- <file>退回到暂存区的状态

1 $ git checkout -- readme.txt
2 $ cat readme.txt
3 Hello world!

2、对文件进行add,查看状态

1 $ git status
2 On branch master
3 
4 No commits yet
5 
6 Changes to be committed:
7   (use "git rm --cached <file>..." to unstage)
8 
9         new file:   readme.txt

要想撤销暂存区的内容使用以下命令:

1 $ git reset HEAD readme.txt
2 fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
3 Use '--' to separate paths from revisions, like this:
4 'git <command> [<revision>...] -- [<file>...]'

竟然会出错,不科学啊!原来是这个文件没有提交过,也就是没有进行commit,再次尝试。

先将有Hello world!文件进行提价,再次进行操作。

扫描二维码关注公众号,回复: 6972364 查看本文章
1 $ git reset HEAD readme.txt
2 Unstaged changes after reset:
3 M       readme.txt
4 
5 $ git checkout -- readme.txt  这一步必须要有
6 
7 $ cat readme.txt
8 Hello world!

四、删除文件

1、手动删除了readme.txt文件,可以恢复

$ rm readme.txt

$ ll
total 0


$ git checkout -- readme.txt  将暂存区的文件恢复到工作区

$ ll
total 1
-rw-r--r-- 1 86173 197609 13 8月   7 12:00 readme.txt

2、手动删除了readme.txt文件,并且删除了暂存区的文件

 1 $ rm readme.txt
 2 
 3 $ ll
 4 total 0
 5 
 6 $ git rm readme.txt
 7 rm 'readme.txt'
 8 
 9 
10 $ git reset HEAD readme.txt    将版本库里的文件恢复到暂存区
11 Unstaged changes after reset:
12 D       readme.txt
13 
14 $ ll
15 total 0
16 
17 $ git checkout -- readme.txt  将暂存区的文件恢复到工作区
18 
19 $ ll
20 total 1
21 -rw-r--r-- 1 86173 197609 13 8月   7 12:06 readme.txt

猜你喜欢

转载自www.cnblogs.com/lilong74/p/11313394.html