【Missing Semester L6】Git背后的data model及常用git命令

在这里插入图片描述


link

Git的数据模型

快照

git会把文件和文件夹的历史建模为一系列快照(snapshots),其中文件又叫"blob",就是一堆字节;文件夹又叫"tree",包含对文件和文件夹名字的映射(毕竟文件夹里也可以有文件夹)。
某一时刻的一个快照可以看作是:
在这里插入图片描述
最顶层的tree包含一个叫foo的文件夹和一个叫baz.txt的文件,然后foo下面又包含了一个叫bar.txt的文件。

历史

git通过相关的快照来建模历史。最简单的想法,自然是顺序的历史,按时间顺序包含一系列快照。不过Git没有采用如此简单的模型。
事实上,在Git种,历史建模为快照的有向无环图。
可以把一个个快照看作是一个个节点,区别于线性模型的是,在图里,一个节点可以有多个父节点。比如在git种,分支合并后得到的快照节点就会有多个父节点。
Git种把这些快照叫做“Commit”,对一个commit的历史进行可视化展示,可能会长这样:
在这里插入图片描述
图里每个“o"代表一个快照,箭头指向父节点,拿第一排来说,现有坐起第一个o,然后才有第二个o这样。第三个快照之后,出现了分支,比如说有两个不同的功能要进行平行的开发,最后可能进行分支合并得到新的快照,比如:
在这里插入图片描述
Git中的commit是不可变的,改动后只会生成新的commit。

数据模型

用伪代码来表示Git的数据模型可以如下图所示:
在这里插入图片描述

对象和content-addressing

Object,对象,在Git里指的是一个blog、一个tree或者一个commit。

type object = blob | tree | commit

在Git的数据存储中,所有的object是通过哈希(SHA-1 hash)来进行内容寻址的(content-addressing)。
在这里插入图片描述
可以理解为每个object通过哈希算出一个ID,然后根据这个ID来获得object。这个ID其实就是我们查看GIt历史的时候那一个个很长的字符串。而且blob tree commit都可以看作是object,当object引用其他object的时候,只要引用哈希后的ID就行。

比如,在这个结构里,可视化一下最顶层的结构(git cat-file -p 698281bc680d1995c5f4caaf3359721a5a58d48d),
在这里插入图片描述
可以看到类似这样的结果:
在这里插入图片描述
顶层结构包含指向baz.txt和foo文件夹的指针。如果继续去看baz.txt的ID所指向的内容(git cat-file -p 4448adbf7ecd394f42ae135bbeed9676e894af8),可以看到这样的内容:
在这里插入图片描述

references

现在,所有的commit都可以用一串哈希之后的ID来标识,但是对人而言,很难记住40位的16进制字符串。
对此,Git提出reference,将这串ID再映射为人类可读的名字。reference是可变的,通常会指向最新的提交
在这里插入图片描述
这样就可以用人类可读的名字标识一串哈希ID了,而标识“我们当前在哪”的是HEAD

repositories

这个repository指的就是Git的objects和references,Git所有在磁盘上的存储就是objects和references,git命令就是操纵commits的DAG,实现增加objects或者修改/增加references。在进行命令操作的时候,可以想一想会对这个DAG产生怎样的操作;同理,想要实现对DAG的某种操作,往往也有对应的git命令。

Staging area

通过staging area可以明确哪些改变需要提交成为shapshot哪些不用

Git命令行接口

更详细的部分可以看Pro git

基础

  • git help <command>: get help for a git command

  • git init: creates a new git repo, with data stored in the .git directory

  • git status: tells you what’s going on

  • git add <filename>: adds files to staging area

  • git commit: creates a new commit. Write good commit messages!

  • git log: shows a flattened log of history

  • git log --all --graph --decorate: visualizes history as a DAG

  • git diff <filename>: show changes you made relative to the staging area

  • git diff <revision> <filename>: shows differences in a file between snapshots

  • git checkout <revision>: updates HEAD and current branch

分支和合并

  • git branch: shows branches
  • git branch <name>: creates a branch
  • git checkout -b <name>: creates a branch and switches to it
    same as git branch <name>; git checkout <name>
  • git merge <revision>: merges into current branch
  • git mergetool: use a fancy tool to help resolve merge conflicts
  • git rebase: rebase set of patches onto a new base

远程

  • git remote: list remotes
  • git remote add <name> <url>: add a remote
  • git push <remote> <local branch>:<remote branch>: send objects to remote, and update remote reference
  • git branch --set-upstream-to=<remote>/<remote branch>: set up correspondence between local and remote branch
  • git fetch: retrieve objects/references from a remote
  • git pull: same as git fetch; git merge
  • git clone: download repository from remote

undo

  • git commit --amend: edit a commit’s contents/message
  • git reset HEAD <file>: unstage a file
  • git checkout -- <file>: discard changes

高级git

  • git config: Git is highly customizable
  • git clone --depth=1: shallow clone, without entire version history
  • git add -p: interactive staging
  • git rebase -i: interactive rebasing
  • git blame: show who last edited which line
  • git stash: temporarily remove modifications to working directory
  • git bisect: binary search history (e.g. for regressions)
    .- gitignore: specify intentionally untracked files to ignore

其他

练习

1、如果之前完全没用过Git的话,先看一下Pro git的前几章或者Learn git brnching
2、clone project,可视化看看历史,谁是最后修改README.md的(提示,用git log加上argument)?最后修改collections中_config.yml这行的提交的message是什么?(提示,用git blame和git show)

git clone https://github.com/missing-semester/missing-semester.git
git log --all --graph --decorate
git log README.md
git blame _config.yml # 查看collections那一行是谁改的
git show a88b4eac # 对照差出来的ID看Message

3、Git中常常出现的一个错误是不小心把特别大或者敏感文件。试着添加一个文件到repositority,做一些提交,然后把这个文件从历史里删掉

wget bfg的地址
 java -jar bfg-1.14.0.jar -D sensifile.py

4、对一个项目进行修改,然后进行git stash,运行git log --all --oneline,运行git stash pop来撤销
在这里插入图片描述
5、在~/.gitconfig中加上别名,运行git graph得到git log --all --graph --decorate --oneline的结果。
在这里插入图片描述
6、可以运行git config --global core.excludesfile ~/.gitignore_global后在~/.gitignore_global中定义忽略的文件,设定忽略一些系统文件
创建文件后加上*.DS_Store
7、提交一个pull request

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值