一篇搞懂Git 和 SVN 的區別【原理篇】

前言

Git和SVN都是版本管理系統,可是他們
命令區別後面會簡單進行一個對比,咱們先從原理的角度分析html

4.git和svn命令

先來複習哈命令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

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是提交到遠程版本庫。json

image

因此能夠很清楚的看出由於原型圖和高保真都是以單個文件爲單位,因此適合用SVN管理,而咱們代碼時以行數爲單位,適合Git瀏覽器

2.文件.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

3..git文件動態分析

3.1 add階段

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是基於內容檢索,是對整個內容檢索,獲得一個真實的存儲位置,相似哈希映射。

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
// 終端輸出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 對象,紀錄了文件內容。

三個文件關係:
image
因此如今知道爲何object文件會很大的吧

3.3 branch

git 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的用戶

結語

看到這裏git和svn不少迷惑都解開了吧, 原創碼字不易,歡迎star!

相關文章
相關標籤/搜索