課程目標

  • 學會 git pull

課程內容

學會把專案上傳 github 了,這次來試試把專案從 github 載下來吧

假設你現在使用另一台新電腦,專案還不在你的電腦上

打開專案頁面

https://github.com/howtomakeaturn/my-first-testing-repo

頁面上有一個 Code 的按鈕,點下去有 clone(複製)的指示

在電腦上先移動到別的資料夾底下,使用 clone 指令把專案抓下來

git clone [email protected]:howtomakeaturn/my-first-testing-repo.git

完成之後,會看到在新資料夾底下,有一份一模一樣的專案!


在新資料夾底下,把 my-work-3.html 裡面隨意加上幾個字,接著

git add my-work-3.html

git commit -m 'commit from new folder'

git push

打開 github,會在上面看到有被更新!


回到之前的「舊資料夾」底下,把 my-work-4.html 裡面隨意加上幾個字,接著

git add my-work-4.html

git commit -m 'commit from old folder'

git push

會發現上傳失敗!

To github.com:howtomakeaturn/my-first-testing-repo.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'github.com:howtomakeaturn/my-first-testing-repo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

原因是,git 發現,你從兩個不同的地方,分別更新過不同檔案,git 怕你會搞混編輯歷史紀錄

(github 上現在有4筆 commit,目前資料夾上也是4筆 commit,git 無法分辨哪個第4筆是最新的)

所以 git 希望你能先把最新版本抓下來,再送出你的版本,比較不會有爭議!

把新版本抓下來的指令是

git pull

抓完之後,因為 git 自動把雲端版本、跟本機你剛改過的版本,合併在一起了

會請你打一段小訊息,備註這次合併

通常你就使用 ctrl + x 或者 :q 離開終端機編輯器就可以了

完成之後,使用 git log 會看到,現在有6筆 commit 紀錄,最後一筆是剛剛自動合併的 commit

Merge branch 'main' of github.com:howtomakeaturn/my-first-testing-repo

使用 git status 也會看到

On branch main
Your branch is ahead of 'origin/main' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

git 提示你目前比雲端版本還多了 2 筆 commit(一筆是你剛加的,一筆是關於自動合併的)

使用 git push 通通推上去 github 吧!


看到這邊你可能會想,剛剛那筆自動合併的 commit 好像有點多餘?

就把 commit from new folder 以及 commit from old folder 按照時間順序排列,不就好了嗎?

其實,那是因為這兩筆 commit 內容沒有重疊,所以看起來很單純

實務上很多時候,兩筆 commit 會更新到「相同的檔案」之中的「同樣幾行程式碼」

這時 git 如果按照時間順序排列,就會讓「較晚送出 commit 的人」把「較早送出 commit 的人」的工作內容覆蓋掉!

這樣一來,根本沒辦法團隊工作:晚提交的人,會一直破壞掉早提交的內容!

所以 git 一律會多一筆「合併 commit」。平常就自動合併沒問題,有衝突時,就可以在這筆 commit 處理

有點不懂沒關係,我們下一課會練習

課後作業

接續前一課的作業,專案已經傳到 github 了

假設你原本是用家裡的電腦開發,現在你打算帶著筆電,出門到咖啡廳繼續開發

請拿出你的筆電,把 github 上那個專案 git clone 到筆電上面

如果你沒有筆電,沒關係,在電腦上另外找個資料夾,用 git clone 從 github 抓專案下來

這樣兩個資料夾對應同一個專案,也可以,模擬跨裝置同步專案


現在分別有兩個資料夾,內容一模一樣,我這邊分別用 at-home 以及 at-laptop 稱呼兩個資料夾

請按照以下步驟,送出 commit

第一步

at-home 資料夾,建立一個檔案 create-this-file-at-home.html 內容放

<p>我在家裡新增這個檔案</p>

然後送出 commit,接著 git push 出去

你會看到 github 上面就被更新了

第二步

at-laptop 資料夾,建立一個檔案 create-this-file-at-laptop.html 內容放

<p>我在咖啡廳新增這個檔案</p>

然後送出 commit,接著 git push 出去

你會看到錯誤訊息!

git 會跟你說,有衝突,請先將資料夾內容下載同步,再更新

第三步

所以請輸入 git pull 先把資料夾更新到最新版本

接著用 git log 看一下,會看到有關 create-this-file-at-home.html 那個檔案的 commit

第四步

這時再 git push 出去

你會看到 github 上面就被更新了

完成以上任務,你就完成這次的課程目標了!


交作業的方法:

可以把 github 專案連結,貼到留言區


歡迎將作業成果,在下方留言,跟大家分享,讓大家給你一些回饋!

可以將每課學到的觀念、關鍵字,丟到網路上去搜尋、研究一下!

發問請在「討論專區」為主,或者分享學習筆記、寫學習心得!

貼文都會出現在個人檔案頁面,成為學習歷程、部落格紀錄!

未來面試時,分享給面試官看,會讓人知道你的積極程度!


共有 7 則留言

交作業 https://github.com/birdiewu/myproject

按讚的人:

很棒!就是這樣!順利完成!

作業繳交 https://github.com/hung-YE/codelove-git

按讚的人:

交作業 https://github.com/superyngo/firstpracticerepo

按讚的人:

交作業,再麻煩站長了,謝謝! https://github.com/pchun2330/my-first-testing-repo

按讚的人:

交作業 https://github.com/adens123/codelove

按讚的人:

作業繳交

https://github.com/Adol8343/codeLoveTestUpload

按讚的人: