1、git操作说明

- 1、
commit
:对当前操作目录、资源文件、代码等的一个快照 - 2、
diff
:commit与commit之间的差异 - 3、
branch
:分支--在一个commit之间进行分开的时候 - 4、
merge
:合并--两个commit合并到一起 -
repository
:仓库--记录上面四个操作的过程叫仓库
Git与SVN的差别
svn存储的是diff
,这就意味着我们在当前commit
下,想要回到之前指定的某个commit
,只能一个一个进行回撤
git存储的是整个快照commit
,如果我们不对.git
文件进行处理,会导致.git
文件越来越大
git工作流程

我们平时都是在workspace
工作区中,然后通过git add
提交到Index\Stage
索引区、暂存区,再通过git commit
提交到Repository
仓库,仓库再通过git push
到远程仓库,远程仓库再通过pull
或者fetch/clone -- > checkout
到本地仓库

git reset与git checkout命令
HEAD
HEAD
是用来指向分支的最后一次提交
git reset
:回退版本,可以指定退回到某次提交的版本
git reset --soft HEAD~1
:回退到上次提交的暂存区,不会改变当前工作区
git reset HEAD .
:移除当前暂存区文件
git reset --hard HEAD~1
:回退到上次提交的工作目录,会改变当前工作区
git checkout master
:切换到master分支
git checkout -- .
:重新存储工作区文件到暂存区
Git key-value⽂件系统
我们在使用Git时会发现无法提交空目录,那是因为在Git中是没有目录的概念,只有文件,使用过保存路径来对应相应的文件
如果我们想提交一个空目录,那我们可以在目录下创建.gitkeep
或者.keep
文件,然后在项目的.gitignore
中设置不忽略.gitkeep
我们在提交修改commit
的时候,git
会根据本次提交修改的文件的哈希值
为Key
,修改的文件的压缩版本为Value
,通过树结构
来存储提交

git hash-object
:计算文件的哈希值作为key
存到系统里,文件内容作为value
存放到.git
目录
git update-index
:将文件计算出来的哈希值存储到暂存区
git write-tree
:将暂存区
的内容生成树结构(本质上也是个哈希值
),然后将树结构提交到commit tree

.git
目录会存储每次文件修改的压缩版本和每次提交的commit
信息的压缩版本
Git对象存储
Git将存储对象的40位HASH分为两部分:
- 头两位作为⽂件夹
- 后38位作为对象⽂件名 .git/objects/hash[:2]/hash[2:40]
为什么要这么设计⽬录结构,⽽不直接使⽤40位hash作为⽂件名?
- 部分⽂件系统对⽬录下的⽂件数量有限制。例如,FAT32限制单⽬录下的最⼤⽂件数量是65535个。
- 部分⽂件系统查找⽂件属于线性查找,⽬录下的⽂件越多,访问越慢。
git init
后的.git
文件目录如下图所示

git add .
后.git
文件目录如下图所示
git cat-file -p
打印哈希值对应的信息


Git index⽂件
Git在 .git ⽂件夹下⾯存放了 index
⽂件,该⽂件表示Git stage
的内
容。该⽂件是⼆进制⽂件
,保存了被stage的⽂件的所有信息,像inode信息、hash值等等


hexdump
hexdump
命令查看index
文件二进制数据信息


git ls-files -s
git ls-files -s
查看暂存区的文件具体信息
100644
对应的是⼀个mode
值,代表普通⽂件
。在Git⾥,⼏个常⽤的mode值包括:
- 100644 - 普通⽂件;
- 100755 - 可执⾏⽂件;
- 120000 - 符号链接(symbolic link);
- 040000 - ⽬录;

git hash-object
git hash-object
:计算⼀个⽂件的git对象ID,即SHA1的哈希值
git hash-object -w
:将指定对象(哈希值)写⼊数据库
当文件内容没有改变,对应的哈希值不会改变

git update-index
git update-index
:将⼯作⽬录的⽂件加⼊索引/暂存区域

git write-tree
git write-tree
:生成文件树,本质也是个哈希值,也是存放在.git/objects
目录下

git read-tree
git read-tree
:将给出的树写⼊索引但不写⼊暂存

git commit-tree
git commit-tree
:将给出的树提交到仓库
远程仓库
创建一个仓库,就可以提交代码到该仓库
git init --bare
初始化git环境
git init
添加一个远程仓库,可以添加多个remote
git remote add

拉取远程代码合并到本地仓库
git pull

提交本地代码到远程仓库
git push --set upstream origin master

当远程的master没有新的commit
的时候,会将新提交的commit放到master分支最后
,如果有新的commit会快速合并失败报错

git mergetool
打开图形界面,解决合并冲突
git rebase
并不是创建一个新的commit,而是将指定的一个分支放到另一个分支的结尾
git rebase origin/master
网友评论