GitDevOpsgithub

理解 Git

2019-07-03  本文已影响11人  不一样的卡梅利多

主要内容介绍

1.设计思想

2.数据流向

3.git flow

4.其他介绍

5.思考讨论

6.参考资料

1.设计思想

1.快照记录,每一次commit ,记录工作目录的完整快照。
2.不变模式,任何改变都将生成一个新的对象(类似java string 设计)。
3.分布式 ,几乎所有操作都在本地仓库完成(非常高效且无依赖),不同节点可以通过同步来更新内容。
4.复用,使用对象池复用对象,相同内容的文件,在对象池中只会存储一份。

2.数据流向

git数据交互.png

2.1术语

2.2数据流向

1.repo(object-pool) —> work-dir
2.work-dir —>repo(object-pool)
3.git-inter(object-pool) —>merge object (object-pool)-->new object(object-pool)

2.3 主要操作:

  1. 无work-dir, 向repo 写入内容,
  2. 使用work-dir 向repo 写入内容
  3. repo内容检出到work-dir
  4. work-dir,index(cache),repo(commit-tree),比较
  5. commit-tree 构建,新增,合并,重建,撤销
  6. commit-log 查看

2.4 测试结果查看关键命令

查看work-dir 查看repo 内容
ls git ls-files :查看index 文件内容。git ls-files -s:查看index 文件内容,并且显示blob id
tree git ls-tree [commit-id] :查看commit 对应的tree 结构,以及在tree 上面所有的文件
cat git cat-file -t <blob-id>:查看blob 文件类型,blob-id 为对象池里面对应对象的id
find find .git/objects -type f : 查找文件 find ./ -mmin -1 -type f :查找最近一分钟修改的文件 git log:查看commit log, (其他命令git show ) git log --all --graph —oneline:查看所有提交日志 git log --graph --oneline :查看当前分支提交日志
git diff: 比较[repo ,work-dir], [repo,repo],[repo,index],[index,worlk-dir] 其他比较命令:diff-tree/diff-files/diff-index
git status : work-dir ,index,tree 对比后的状态查看
git for-each-ref :查看所有引用(指针)

2.5 测试以及命令解释

2.5.1 无work-dir, 向repo 写入内容

详细描述: https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
主要命令:

2.5.2 使用work-dir 向repo 写入内容

注:流程:工作空间—>缓存(index)—>存储

2.5.3 repo内容检出到work-dir

注:
1.blob 可以用文件路径引用,tree 用commit-id引用,tree 对象比较时,如果是文件增加减少一般可以自动完成操作,如果涉及同一个文件的多次修改,操作可能失败,需要合并tree。
2.流程:工作空间<—缓存(index)<—存储

2.5.4 比较working-tree ,index, tree


work-dir,working-tree 表示相同含义
index ,cache,cache-tree表示相同含义
commit-tree,tree,commit 表示相同含义

2.5.5 tree (commit / commit-tree) 构建,新增,合并,重建,撤销

2.5.6 commit-log 查看

3. git flow

   $ git show 1c002dd4b536e7479fe34593e72e6c6c1819e53b
   $ git show 1c002dd4b536e7479f
   $ git show 1c002d

HEAD@{5}/stash@{1}:reflog 与stash 引用
HEAD^[number] :head 的父节点,可以引用多个父节点(一个节点可能有多个直接父节点)
HEAD~[number] :head的父节点,只能指明第一个父节点
详见: https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection

3.1 分支协作

4 其他介绍

5 思考/讨论

1、怎么合并tree ,以及找到tree 不同,git怎么决定冲突的?

1、 commit1-tree1,commit2-tree2
2、如果commit1是commit2 的祖先,直接使用commit2-tree2 (直接前驱,使用最新版本,无冲突)
3、commit1 与commit2 不是直接没有直接的祖父关系,则比较 tree1 ,tree2 ,
4、tree1,tree2 上面含有相同的文件路径,但是文件内容不同,则产生冲突。需要手动处理。(无法判断那一次的内容是最新版本,)
5、冲突处理方式, 使用 tree1(ours) ,使用tree2 (others),使用手动处理的版本
6、tree1,tree2 合并后产生一个新的tree-new。

2、git status 显示文件状态依据?

依据working-tree ,index(cache),tree 里面的对应的hash 值来判断。

3、怎么显示增量更新内容?

比较两次提交的tree。

4、 commit 操作具体是如何构建tree 的?

从当前 index 文件 构建tree

5、如何实现 git 与远程分支数量对比,本地落后远程几次提交,本地多于远程几次提交?

1、找到远程head 与本地head 共同的祖先A
2、 [A,remote-head] commit 数量为本地落后远程分支数量
3、 [A,local-head] commit 数量为本地多于远程分支数量
4、 git log 可以实现

6、delta storage VS snapshot storage,snapshot 有何优势 ?

1、snapshot 实现简单,高效(复用已有对象),保留了全部内容。可实现的功能更多
2、delta 模式,记录复杂,实现复杂。
3、delta storage VS snapshot storage:

delta vs snapshot.png

7、javer 如何开发git 适用工具 ?

dea4git 实现:
ProcessBuilder pb = new ProcessBuilder(cmdLine)
执行git 命令
解析git 命令输出结果

8、比较算法为何高效?

tree 比较算法介绍: http://thume.ca/2017/06/17/tree-diffing/
文件比较算法介绍: patience|minimal|histogram|myers https://blog.jcoglan.com/2017/09/19/the-patience-diff-algorithm/

9、如何更改commit 日志信息?

git rebase -i (reword):edit the commits which are rebased
git commit --amend : 修改提交commit-msg
注 amend:修改,修正,git rebase 交互模式详情:https://git-scm.com/docs/git-rebase

10、如何让work-tree clean ,不丢内容?

git stash,将work-dir 内容commit 。并checkout HEAD 内容到working-tree

11、如何debug git ?

1、查看.git/logs/ 目录下面日志 ,git 命令执行日志,不是commit(git log 命令) 日志。
2、使用git core (Plumbing Commands)命令
3、使用debug 选项 git ls-files —debug

12、git gc 与jvm gc 有何异同 ?

jvm gc 基于内存,会涉及内存碎片整理,有多种垃圾处理策略(标记整理,标记复制等)
git gc基于磁盘存储,不用整理磁盘碎片
都是基于tree 来查找垃圾对象

6、参考资料

1、Pro Git :https://git-scm.com/book/en/v2
2、 Manual Page https://git.github.io/htmldocs/
3、Pro Git 作者talk : https://www.youtube.com/watch?v=xbLVvrb2-fY
4、帮助 man git 、 git help [command]

上一篇 下一篇

猜你喜欢

热点阅读