Git工作原理

1. Git操作名詞介紹

  • 倉庫(Repository):存儲(chǔ)代碼及其版本歷史的容器,分為本地倉庫和遠(yuǎn)程倉庫。
  • 工作區(qū)(Working Directory):本地電腦上的代碼目錄,包含未提交的修改。
  • 暫存區(qū)(Staging Area):存儲(chǔ)待提交的代碼改動(dòng),也稱為索引(Index)。
  • 提交(Commit):將暫存區(qū)改動(dòng)保存到本地倉庫,形成版本快照。
  • 合并(Merge):將分支代碼合并到當(dāng)前分支
  • 推送(Push):將本地倉庫代碼上傳到遠(yuǎn)程倉庫。
  • 拉?。≒ull):從遠(yuǎn)程倉庫獲取最新代碼并合并到本地倉庫。
  • 分支(Branch):用于隔離不同開發(fā)任務(wù),可獨(dú)立維護(hù)代碼版本。
  • HEAD:指向當(dāng)前操作的分支
git操作說明

2. Git常見工作流程詳解

git工作流程
git工作流程
查看賬號(hào)信息
yuzhenteng@yuzhentengdeMac-mini ~ % git config user.name
lixuyan
yuzhenteng@yuzhentengdeMac-mini ~ % git config user.email
123456789@qq.com
git 初始化
yuzhenteng@yuzhentengdeMac-mini git_day1 % git init
Initialized empty Git repository in /Users/yuzhenteng/Desktop/demo/local_repo/git_day1/.git/
查看當(dāng)前狀態(tài)
yuzhenteng@yuzhentengdeMac-mini git_day1 % git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    file.txt

nothing added to commit but untracked files present (use "git add" to track)
將工作目錄修改添加到暫存區(qū)
yuzhenteng@yuzhentengdeMac-mini git_day1 % git add .
將暫存區(qū)內(nèi)容提交到本地倉庫
yuzhenteng@yuzhentengdeMac-mini git_day1 % git commit -m"add file to local repo"
[main (root-commit) 429cfd4] add file to local repo
 1 file changed, 5 insertions(+)
 create mode 100644 file.txt
查看提交日志
yuzhenteng@yuzhentengdeMac-mini git_day1 % git log --oneline --decorate --graph --stat
* 429cfd4 (HEAD -> main) add file to local repo
   file.txt | 5 +++++
   1 file changed, 5 insertions(+)

3. git reset 與git checkout命令講解

git reset

  • HEAD: 用來指向分支的最后一次提交對象。
  • git reset :用于回退版本,可以指定退回某一次提交的版本。
    --soft:回退到暫存區(qū)。
    --hard: 回退到工作目錄。

我們在file.txt中添加-----6----- 并提交到本地倉庫 這段操作我就不展示了。接下來我將這次提交回退到工作區(qū)

yuzhenteng@yuzhentengdeMac-mini git_day1 % git reset --hard HEAD~1
HEAD is now at 429cfd4 add file to local repo
或
yuzhenteng@yuzhentengdeMac-mini git_day1 % git reset --hard 429cfd4
HEAD is now at 429cfd4 add file to local repo
  • HEAD~1:指HEAD指向當(dāng)前操作分支的最后一次提交對象的前一個(gè)提交對象?;蛘咧苯佑们耙粋€(gè)commitId代替。

下面我們用--soft將提交對象回退到暫存區(qū)。

yuzhenteng@yuzhentengdeMac-mini git_day1 % git reset --soft HEAD~1

此時(shí)工作區(qū)內(nèi)容沒有發(fā)生變化。我想回退到工作區(qū)內(nèi)容。接下來就用到了checkout。

checkout

  • git checkout :
    A.切換分支,例如:git checkout master
    B.重新存儲(chǔ)工作區(qū)文件 git checkout -- .
