# 專案協作與開發 - Git ## 筆記 [課程共筆](https://hackmd.io/@yillkid/HkP4U9vE2) [上課講義](https://docs.google.com/presentation/d/1LVVzpNTZCkzvQKuiqci6qqSDRqWX1QuvgTGAaE3ZPsA/edit#slide=id.g1090ffa1a8f_0_89) * WD (working directory/tree) 本地端尚未提交的檔案 * Stage 暫存區,兩次提交的差異也叫Revision * Repository倉庫,用來存放檔案與修改紀錄 * 種類: Remote / Local ![](https://hackmd.io/_uploads/rJBRVzKEh.png) ![](https://hackmd.io/_uploads/r1HvVzt4h.png) * [Git安裝教學](https://git-scm.com/book/zh-tw/v2/%E9%96%8B%E5%A7%8B-Git-%E5%AE%89%E8%A3%9D%E6%95%99%E5%AD%B8) ### 開始使用Git * 初始化: **$git init** 讓 Git 對此目錄開始版控 * 將檔案從 WD 添加到 Stage,有三種寫法: **$git add 檔案名稱 $git add .(全部的檔案) $git add 檔案1 檔案2 ...(多選)** * 查看狀態指令,看哪些檔案在WD區、哪些檔案在Stage區: **$git status** * 將檔案由Stage提交到Repository,注意會將Stage所有檔案一次一起提交,[說明參考](https://wadehuanglearning.blogspot.com/2019/05/commit-commit-commit-why-what-commit.html) **$git commit -m "說明此次提交的文字內容"** * 查看Repository歷史紀錄,Head會放在最新的一筆紀錄上 **$git log** * 查看詳盡日誌,被reset的log也會顯示: **$git reflog** * 查看被reset hard移除的檔案內容,先用reflog找到編號,再使用: **$git show 編號** ### 恢復各種狀態與時光機 ![](https://hackmd.io/_uploads/SyyYKMF4h.png) ![](https://hackmd.io/_uploads/BJ42YfKN3.png) ### 使用Github * 創建Github帳號: https://github.com/SamanthaYu0611/SeleniumPractice * 將Github的Repository複製到Local端 **$git clone 網址** * 在Local端會出現一樣名稱的資料夾,在該資料夾內進行WD/Stage/Local Repository的作業。 * 將Local Repository的檔案推到GitHub: **$git push https://(github的key)@github.com/帳號名稱/資料夾名稱** * 不同專案協作成員必須先git clone將Github上的資料複製下來後,在<span style="color:blue;">該資料夾下</span>使用pull更新別的成員push的最新檔案與log。 **$git pull** * 使用分支: **$git branch 確認目前所在分支 $git branch 新分支名稱 (新增分支) $git checkout 分支名稱 (切換分支)** 分支推上Github後,會出現pull request的請求 ![](https://hackmd.io/_uploads/SJb7_-qNn.png) * 關於合併(git merge): 建立A、B兩分支,在A分支新增檔案尚未進入stage前,A、B分支都可看到新增的檔案。當在A分支將檔案進入版控(git add、git commit)後,則在B分支就看不到該檔案。若在B分支將A分支合併後,則在B分支又可看到在A分支版控的檔案。可使用指令如下: **$git branch #查看所有分支與目前分支 $git branch newBranch $git checkout newBranch $touch test.txt $git add test.txt ; git commit -m "feat:Add new branch file." $git chekcout main $ls #確認看不到test.txt $git merge newBranch $ls #檔案test.txt又出現了 $git log #在A分支的log也會合併過來** * 每一個任務(Task or Function)都該建立一個Branch,分別提出pull request。在Github上的pull request都可以review完做註解。Merge後可刪除分支(任務完成通常就該刪除)。如果 ![](https://hackmd.io/_uploads/Syul_z5Nn.png) ![](https://hackmd.io/_uploads/BJWbdMqV2.png) * 在Local端刪除Branch: **$git branch --delete 分支名稱 $git branch -D 分支名稱** * fork與clone的差異: https://ithelp.ithome.com.tw/articles/10140305 * Fetch:將遠端的Repository下載下來。 與git pull有點像,但不同的是,git pull是直接把程式同步到WD,而git fetch會另外開一個的分支儲存。 ![](https://hackmd.io/_uploads/rJ4x3Bc43.png) * 用Github架網站: 1. 只支援index.html 或 README.md 2. 可以到別人Github的Fork整個資料夾到自己的,然後再到自己的repository修改。 ![](https://hackmd.io/_uploads/By2a_mqNh.png) 4. 在Repository-Setting-Pages,修改Branch後Save,到Action看推的狀態,成功後則網址會出現在Pages的上方。 ![](https://hackmd.io/_uploads/BJOQuQ9V2.png) ### 備註 * git add 出現錯誤 解法:砍了檔案就可以繼續 ![](https://hackmd.io/_uploads/SySoU2OV2.png) ###### tags: `git` `專案協作與開發` `github`