Git是由C語言開發的一套內容尋址文件系統,並在此之上提供了一個VCS用戶界面。node
使用git init
命令初始化當前目錄,生成**.git**文件夾。git
git init
執行後並無產生.git/index,而是在首次執行git add
命令後才生成,並把由更新文件生成的blob對象放入**.git/objects/**內。)
.git/包含了如下目錄和文件:github
Git包含底層命令(Plumbing)和高層命令(Procelain)。shell
Git定義了4種對象:blob、tree、commit和tag,它們都位於**.git/objects/**目錄下。git對象在原文件的基礎上增長了一個頭部,即對象內容 = 對象頭 + 文件內容
。這種格式沒法直接經過cat
命令讀取,須要使用git cat-file
這個底層命令才能正確讀取。 對象頭的格式爲:對象頭 = 對象類型 + 空格 + 數據內容長度 + null byte
,例如一個文件內容爲「hello world」,其blob對象頭爲"blob 11\000"。bash
git hash-object
命令,對文件內容增長頭信息後計算hash值並返回,增長-w
參數後在git倉庫內建立blob對象(blob對象 = 對象頭 + 文件內容)。62/0d4582bfbf773ef15f9b52ac434906a3cdf9c3
,那麼它在git倉庫中的路徑爲.git/objects/62/0d4582bfbf773ef15f9b52ac434906a3cdf9c3
。ref: refs/heads/master
。
引用規格指的是遠程倉庫分支和本地分支的映射,可表示爲<src>:<dst>
,這也暗示了數據流向爲src → dst
。ide
# 兩命令都包含引用規格(refspec)來指定數據流向。
git fetch [remote repository] [remote branch]:[local branch]
git push [remote repository] [local branch]:[remote branch]
複製代碼
當使用缺省的fetch/push命令時,Git會根據.git/config中的refspec配置進行操做。工具
git remote add
命令添加一個遠程分支的同時,會在.git/config文件中添加一個配置結點。
git fetch orgin
這個缺省命令時,會拉取origin遠程倉庫的全部分支。git log origin/master
來查看從遠程倉庫fetch的master分支。# 如下三個命令是等價的,Git會把他們都擴展爲refs/remote/origin/master
git log origin/master
git log remote/origin/master
git log refs/remote/origin/master
複製代碼
1)可經過改寫fetch行爲fetch = +refs/heads/master:refs/remotes/origin/mymaster
,指定把遠程的master分支映射爲本地的origin/mymaster分支。 2)也可指定多個映射,一次拉取多個指定分支。fetch
[remote "origin"]
url = git@github.com:kivihub/test.git
fetch = +refs/heads/master:refs/remotes/origin/master
fetch = +refs/heads/experiment:refs/remotes/origin/experiment
fetch = +refs/heads/qa/*:refs/remote/orgin/qa/*
複製代碼
3)可同時指定push的refspec
若要把本地的master分支push到遠程的qa/master分支,可配置以下:ui
[remote "origin"]
url = git@github.com:kivihub/test.git
fetch = +refs/heads/master:refs/remotes/origin/master
fetch = +refs/heads/experiment:refs/remotes/origin/experiment
fetch = +refs/heads/qa/*:refs/remote/orgin/qa/*
push = refs/heads/master:refs/heads/qa/master
複製代碼
4)刪除遠程分支
經過命令git push origin :master
能夠刪除遠程origin庫的master分支。由於refspec的格式爲:,經過把置空表示把遠程分支變爲空,也就是刪除它。url
git gc
垃圾回收命令用於壓縮或刪除數據,節省磁盤空間。