yuzhenteng@yuzhentengdeMac-mini git_day1 % git reset HEAD .
Unstaged changes after reset:
M   file.txt
yuzhenteng@yuzhentengdeMac-mini git_day1 % git checkout -- .
yuzhenteng@yuzhentengdeMac-mini git_day1 % git status
On branch main
nothing to commit, working tree clean
  • git reset HEAD . 將暫存區(qū)內(nèi)容移除, .:點(diǎn)代表當(dāng)前目錄。

4. Git key-value文件系統(tǒng)

git key-value文件系統(tǒng)
  • git cat-file -p commit-hash 用于查看提交信息
  • Git對象存儲(chǔ)
    Git將存儲(chǔ)對象的40位HASH分為兩部分:
    A. 頭兩位作文件夾
    B.后38位 為對象文件名 git/objects/hash[:2]/hash[2:40]
  • 為什么要這么設(shè)計(jì)目錄結(jié)構(gòu),而不直接使用49位hash作文件名?
    1.部分文件系統(tǒng)對目錄下的文件數(shù)量有限制。例如,F(xiàn)AT32限制單目錄下的最大文件數(shù)量是65535個(gè)
    2.部分文件系統(tǒng)查找文件屬于線性查找,目錄下的文件越多,訪問越慢。
  • Git index文件
    Git在?git 文件夾下面存放了 index 文件,該文件表示Git stage 的內(nèi)容。該文件是二進(jìn)制文件,保存了被stage的文件的所有信息,像inode信息、hash值等等
  • git ls-files -s 查看暫存區(qū)文件
  • 為文件生成hash值且保存文件 git hash-object -w 文件路徑
yuzhenteng@yuzhentengdeMac-mini git_day1 % git hash-object -w file.txt 
6fc3c2d99d427db79cd35a409043889f4dcaff5c
  • 保存到索引/暫存區(qū) git update-index --add --cacheinfo 100644 hash 文件路徑
yuzhenteng@yuzhentengdeMac-mini git_day1 % git update-index --add --cacheinfo 100644 6fc3c2d99d427db79cd35a409043889f4dcaff5c file.txt
yuzhenteng@yuzhentengdeMac-mini git_day1 % git ls-files -s
100644 6fc3c2d99d427db79cd35a409043889f4dcaff5c 0   file.txt

5. 手動(dòng)構(gòu)建git commit tree

  • 寫樹 git write-tree
yuzhenteng@yuzhentengdeMac-mini git_day1 % git write-tree
639282694c1a66e7936f273449bcfa25ff2ffc29
yuzhenteng@yuzhentengdeMac-mini git_day1 % git cat-file -p 639282694c1a66e7936f273449bcfa25ff2ffc29
100644 blob 6fc3c2d99d427db79cd35a409043889f4dcaff5c    file.txt
  • 在暫存區(qū)創(chuàng)建新樹 git read-tree --prefix=文件夾/ hash
yuzhenteng@yuzhentengdeMac-mini git_day1 % git read-tree --prefix=hank/ 639282694c1a66e7936f273449bcfa25ff2ffc29
yuzhenteng@yuzhentengdeMac-mini git_day1 % git write-tree
0191adbc95c577c0b77c31bfc47e714306b60645
yuzhenteng@yuzhentengdeMac-mini git_day1 % git ls-files -s
100644 6fc3c2d99d427db79cd35a409043889f4dcaff5c 0   file.txt
100644 6fc3c2d99d427db79cd35a409043889f4dcaff5c 0   hank/file.txt
yuzhenteng@yuzhentengdeMac-mini git_day1 % git cat-file -p 0191adbc95c577c0b77c31bfc47e714306b60645
100644 blob 6fc3c2d99d427db79cd35a409043889f4dcaff5c    file.txt
040000 tree 639282694c1a66e7936f273449bcfa25ff2ffc29    hank
yuzhenteng@yuzhentengdeMac-mini git_day1 % echo "add 10" | git commit-tree bdd8135704656336dacde01b5bcdba81fedc3752
5200a9049b527ee704b99ee7030ac12203ae3d63
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容