Skip to content

Commit e47679a

Browse files
committed
add git clone --depth tutorial
1 parent 21c0f47 commit e47679a

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,103 @@ git clone git@github.com:twtrubiks/test.git
6464
如圖 ( 下載成功 ),在你的下載路徑下就會多出一個資料夾
6565
![alt tag](https://i.imgur.com/iIkTlqf.jpg)
6666

67+
### 如何改善(加速)大型 repo git clone 速度
68+
69+
* [Youtube Tutorial - 如何改善(加速)大型 repo git clone 速度](https://youtu.be/YHX0qkQa1UI)
70+
71+
有時候我們會需要 clone 很大的 repo,執行 `git clone` 都需要很長的時間,是不是有方法可以
72+
73+
加速 clone 的速度呢 :question:
74+
75+
直接開始動手嘗試 ( 使用 [django](https://github.com/django/django) 當範例 ),
76+
77+
`git clone git@github.com:django/django.git`
78+
79+
( 你會發現 clone 需要一些時間 :triumph:)
80+
81+
![alt tag](https://i.imgur.com/yMH6L8F.png)
82+
83+
接著查看 log,`git log`
84+
85+
![alt tag](https://i.imgur.com/vJkFTr2.png)
86+
87+
嘗試切換 branch `git checkout stable/2.2.x`
88+
89+
![alt tag](https://i.imgur.com/UtxJ2ER.png)
90+
91+
開始改善(加速) clone 的時間,
92+
93+
可以透過 `--depth` 這個參數來完成,簡單說明一下他的功能,當我們一般執行 clone 之後,
94+
95+
接著執行 `git log` 你會發現有大量的 log,在某修情況下,你可能不需要那麼多的 log,
96+
97+
也就是說你可能只需要最近 10 筆的 history commit,甚至你只需要 1 筆 ( 也就是根本不需要
98+
99+
history commit ),這時候就很適合使用 `--depth`
100+
101+
`git clone git@github.com:django/django.git --depth 1`
102+
103+
( 你會發現這次快很多了 )
104+
105+
![alt tag](https://i.imgur.com/yvkZUZI.png)
106+
107+
接著查看 log,`git log`
108+
109+
( 會變快的原因是因為我們只保留最新的一筆 history commit ,
110+
111+
如果你需要最近 10 筆,改成 --depth 10 即可 )
112+
113+
![alt tag](https://i.imgur.com/at9Zzq3.png)
114+
115+
但是會有一個問題,當嘗試切換 branch `git checkout stable/2.2.x`
116+
117+
( 你會發現你無法切換 remote branch :scream:
118+
119+
原因是因為使用 `--depth` 相當於是 `--single-branch`
120+
121+
所以當然沒有其他的 branch。 )
122+
123+
![alt tag](https://i.imgur.com/gDaeq1W.png)
124+
125+
也就是說以下兩條指令其實是相等的
126+
127+
```cmd
128+
git clone git@github.com:django/django.git --depth 1
129+
git clone git@github.com:django/django.git --depth 1 --single-branch
130+
```
131+
132+
為了解決這個問題,比較好的做好應該是這樣
133+
134+
```cmd
135+
git clone git@github.com:django/django.git --depth 1 --no-single-branch
136+
```
137+
138+
( 這個和 `--single-branch` 比會稍微久一點點,因為每個 branch 的最新一個 history commit 都要 clone 下來 )
139+
140+
這樣的話,就可以保留 remote 的 branch 了,
141+
142+
![alt tag](https://i.imgur.com/BkLKVZz.png)
143+
144+
成功切換 remote 的 branch, `git checkout stable/2.2.x`
145+
146+
![alt tag](https://i.imgur.com/VCvcSTr.png)
147+
148+
最後稍微整理,
149+
150+
如要 clone 最近一次的 history,而且也需要其他 branch,使用如下,
151+
152+
`git clone git@github.com:django/django.git --depth 1 --no-single-branch`
153+
154+
如要 clone 最近一次的 history,而且**不需要**其他 branch,使用如下,
155+
156+
`git clone git@github.com:django/django.git --depth 1 --single-branch`
157+
158+
or
159+
160+
`git clone git@github.com:django/django.git --depth 1`
161+
162+
更多詳細參數說明請參考 [git clone](https://git-scm.com/docs/git-clone)
163+
67164
## git status 指令
68165

69166
```cmd

0 commit comments

Comments
 (0)