git和svn命令
先来复习哈命令
浸染
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
1.存贮差异
大家想想为什么我们代码管理为什么一样平常用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是提交到远程版本库。
以是可以很清楚的看出由于原型图和高保真都因此单个文件为单位,以是适宜用SVN管理,而我们代码时以行数为单位,适宜Git
2.文件.svn和.git差异
1..svn目录
随便打开一个.svn的目录可以看到构造:如果无法查看.svn,window电脑-点击查看-勾选隐蔽文件;mac直接shift + command + .
├── pristine 各个版本记录,这个文件一样平常较大├── tmp ├── entries 当前版本号├── format 文本文件, 放了一个整数,当前版本号├── wc.db 二进制文件├── wc.db-journal 二进制文件
2..git 目录构造
你可能对这些目录构造很陌生,没紧要,直接在终端输入 git help gitrepository-layout回车,你会创造浏览器会打开一个html文件,实际上就会打开安装git下面的一个html文档
├── 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要强大一些
3..git文件动态剖析3.1 add阶段
1.实行git init会天生一个初始化的.git,会创造上面有些目录文件没有,由于有些文件是指定的命令后才会天生
2.新建一个test.txt,随便写点内容,实行git status
On branch master // 默认一个master 分支No commits yetUntracked files: // 未提交的文件 (use "git add <file>..." to include in what will be committed) test.txtnothing 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 masterNo commits yetChanges 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是基于内容检索,是对全体内容检索,得到一个真实的存储位置,类似哈希映射。
3.2 commit阶段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// 终端输出treegit cat-file -t 26ef8e8// 终端输出commitgit cat-file -p edfd7e90// 终端输出 100644 blob 61de0edff4ebeeff225da34006cbe6427638fadc test.txtgit cat-file -p 26ef8e8// 终端输出 tree edfd7e903f8f622f9a52542adfa077552608202dauthor 信息 1612668900 +0800committer author 信息 1612668900 +0800
ed/fd7e90 是一个commit 工具,tree属性指向了26/ef8e8,记录了文件操作,作者,提交者信息;26/ef8e8 是一个tree 工具,blob 属性指向了blob工具61/de0edf,记录了文件名;61/de0edf 是一个blob 工具,记录了文件内容。
三个文件关系:
以是现在知道为什么object文件会很大的吧
3.3 branchgit branch 获取分支列表列表保存到refs/heads/master 下面
3.4 git工具模型通过上面3.2的剖析知道,在git系统中有四种尅性的工具:1.commit:指向一个tree,记录了文件操作,作者,提交者信息;2.tree:工具关系树,管理tree和blob的关系;3.blob:保存文件内容;4.tag:标记提交。
3.5 git生命周期钩子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的用户
原文来自:https://segmentfault.com/a/1190000039978493