Git本身的原理記錄檔案快照,也是就是完整的備份,而不是差異備份。
所有的commit都可以獨立看待,這樣的優點是有相當高的自由度,
Git每一次的版本(git),都是完整快照 |
一般的版控是差異備份 EX: SVN |
所有的commit都可以獨立看待,這樣的優點是有相當高的自由度,
你可以任意組合你的commit,建立你的history線圖,而且大部份的操作都可以離線作業。
缺點是會佔用相當大的磁碟空間,比如說:你簽入了一個大的檔案(圖檔之類的),或是你常常reset 你的commit ,因而產生這unreachable commit 。
Git 本身提供了GC(Garbage collect)的機制,不定時的會執行git gc,將過期且不被使用的物件清除,但是我們如果想手動清除掉這些不用的物件,以釋放空間,可以手動執行以下命令。
Git 本身提供了GC(Garbage collect)的機制,不定時的會執行git gc,將過期且不被使用的物件清除,但是我們如果想手動清除掉這些不用的物件,以釋放空間,可以手動執行以下命令。
git reflog expire --expire-unreachable=now --all
git gc --prune=now
實作記錄如下:
- 建立一個資料夾「GitGC」
- 開啟CMD切換到「GitGC」
admin@pc MINGW32 /$ cd /d/Repo/SandBox/GitGC/ - 建立Git Repo
dmin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git init - 檢查一下目前git repo的大小
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git count-objects -v
count: 0
size: 0
in-pack: 0
packs: 0
size-pack: 0
prune-packable: 0
garbage: 0
size-garbage: 0 - 放入一個大的圖檔,加入track
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
80-20121007_stitch.jpg
nothing added to commit but untracked files present (use "git add" to track)
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git add .
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: 80-20121007_stitch.jpg - 簽入local 的Repo
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git commit
[master (root-commit) ed987f4] add image:
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 80-20121007_stitch.jpg
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git status
On branch master
nothing to commit, working directory clean - 檢查 Repo 目前的大小
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git count-objects -v
count: 3
size: 37467
in-pack: 0
packs: 0
size-pack: 0
prune-packable: 0
garbage: 0
size-garbage: 0 - 隨便改一改圖,再簽入(重複這個動作2~3次),讓我們有多個commit,這時候節點應該會類似於下圖
- 檢查一下 Git Repo的大小,可以看到Git Repo Size的膨漲
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git count-objects -v
count: 10
size: 73770
in-pack: 0
packs: 0
size-pack: 0
prune-packable: 0
garbage: 0
size-garbage: 0 - 接下來我們Reset 回第一個commit
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git reflog
3260eda HEAD@{0}: commit: modify image2
d29b377 HEAD@{1}: commit: modify image
91af9eb HEAD@{2}: commit: modify image
ed987f4 HEAD@{3}: commit (initial): add image:
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git reset ed987f4
Unstaged changes after reset:
M 80-20121007_stitch.jpg - 再檢查一下Size 並沒有因此縮小
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git count-objects -v
count: 10
size: 73770
in-pack: 0
packs: 0
size-pack: 0
prune-packable: 0
garbage: 0
size-garbage: 0 - 強制執行git gc ,但是git repo並不會縮小,這是因為git gc只會回收過期的commit (預設是90天)
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git gc
Counting objects: 10, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), done.
Total 10 (delta 1), reused 0 (delta 0)
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git count-objects -v
count: 0
size: 0
in-pack: 10
packs: 1
size-pack: 73802
prune-packable: 0
garbage: 0
size-garbage: 0 - 所以,我們必須讓這些不屬於任何branch的commit,強制過期,執行以下語法
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git reflog expire --expire-unreachable=now --all - 再執行一次GC
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git gc --prune=now
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), done.
Total 3 (delta 0), reused 3 (delta 0) - 檢查大小,可以看到Git Repo已經縮小
admin@pc MINGW32 /d/Repo/SandBox/GitGC (master)
$ git count-objects -v
count: 0
size: 0
in-pack: 3
packs: 1
size-pack: 37471
prune-packable: 0
garbage: 0
size-garbage: 0
沒有留言:
張貼留言