Git

tags: summer course

contributed by <YW0330>

你為什麼要學 Git ?

版本控制簡介

  • 版本控制 Version Control System (VCS)
    • 紀錄修改歷程
  • 應用-主要是文字類檔案
    • 程式 (C/C++, java, python)
    • 論文 (LaTex)
  • VCS 分類
    • 中央式
      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 SCM

  • 官方網站
    • 下載

      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 →
      Windows: 64-bit Git for Windows Setup.

  • Source Code Management
  • Linux kernal 開發者 Linus Torvalds 創建
  • C 語言編寫
  • 分散式

Git 操作

使用者設定

  • 查看設定指令 $ git config --list
  • 設定使用者名稱 $ git config --global user.name "John Doe"
  • 設定email $ git config --global user.email johndoe@example.com

基礎概念

  • Repository (倉儲): 儲存修改紀錄的資料庫
    • Local Repository: 在個人電腦上
      • Git directory: .git 資料夾,儲存 Local repository 的所有資料,包含修改紀錄
      • Working tree: 當前的工作目錄,即正在編輯的檔案
      • Staging area (預存區): 紀錄需要儲存進 local repository 的檔案變更,沒有在 staging area 的檔案不會進入 repository
    • Remote Repository: 在遠端伺服器 (server)
  • Working tree 的檔案狀態
    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 →
    • Tracked: 有進行版控的檔案
      • commited/unmodified: 已提交 (commit) 修改的檔案
      • modified: 從上次提交後有做過修改
      • staged: 等待提交
    • Untracked: 沒有進行版控的檔案

基本操作

  1. 開啟 Git Bash
    • 右鍵菜單點選 Git Bash Here

    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 →
    Windows 系統也可以用 cmd,linux系統直接使用 Terminal

  2. 複製 GitHub 上的倉儲
    ​​​​$ git clone <url>
    

    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 →
    <url> 可由 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 →

    一個專案只需要一次 clone

    • 變更複製的倉儲名稱
      ​​​​​​​​$ git clone <url> <desired repository name>
      

      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 →
      在 windows 中使用 $ git clone 時,注意不要使用 Ctrl+V 來貼上文字,使用 Ctrl+V 貼上文字然後再右鍵按 paste 會造成以下 bug

      ​​​​​​​​Git fatal: protocol 'https' is not supported
      

      解決方式: stackoverflow-Git fatal

    • 下載整個倉儲
      • 上面圖片點選 Download ZIP
    • 下載特定 GitHub 檔案或資料夾
  3. 新建 git 倉儲
    • 在需要進行版控的目錄下
      ​​​​​​​​$ git init
      
      完成後會看到該目錄下多了一個 .git 隱藏資料夾。
  4. 將檔案加入 Git 管理中
    • 新增當下目錄中的單一檔案
      ​​​​​​​​$ git add <filename>
      
    • 新增當下目錄中的全部檔案
      ​​​​​​​​$ git add .
      
  5. 查看檔案狀態
    ​​​​$ git status
    
  6. 提交
    ​​​​$ git 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 →
    可改用 $ git commit -a 這道命令,讓 Git 自動追蹤變更的檔案並提交。

    • Commit Message
      • 第一列: 標題摘要
        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 commit -m "<commit title>"
        
      • 第二列: 空白
      • 第三列: 內文,如功能詳情與修改理由等
    • 如何寫好 Git Commit Message

其他常用操作

  • 重新提交
    ​​​​$ git commit --amend
    
    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 commit --amend 僅限於重新提交最新的 commit message,若要修改之前的 commit message,必須使用 git rebase -i HEAD~~<倒數第幾個提交的 commit>
    範例:
    fewletter/fibdrv commit
    ​​​​$ git rebase -i HEAD~10
    
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    選取要修改的檔案修改前贅詞 pickrreword,儲存檔案在 git-rebase-todo,接著離開
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    會看到
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    然後將想改的 commit message 加到上面,儲存檔案在 COMMIT_EDITMESG,接著離開
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →
    ​​​​$ git push -f
    
    f 代表 force 的意思,他會覆蓋掉整個 branch 的 commit message
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    從下圖可以看到 Fix big number in driver 已經改成 Fix big number size and time in driver
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →
  • 查看歷程記錄
    ​​​​$ git log
    
    • 僅顯示標題
      ​​​​​​​​$ git log --oneline
      
  • 查看尚未預存的修改
    ​​​​$ git diff
    
  • 將 staged 檔案移出預存區
    ​​​​$ git reset <filename>
    
  • 復原被修改的檔案
    ​​​​$ git checkout -- <filename>
    

    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 →
    對未提交 commit 的檔案進行復原或刪除,有可能導致檔案遺失。

