图解Git内部结构

tree
高效比较
Git 比较版本间差异时,只需对比 tree 的哈希值,而不是整个文件内容
git diff commitA commitB
Git 内部做的是:
- 拿
commitA和commitB各自指向的tree - 对这两个
tree做递归比较 - 得出哪些文件改了
git checkout <commit>
Git 做的其实是:
-
根据该 commit 中的 tree 哈希
-
把那个 tree 下所有的文件结构恢复出来
-
让你的工作区“变成那个时候的样子”
| 操作 | 命令 |
|---|---|
| 获取某个 commit 的 tree | git cat-file -p <commit_hash> |
| 查看 tree 的结构 | git cat-file -p <tree_hash> |
| 查看某个文件内容 | git cat-file -p <blob_hash> |
| 查看对象类型 | git cat-file -t <hash> |

数字的含义
文件或目录的权限(mode),例如:
100644表示普通文件100755表示可执行文件040000表示目录(即 tree)
Git命令
git log --all --graph --decorate
git log --all --graph --decorate --oneline
git log -vv 用来查看提交历史并附加本地分支与远程分支的状态。
git branch -vv 详细告诉所有分支信息
git remote add <name> <url> 添加远程仓库
git push <remote name> <local branch>:<remote branch>
git clone <url> <folder name>
git branch --set-upstream-to=origin/master 设置上游仓库
git fetch <remote name>
git pull = git fetch + git merge
进阶:
vim ~/.gitconfig
git clone --shallow 不克隆版本记录,只有最近一次快照
git add -p xxx.txt 交互式地觉得哪些修改需要暂存
git diff --cached 显示暂存区的更改(区别于git diff 显示实际更改)
git blame 和 git show 见https://note.youdao.com/s/HfYNXJkd
git stash 暂时丢弃更改恢复原状(实际上保存在了某个地方);git stash pop 恢复更改
git bisect 找到单元测试不在通过的第一个提交(使用二分法)