git结构图
工作区:文件的最直接的增删改都发生在工作区。
暂存区(state或index):实际就是.git目录下的index文件,它是一个包含文件索引的目录树,记录了文件名和文件状态(长度,大小)和指向object的对应关系,而文件的内容则保存在.git/objects中。
版本库:也就是.git/objects中的对象库。
删除和恢复情况:
假如工作区添加了新文件gitTest.php,并已添加到暂存区和版本库
1.rm gitTest.php 只会删除工作区的文件,暂存区索引和本版库内容不改变
暂存区->工作区: git checkout gitTest.php
版本库->工作区:git checkout HEAD gitTest.php
2.git rm gitTest.php 会删除工作区和暂存区的gitTest.php的相关数据(git commit后会删除版本库内容)
版本库->工作区+暂存区:git checkout HEAD gitTest.php
3.git rm --cached gitTest.php 只会删除暂存区gitTest.php内容
工作区->暂存区:git add gitTest.php
版本库->暂存区: git checkout HEAD gitTest.php
注意:
HEAD是指当前分支最新的一次提交,也就是版本库中该分支的最新版本
git checkout . 或者git checkout - 会用暂存区所有的文件代替工作区中的文件,命令很敏感,慎用。
总结:git chekcout HEAD file 版本库->暂存区+工作区 git checkout file 暂存区->工作区 git checkout 分支名 切换分支
git diff 查看两个区的差别:
git diff 工作区vs暂存区
git diff --cached 暂存区vs版本库
git diff HEAD 工作区vs版本库