git-工作区-暂存区-仓库(五)

来源:互联网 发布:java打印等边三角形用 编辑:程序博客网 时间:2024/05/16 14:31

git-工作区-暂存区-仓库操作

实践出真知,此处进行验证。

1.从github.com克隆一个现有的demo仓库da,此仓库只有一个README.md文件,执行命令:

git clone https://github.com/ic-end/demo.git

2.用cd命令切换到demo目录

3.用touch命令在demo目录下创建文件new.txt,此时该文件位于工作区。

touch new.txt

4.此时执行git status命令检测状态,

Untracked files:  (use "git add <file>..." to include in what will be committed)        new.txt

意思是说:new.txt文件处于untracked状态,可以使用git add <file>命令将文件添加到提交清单中,也就是放到暂存区。

5.执行rm <file>命令可以把工作区中的文件删除。

rm new.txt

注意,是rm new.txt,并不是git rm new.txt两者的意义不同,下面会说到git rm

6.此时执行git status命令检测状态,

nothing to commit, working directory clean

也就是没有文件更改,没有文件需要提交。

7.重新创建一个new.txt文件,并把它添加到暂存区。

touch new.txtgit add new.txt

8.此时执行git status命令检测状态,

Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        new file:   new.txt

文件new.txt的状态变为了Changes to be committed,也就是说已经放在暂存区并生成快照,等待被提交。

9.用git rm --cached <file>命令删除暂存区中的文件。

git rm --cached new.txt

如果执行git rm new.txt文件,则会从暂存区和工作区同时删除new.txt

10.此时执行git status命令检测状态,

Untracked files:  (use "git add <file>..." to include in what will be committed)        new.txt

可以看出,已经返回到第4步的状态,也就是说git add new.txt之前的状态。

11.执行git add new.txt,跳转到第7步的状态,此时执行git status命令检测状态,

Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        new file:   new.txt

12.此时如果打开文件new.txt,并输入Hello World!,然后执行git status检查状态,

Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        new file:   new.txtChanges 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:   new.txt
  1. Changes to be committed是说,在暂存区有一个new.txt文件等待着被提交,Changes not staged for commit:是说new.txt文件已经被修改,但是还没有被放入暂存区,如果此时执行git commit命令的话,提交的只是修改之前的new.txt文件。

  2. 在此状态下可以执行git add new.txt命令,把已经修改过的new.txt文件添加到暂存区,也就是说,用修改过的new.txt文件替换修改之前的文件。

  3. 在此状态下,也可以使用git checkout -- new.txt命令,用暂存区中未修改的new.txt文件覆盖工作区中修改过的new.txt文件。

13.使用git commit -m "first commit"命令提交文件

git commit -m "first commit"

14.此时执行git status命令检测状态,

On branch masterYour branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)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:   new.txtno changes added to commit (use "git add" and/or "git commit -a")

可见,此时是把暂存区中修改前的new.txt提交到master指向仓库了,而工作区中还是修改过的new.txt

15.使用git checkout HEAD new.txt命令从HEAD指向的master分支中恢复new.txt文件

git checkout HEAD new.txt

16.此时执行git status命令检测状态,

nothing to commit, working directory clean

此时,工作区中的new.txt文件也变成了修改前的状态。

17.如果在第14步之后执行git add new.txt,并检查状

git add new.txtgit status

输出结果如下:

Your branch is ahead of 'origin/master' by 1 commit.  (use "git push" to publish your local commits)Changes to be committed:  (use "git reset HEAD <file>..." to unstage)        modified:   new.txt

此时,工作区和暂存区中都是修改过的new.txt文件。

18.此时执行git reset <file>命令,用master分支指向的目录树替换暂存区的目录树。

git reset new.txt

19.此时执行git status命令检测状态,

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:   new.txtno changes added to commit (use "git add" and/or "git commit -a")

回到第14步的状态,此时暂存区中是修改前的new.txt,而工作区中还是修改过的new.txt

0 0
原创粉丝点击