VS Code 中操作 Git

  1. 安裝 GitLens, Git History 擴充插件

  2. 新建 git repository
    點選 初始化
  3. 可用檔名上的 +, - 號變更檔案狀態
    • Changes (變更): 未加入 staged area
    • Staged Changes (暫存的變更): 已加入 staged area
    • 檔案右邊名稱最右邊字母表示狀態
      • A: added
      • U: untracked
      • M: modified
      • D: deleted
  4. 在文字框中輸入 commit message,Ctrl+Enter 可以提交 commit

GitHub

基本操作

  1. 在本地端加入 remote repository
    ​​​​$ git remote add <remote username> <url>
    
    • 常見的 <remote username>: origin,clone 的預設名稱
  2. 上傳 local repository 到 remote repository
    ​​​​$ git push <remote username> <local branch>
    
    • 常見的 <local branch>: master, main
    • 在第一次上傳後,往後可以直接執行 $ git push
  3. 下載 remote repository 到 local repository
    ​​​​$ git pull <remote username> <local branch>
    
    • 往後可以直接執行 $ git push

參與公開專案

  1. 參與一個沒有權限的專案,可利用 fork 複製一份專案副本到你的 GitHub 帳號。
  2. 對合作專案提交 pull request
    當你參與了一個專案 (已經進行 fork 後),並且想要修改原本的合作專案,可以提交 pull request,但是提交前要先注意 fork 時候的版本跟原本專案的版本是否相同。
  • fork 版本跟原本專案的版本相同
    直接提交 pull request

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    出現下面的畫面,撰寫標題和描述然後點選 Create pull request 即可。

    請注意此處標題須符合 commit message 的規範,因為原專案作者會使用你的標題去創造 commit。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • fork 版本跟原本專案的版本不相同

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    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 →
    在提交 pull request 時,會發現一次提交所有 commit,但問題是如果你只想提交某個 commit 時怎麼辦?

    1. 首先先增加遠端儲存庫
      ​​​​​​​​$ git remote add upstream <原本專案網址>
      

      git 本身即支援多個倉儲,而自己本身的默認倉儲的名稱為 origin。

    2. 同步遠端倉儲至本地端
      ​​​​​​​​$ git fetch upstream
      
    3. 創造分支於上游倉儲
      ​​​​​​​​$ git checkout -b <分支名稱> upstream/master
      

      請注意此處也會在原本專案中創造分支,代表原本專案作者也看的到分支。

    4. 切回原本分支
      ​​​​​​​​$ git checkout master
      
    5. 瀏覽並挑選要提交的 commit,請記住要提交的 commit 的 ID。
      ​​​​​​​​$ git log
      

      切回原本分支和瀏覽並挑選要提交的 commit 可以用看 github 中 commit 取代。

    6. 切回自己創造的分支,接著挑選自己剛才記住 commit ID
      ​​​​​​​​$ git checkout <分支名稱>
      ​​​​​​​​$ git cherry-pick <commit ID>
      
    7. 直接推回自己的專案
      ​​​​​​​​$ git push origin <分支名稱>
      
      選取要的分支然後提交 pull request。
      Image Not Showing Possible Reasons
      • The image was uploaded to a note which you don't have access to
      • The note which the image was originally uploaded to has been deleted
      Learn More →

      等到專案作者接受就可以刪掉分支。

參考資訊: 如何只提交/應用指定 commit

其他

忽略不需要的檔案

  • 新增 .gitignore 檔案
    • 範例
      ​​​​​​​​# 不要追蹤檔名為 .a 結尾的檔案
      ​​​​​​​​*.a
      
      ​​​​​​​​# 但是要追蹤 lib.a,即使上面已指定忽略所有的 .a 檔案
      ​​​​​​​​!lib.a
      
      ​​​​​​​​# 只忽略根目錄下的 TODO 檔案,不包含子目錄下的 TODO
      ​​​​​​​​/TODO
      
      ​​​​​​​​# 忽略 build/ 目錄下所有檔案
      ​​​​​​​​build/
      
      ​​​​​​​​# 忽略 doc/notes.txt,但不包含 doc/server/arch.txt
      ​​​​​​​​doc/*.txt
      
      ​​​​​​​​# 忽略所有在 doc/ 目錄底下的 .pdf 檔案
      ​​​​​​​​doc/**/*.pdf
      
  • Visual Studio Code - 右鍵開啟檔案或資料夾
  • Git 發布 Release 版本