--- title: '[社課]Denny學長的Git教學(上)' tags: - 社課 - 共筆 - Git --- # Denny學長的Git教學(上) <!-- 活動簡述 --> **簡報連結:** - https://denny.one/git-slide - https://denny.one/git-g0v-sch001/ > 請從這裡開始 ## 事前準備 參考:[Denny 學長的 Git 教學—事前準備](/Gvcf33DfQa6ryz3-pMzJzw) ## 環境設定 :::info <i class="fa fa-info-circle" aria-hidden="true"></i> **Git的執行環境** Windows 系統在安裝 Git 時會順帶將 Terminal 安裝進去,可以用 Git Bash 進行 Git 的操作。若電腦上有安裝 Windows Terminal + Power Shell 的環境的話,也可以直接在上面執行。 Mac 和 Linux 則直接開啟內建的 Terminal 直行即可。 ::: 首先先在 Terminal 中輸入以下指令 ```shell! $ cat ~/.gitconfig ``` 這時候畫面上會出現錯誤,找不到檔案,或著是直接是一片空白那都會是正常的。`.gitconfig` 是儲存 Git 設定的檔案,預設的目錄會在家目錄 `~/` 底下。 在正式開始使用 Git 之前需要先把資料都設定好。下面兩個指令依序是設定你的電子郵件以及使用者姓名。 ```shell! $ git config --global user.email "you@example.com" $ git config --global user.name "Your Name" ``` 把 ui 介面設成有顏色的 ```shell! $ git config --global color.ui true ``` 設定預設文字編輯器,不會使用 Vim 的話就一律用 notepad 或是其他編輯器。 ```shell! $ git config --global core.editor notepad # 不會使用 vim 的用這行 $ git config --global core.editor vim ``` 設定指令別名 ```shell! $ git config --global alias.co commit $ git config --global alias.lg "log --color --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --" ``` 這樣設定後的結果,在終端機輸入 `git co` $\rightarrow$ `git commit`,輸入 `git lg` 則會有格式化過後,精簡漂亮的版本紀錄。 :::warning <i class="fa fa-question-circle" aria-hidden="true"></i> **為何使用 `--global` 參數** ::: 這時後再使用 `cat ~/.gitconfig` 指令,就會發現剛剛的設定都顯示在螢幕上了。 :::info <i class="fa fa-info-circle" aria-hidden="true"></i> **`cat` 指令** `cat` 指令是做什麼的我們可以用 `man` 指令來查詢 ```shell! $ man cat CAT(1) User Commands CAT(1) NAME cat - concatenate files and print on the standard output SYNOPSIS cat [OPTION]... [FILE]... DESCRIPTION Concatenate FILE(s) to standard output. With no FILE, or when FILE is -, read standard input. -A, --show-all equivalent to -vET -b, --number-nonblank number nonempty output lines, overrides -n Manual page cat(1) line 1 (press h for help or q to quit) ``` 簡單來說就是將檔案的內容印在終端機上,使用的方式就是 ```shell! $ cat [檔案] ``` e.g. ```shell! $ cat ~/.gitconfig ``` ::: ## 版本庫 Repository 在使用 Git 做版本控制時,我們會建立一個資料庫來管理一個專案的程式碼,記錄各個版本之間程式碼的差異。而這個資料庫被稱作版本庫(repository,簡稱 repo)。 ### 建立版本庫 我們先打開 GitHub 建立一個新的版本庫。 ![image](https://hackmd.io/_uploads/rkbvob6eC.png) 右上角有一個綠色的按鈕 <span style="background-color:#238636; color: #ffffff; padding: 0.5rem; border-radius: 0.375rem; font-size:12px"><i class="fa fa-book" aria-hidden="true"></i> **New**</span> 按下去之後會跳轉到建立 repo 的頁面 可以看到頁面最上面會有 repository name 以及 Description,這兩個欄位會是這個 repo 的基本資訊。 ![image](https://hackmd.io/_uploads/HJ_FgfagA.png) ### public/private 接下來往下滑會看到兩個選項:public、private,決定了這個 repo 是否公開,預設是 public。 ![image](https://hackmd.io/_uploads/BkojNUTl0.png) 使用 private 是要付費的,但如果是學生的話,GitHub 有提供 [GitHub Student Developer Pack](https://education.github.com/pack),可以去申請。 ### `README.md` README檔案是專案的說明文檔,上面通常會提供一些關於這個專案的安裝方式、部署方式、相依套件......等等。在 GitHub 上,多數的 `README` 檔案都是使用 MarkDown 撰寫的,因此學會撰寫 MarkDown 檔案在軟體工程是非常重要的。 ![image](https://hackmd.io/_uploads/ryNkWIpgC.png) - [GitHub 所提供的 MarkDown 教學](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax) ### `.gitignore` `.gitignore` 檔案是用來將開發過程種所產生的檔案剔除於版本庫之外。在開發過程中,程式會產生一堆有的沒有的檔案,以 C 語言為例,有`*.o`、`*.elf`、`*.exe`、`*.out`。由於這些檔案都是可以透過編譯不斷產生的,若把這些檔案都納入版本庫,會讓裡面的資訊變得很雜亂。因此我們會用 `.gitignore` 檔案,將我們不想要納入版本庫的檔案規範住。 - [A collection of `.gitignore` templates](https://github.com/github/gitignore) ![image](https://hackmd.io/_uploads/SknEW8TlA.png) 用 C 語言為例,我們可以撰寫兩個檔案 `a.c`、`b.c`。 ###### a.c ```c #include <stdio.h> int main(){ printf("Hello\n"); return 0; } ``` ###### b.c ```c #include <stdio.h> int main(){ printf("World\n"); return 0; } ``` 這時我們可以使用指令比較兩個檔案的差異,不同之處會被標示出來。 ```shell $ vim -d a.c b.c ``` ![比較 `a.c` 和 `b.c` 的差異](https://hackmd.io/_uploads/Byr_NPpgC.png) 然後我們編譯兩個檔案。 ```shell! $ gcc a.c -o a $ gcc b.c -o b ``` 然後再去比較兩個檔案的差異,除非你是什麼肉眼 binary 分析大師,不然一般人根本看不出兩者的差異是動了什麼。 ![比較 `a.exe` 和 `b.exe` 的差異](https://hackmd.io/_uploads/S14_4PaeA.png) 這就是為什麼我們在管理程式碼的版本時,只管理程式碼的部分。而由程式碼產生的檔案,我們則透過 `.gitignore` 來限制。 ### License 我們在把程式碼開源出來時,不代表希望別人可以毫無限制的去抄自己的程式碼。所以我們可以使用別人所規範出來的授權條款,來限制使用你程式碼的開發者。 - [授權條款介紹](https://ossf.denny.one/tw/licenses) 也可以用 [授權條款精靈](https://ossf.denny.one/tw/license-wizard/licensewizardv33.html) 來快速選擇你所需要的授權條款。 ![image](https://hackmd.io/_uploads/rywFNIpxC.png) ### Repo 參考設定 :::info <i class="fa fa-info-circle" aria-hidden="true"></i> **Repo 建立頁面** ![image](https://hackmd.io/_uploads/H14iCbpeC.png) ::: 建立完成後,會得到大概長這樣的頁面,裡面會有剛剛所選的 `README.md`、`.gitignore`、`LICENSE`。 ![螢幕擷取畫面_17-4-2024_22849_github.com](https://hackmd.io/_uploads/rkS7d8Te0.jpg) ### Repo 建立成功 經歷了辛苦的設定過後,==**把你的 Repo 刪掉**==。 點擊最上面導覽列的 **<i class="fa fa-cog" aria-hidden="true"></i> Settings** ![image](https://hackmd.io/_uploads/BkdC9UTx0.png) 滑到頁面的最下面,會有一個 Danger Zone,按最底下的 <span style="background-color:#21262d; color: #f85149; padding: 0.5rem; border-radius: 0.375rem; border-color: #30363d;font-size:12px">**Delete this repository**</span> ![image](https://hackmd.io/_uploads/r1YF3Lag0.png) 會先跳出視窗詢問你要不要刪除。 ![image](https://hackmd.io/_uploads/BJy03L6e0.png) 然後跟你講真的很危險。 ![image](https://hackmd.io/_uploads/r1hWaU6gC.png) 最後依照指示,把 `帳號/Repo名` 輸入進去就可以刪除 Repo 了。 ![image](https://hackmd.io/_uploads/HJgL6IpxR.png) ## 本地端建立 repo 接著重新建立一個 repo,這次除了 repo name 跟 Description 外剩下的都不要額外勾選。最終應該會得到類似以下的畫面。 ![image](https://hackmd.io/_uploads/HyaIkP6lC.png) 上面很貼心了告訴你了你可以怎麼做,與本地端的 repo 連結。