iT邦幫忙

2025 iThome 鐵人賽

DAY 13
0
自我挑戰組

Git 與 GitHub 入門日誌系列 第 13

Day 13:下載專案之 clone 與 pull 的差別

  • 分享至 

  • xImage
  •  

今日的學習目標有:

  1. 了解 git clone 與 git pull 的用途
  2. 學會從 GitHub 下載專案到本地端(昨天是本地端上傳到GitHub!)
  3. 學會更新遠端最新的修改

1. git clone:第一次下載專案

git clone 是用來把整個遠端 Repo 複製到本地端。
它會自動幫你:

  • 建立一個資料夾
  • 複製所有檔案
  • 帶下完整的提交歷史
  • 自動設定好 remote(通常叫 origin)

我們先來實作看看!
先輸入下列指令:

git clone https://github.com/你的帳號/git-demo.git

執行後,你會得到一個 git-demo 資料夾,裡面就是 GitHub 上的檔案。
https://ithelp.ithome.com.tw/upload/images/20250924/20169195ZPmQRkIKVX.png

2. git pull:更新專案

當你已經 clone 過一次,
以後只要想更新遠端的最新變更,就用 git pull。

它的功能其實等於:

git fetch   # 抓下遠端的最新版本
git merge   # 把遠端的版本合併到本地

一樣,我們來實作:

cd git-demo
git pull origin master

https://ithelp.ithome.com.tw/upload/images/20250924/201691955bZd7wyUZn.png
這樣如果遠端有人新增檔案、修改內容,你的本地端就會同步更新。

3. 差異比較

  • git clone
    通常用在第一次下載專案
    建立新資料夾,把整個 Repo 複製下來

  • git pull
    通常為專案已存在時
    來更新 Repo,讓本地端與遠端保持一致

可以把 clone 想成「下載專案的第一次安裝包」,
而 pull 是「之後的更新檔」。


了解 clone 和 pull的用法後,
今日來個小練習時操一下!!

  1. 首先要找一個公開的 GitHub 專案,
    這裡就用這個repo:https://github.com/octocat/Hello-World.git

  2. 接下來用 git clone 把它下載到你的電腦!!
    https://ithelp.ithome.com.tw/upload/images/20250924/20169195VuG24nxCHl.png

https://ithelp.ithome.com.tw/upload/images/20250924/2016919571O03f1QDd.png

下載完成時,我們可以在GitHub頁面點選右上角頭像,
profiles > repositories,
檢查是否有 Hello-World 的本地分支!
有的話就點進去!!!
https://ithelp.ithome.com.tw/upload/images/20250924/20169195nQgrpTSkzw.png
https://ithelp.ithome.com.tw/upload/images/20250924/20169195SXchQJ4VB1.png

  1. 接著我們嘗試修改 README,然後用 git pull 更新
    https://ithelp.ithome.com.tw/upload/images/20250924/20169195dvkQtChCmb.png

這裡我就加了一句「Hello from Sharon!」,
記得按commit changes,
然後一定要選Commit directly to the main branch
不然你在你的電腦就看不到更改了~

  1. 比對前後的差異
    https://ithelp.ithome.com.tw/upload/images/20250924/201691955RRBfO32NS.png
    我們要把它下再到本地端,
    成功後才能查看(cat或type),
    途中就能看到我新增的那行了。

git clone 只需要執行一次就可以囉,
之後每次更新遠端內容,用 git pull 就好

如果之後 pull 的時候出現衝突,就要手動解決(可至第九天文章查看)。


上一篇
Day 12:將本地專案推上 GitHub
下一篇
Day 14:GitHub 介面導覽(Issues、Wiki、Insights)
系列文
Git 與 GitHub 入門日誌17
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言