Git和SVN都是版本管理系统,可是他们
命令区别后面会简单进行一个对比,咱们先从原理的角度分析html
先来复习哈命令git
做用 | git | svn |
---|---|---|
版本库初始化 | git init | svn create |
clone | git clone | svn co(checkout) |
add | git add (.除去.gitignore,*全部的文件) | svn add |
commit | git commit | svn commit |
pull | git pull | svn update |
push | git push | - |
查看工做状态 | git status | svn status |
建立分支 | git branch <分支名> | svn cp <分支名> |
删除分支 | git branch -d <分支名> | svn rm <分支名> |
分支合并 | git merge <分支名> | svn merge <分支名> |
工做区差别 | git differ (-cached / head) | svn diff |
更新至历史版本 | git checkout <commit> | svn update -r <rev> |
切换tag | git checkout <tag> | svn switch <tag> |
切换分支 | git checkout branch | svn switch branch |
还原文件 | git checkout - path | svn revert path |
删除文件 | git rm path | svn rm path |
移动文件 | git mv path | git mv path |
清除未追踪文件 | git clean | svn status sed -e |
你们想一想为何咱们代码管理为何通常用git,原型图和高保真管理通常用SVN? 算法
1.git是分布式的,有本地和远程两个版本库,SVN是集中式,只有一个远程版本库;
2.git的内容是按元数据方式存贮,全部控制文件在.git中,svn是按文件处理,全部资源控制文件在.svn中;
3.svn的分支是一个目录,git不是;
4.git没有一个全局的版本号,svn有;
5.git内容存贮是使用SHA-1哈希算法,能确保代码完整性;
6.git 有工做区,暂存区,远程仓库,git add将代码提交到暂存区, commit提交到本地版本库,push推送到远程版本库。svn是add 提交到暂存,commit是提交到远程版本库。json
因此能够很清楚的看出由于原型图和高保真都是以单个文件为单位,因此适合用SVN管理,而咱们代码时以行数为单位,适合Git浏览器
1..svn目录 app
随便打开一个.svn的目录能够看到结构:
若是没法查看.svn,window电脑-点击查看-勾选隐藏文件;
mac直接shift + command + .编辑器
├── pristine 各个版本纪录,这个文件通常较大 ├── tmp ├── entries 当前版本号 ├── format 文本文件, 放了一个整数,当前版本号 ├── wc.db 二进制文件 ├── wc.db-journal 二进制文件
2..git 目录结构 分布式
你可能对这些目录结构很陌生,不要紧,直接在终端输入 git help gitrepository-layout回车,你会发现浏览器会打开一个html文件,实际上就会打开安装git下面的一个html文档svn
├── hooks 钩子文件 │ ├── applypatch-msg.sample │ ├── commit-msg.sample │ ├── fsmonitor-watchman.sample │ ├── fsmonitor-watchman.sample │ ├── pre-applypatch.sample │ ├── pre-commit.sample commit时会触发这个钩子 │ ├── pre-push.sample push触发 │ ├── pre-rebase.sample │ ├── pre-receive.sample │ ├── prepare-commit-msg.sample │ ├── update.sample update触发 ├── info │ ├── exclude 忽略的文件 ├── object git数据对象,包括commit,trees,二进制对象,标签等 ├── COMMIT_EDITMSG 上一次提交的注释信息 ├── log 各个refs的历史信息 ├── refs 每一个分支指向那个提交 ├── config 本项目配置信息,包括仓库地址,分支,用户帐号等 ├── description 项目描述 ├── HEAD 当前分支的最后一次提交 ├── index 索引文件,存贮git add把要添加的项 ├── packed-refs 分支标识文件
因此能够看出git在处理代码方面功能比svn要强大一些post
1.执行git init会生成一个初始化的.git,会发现上面有些目录文件没有,由于有些文件是指定的命令后才会生成
2.新建一个test.txt,随便写点内容,执行git status
On branch master // 默认一个master 分支 No commits yet Untracked files: // 未提交的文件 (use "git add <file>..." to include in what will be committed) test.txt nothing added to commit but untracked files present (use "git add" to track)
运行 find . -type f
./config ./HEAD ./info/exclude ./description ./hooks/commit-msg.sample ./hooks/pre-rebase.sample ./hooks/pre-commit.sample ./hooks/applypatch-msg.sample ./hooks/fsmonitor-watchman.sample ./hooks/pre-receive.sample ./hooks/prepare-commit-msg.sample ./hooks/post-update.sample ./hooks/pre-applypatch.sample ./hooks/pre-push.sample ./hooks/update.sample ./index
3.执行 git add text.txt,显示
On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: test.txt
运行find . -type f
./config ./objects/61/de0edff4ebeeff225da34006cbe6427638fadc # 比以前多了一个文件 ./HEAD ./info/exclude ./description ./hooks/commit-msg.sample ./hooks/pre-rebase.sample ./hooks/pre-commit.sample ./hooks/applypatch-msg.sample ./hooks/fsmonitor-watchman.sample ./hooks/pre-receive.sample ./hooks/prepare-commit-msg.sample ./hooks/post-update.sample ./hooks/pre-applypatch.sample ./hooks/pre-push.sample ./hooks/update.sample ./index
4.总结:能够看出git add后test.txt 被标记为staged 状态,并且object多了一个61/de0edff 文件,因此object 能够存贮git仓库内容,以二进制方式存贮。
5.咱们能够查看下文件来源
git cat-file -p 61de0edf 打印 test
6.git如何管理和归档文件
咱们常见的文件系统(NTFS、FAT、FAT32)是基于地址方式检索文件,即先给具体的地址,而后从地址编号对应的存储单元读取文件内容,而git是基于内容检索,是对整个内容检索,获得一个真实的存储位置,相似哈希映射。
1.执行 git commit -m 'add test'
1 file changed, 1 insertion(+) create mode 100644 test.txt
2.运行 find . -type f
./test.txt ./.git/config ./.git/objects/61/de0edff4ebeeff225da34006cbe6427638fadc ./.git/objects/ed/fd7e903f8f622f9a52542adfa077552608202d ./.git/objects/26/ef8e81bc27b4a67f251145a4f83782364fa9fa ./.git/HEAD ./.git/info/exclude ./.git/logs/HEAD ./.git/logs/refs/heads/master ./.git/description ./.git/hooks/commit-msg.sample ./.git/hooks/pre-rebase.sample ./.git/hooks/pre-commit.sample ./.git/hooks/applypatch-msg.sample ./.git/hooks/fsmonitor-watchman.sample ./.git/hooks/pre-receive.sample ./.git/hooks/prepare-commit-msg.sample ./.git/hooks/post-update.sample ./.git/hooks/pre-applypatch.sample ./.git/hooks/pre-push.sample ./.git/hooks/update.sample ./.git/refs/heads/master ./.git/index ./.git/COMMIT_EDITMSG
能够看出commit 后在add 的基础上object多了两个文件ed/fd7e90和26/ef8e8,从文件的归档路径和命名能够看出git使用SHA-1算法对文件内容进行了校验
还多了一个COMMIT_EDITMSG ,里面是上一次提交的注释信息
3.使用git cat-file 查看来源
git cat-file -t edfd7e90 // 终端输出tree git cat-file -t 26ef8e8 // 终端输出commit git cat-file -p edfd7e90 // 终端输出 100644 blob 61de0edff4ebeeff225da34006cbe6427638fadc test.txt git cat-file -p 26ef8e8 // 终端输出 tree edfd7e903f8f622f9a52542adfa077552608202d author 信息 1612668900 +0800 committer author 信息 1612668900 +0800
ed/fd7e90 是一个commit 对象,tree属性指向了26/ef8e8,纪录了文件操做,做者,提交者信息;
26/ef8e8 是一个tree 对象,blob 属性指向了blob对象61/de0edf,纪录了文件名;
61/de0edf 是一个blob 对象,纪录了文件内容。
三个文件关系:
因此如今知道为何object文件会很大的吧
git branch 获取分支列表
列表保存到refs/heads/master 下面
经过上面3.2的分析知道,在git系统中有四种尅性的对象:
1.commit:指向一个tree,纪录了文件操做,做者,提交者信息;
2.tree:对象关系树,管理tree和blob的关系;
3.blob:保存文件内容;
4.tag:标记提交。
1.钩子初始化:
上面说到的hooks 下面都是生命周期脚本,初始化仓库(git init)或 git clone 都会初始化.git文件;
2.钩子是本地的,由于不会提交到代码仓库,只不过clone的时候会初始化;
3.钩子分类:
钩子名 | 做用 |
---|---|
pre-commit | 每次git commit以前会触发,很常见的应用就是在package.json结合husky和lint-staged作代码eslint校验 |
prepare-commit-msg | 在pre-commit在文本编辑器生成提交信息被调用,方便的修改自动生成的squash和merage提交 |
commit-msg | 用户输入提交信息被调用,就是commit -m 后面那个提交信息,能够用来规范提交信息 |
post-commit | commit-msg后执行,通知git commit的结果 |
post-checkout | git checkout被调用 |
pre-rebase | git rebase 更改以前运行 |
pre-receive | git push后执行,存在于远程仓库中,服务端远程钩子 |
update | pre-receive 后调用 |
post-receive | push 推送成功后被调用,通知push的用户 |
看到这里git和svn不少迷惑都解开了吧, 原创码字不易,欢迎star!