Try   HackMD

初步掌握Git與Github應用(11) Git、Github 團隊協作 (2)

tags: 初步掌握Git與Github應用

希望藉由這份筆記,讓正在學習這類知識的你/妳,能夠得到些許幫助。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Hint: 以下筆記內容所使用的作業系統為"Windows 10"

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
下載遠端數據庫

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
使用的指令

指令 功能說明
git pull 將遠端數據庫的檔案內容更新到本地工作目錄

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
情境假設

假設現在有 "A (使用 a 資料夾作為工作目錄)"、"B (使用 b 資料夾作為工作目錄)" 兩個人在進行共同開發,而且沒有衝突發生。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
實際操作

  • 先到 Github 上建立 repository

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • A: 建立資料夾、移動路徑、clone 遠端數據庫到本地路徑下、移動路徑到遠端數據庫的資料夾下 (gittest3)

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • B: 建立資料夾、移動路徑、clone 遠端數據庫到本地路徑下、移動路徑到遠端數據庫的資料夾下 (gittest3)

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • A: 新增檔案 (index.html)、加入索引、提交索引、推送 master 分支到 origin 遠端數據庫

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • B: 查詢 commit 紀錄(目前是空的)、將 A 剛剛更新到遠端數據庫的檔案內容拉到 B 的工作目錄、查詢 commit 紀錄(可以看到 Acommit 紀錄)

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • B: 新增檔案 (b.html)、加入索引、提交紀錄、推送 master 分支到 origin 遠端數據庫

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • A: 將 B 剛剛更新到遠端數據庫的檔案內容拉到 A 的工作目錄、查詢 commit 紀錄(可以看到 Bcommit 紀錄)

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Hint:

  1. 使用git pull的時機: 要更新其他開發者的檔案內容到自己的工作目錄。
  2. 使用git push的時機: 要將檔案內容更新到遠端數據庫。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
解決 git pull 帶來的衝突

有些情況下會無法進行 push,要先進行 pull。這種情況出現的主要原因是遠端數據庫的檔案內容與本地工作目錄的檔案內容有所不符 (有一方去更新遠端數據庫,但另一方並不知情就試圖進行 push)。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
情境假設

假設現在有 "A (使用 a 資料夾作為工作目錄)"、"B (使用 b 資料夾作為工作目錄)" 兩個人在進行共同開發,現在因為 AB 不知情的情況下更新遠端數據庫,所以在 B 之後做 push 時有衝突發生。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
實際操作

  • 先到 Github 上建立 repository

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • A: 建立資料夾、移動路徑、clone 遠端數據庫到本地路徑下、移動路徑到遠端數據庫的資料夾下 (gittest5)

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • B: 建立資料夾、移動路徑、clone 遠端數據庫到本地路徑下、移動路徑到遠端數據庫的資料夾下 (gittest5)

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • A: 新增檔案 (index.html)、加入索引、提交索引、推送 master 分支到 origin 遠端數據庫

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • B: 將 A 剛剛更新到遠端數據庫的檔案內容拉到 B 的工作目錄

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • A: 新增檔案 (index2.html)、加入索引、提交紀錄、推送 master 分支到 origin 遠端數據庫

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • B: 新增檔案 (b.html)、加入索引、提交紀錄

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • B: 推送 master 分支到 origin 遠端數據庫時,出現錯誤訊息,因為在 Bpush 之前,A 已經先跑去更新遠端數據庫,造成 B 沒有 A 推送的 commit 紀錄,無法成功更新遠端數據庫

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • B: 將 A 先前更新到遠端數據庫的檔案內容拉到 B 的工作目錄

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • B: 出現合併分支的提示訊息,輸入:q後按 enter 關閉訊息

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • B: 成功推送紀錄到遠端數據庫

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • A: 將 B 剛剛更新到遠端數據庫的檔案內容拉到 A 的工作目錄

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Hint: 不用擔心 push 之後,覆蓋掉別人的檔案,因為一定要有先前別人的 commit 紀錄,才能進行 push


Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
BONUS: git pull 的實質意義

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
git pull = git fetch + git merge

使用過git pull指令後,可以知道它就是將遠端分支拉下來和本地分支進行合併(merge)。

有時候因為不希望 git pull 下來的檔案內容造成本地數據庫太亂或有衝突,可以使用git fetch origin(遠端數據庫名稱) branch(遠端分支名稱),此時本地工作目錄會多出一個 FETCH_HEAD 分支,這個分支上放的就是來自遠端數據庫的檔案內容。可以等到確認沒問題後,再合併 FETCH_HEAD 分支。


Last updated 2021/07/25 by Qi Xiang