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
Changes to be committed
是说,在暂存区有一个new.txt
文件等待着被提交,Changes not staged for commit:
是说new.txt
文件已经被修改,但是还没有被放入暂存区,如果此时执行git commit
命令的话,提交的只是修改之前的new.txt
文件。在此状态下可以执行
git add new.txt
命令,把已经修改过的new.txt
文件添加到暂存区,也就是说,用修改过的new.txt
文件替换修改之前的文件。在此状态下,也可以使用
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
。
- git-工作区-暂存区-仓库(五)
- git-工作区-暂存区-仓库(六)
- git的工作区、暂存区、本地仓库、远程仓库
- Git工作区,暂存区,远程仓库及分支管理
- Git使用手册:Git的使用:工作区、暂存区、本地仓库、远程仓库
- Git 工作区、暂存区
- Git的使用教程(五)工作区和暂存区
- git图形化工具GitKraken的使用——工作区、暂存区、本地仓库
- Git学习(五)----Git工作区&暂存区&版本库(“三巨头”)、删除文件、撤销操作
- 五.工作区和暂存区
- git基础学习(四):工作区和暂存区
- Git学习笔记(四)---工作区与暂存区
- git的学习(工作区与暂存区)
- Git-基本结构(工作区和暂存区-笔记)
- git 工作区、暂存区、存储库
- Git-工作区和暂存区
- Git-03_工作区-暂存区
- Git教程:工作区和暂存区
- POJ-3253-Fence Repair(Java+哈夫曼树)
- FZU(2188):狼羊过河问题,状态转移,BFS求解
- 代码 AutoLayout
- 二叉查找树镜像翻转
- Powerbuilder中的内存操作大搜集
- git-工作区-暂存区-仓库(五)
- (1.1.15)对象成员变量的构造函数和对象构造函数的执顺序以及Static、final、abstract、interface
- ubuntu 设置输入法
- 《Android内核剖析》读书笔记 第1章 Linux基础
- FTP与SFTP的区别
- elipse 和 MyEclipse中的还原和重置窗口布
- WM_COPYDATA的作用
- java基础-抽象类与抽象方法
- Microsoft Jet 数据库引擎打不开文件’(未知的)’。 它已经被别的用户以独占方式打开