# Git程式版本管控 - 由簡單到難(筆記) ###### tags: `進度條線上課程 Progress Bar` `Git程式版本管控 - 由簡單到難` ## Git 與Github Desktop GUI基本使用介紹 ### 1 Git課程基本介紹 Git 是一種分散式版本的版本控制系統 **Git 的優點 :** **1.免費、開源** **2.速度快、檔案體積小** 版控系統大多是記錄每個版本之間的差異,而不是完整的備份整個目錄,所以整個目錄的大小就不會快速的增加。 **3.分散式系統** CVS 或是 SVN 之類的集中式的版控系統(Centralize Version Control),都需要有一台專用的伺服器,所有的更新都需要跟這台伺服器溝通。也就是說,萬一這台伺服器壞了,或是沒有網路連線的環境,版本控制功能就沒辦法使用。 Git 是一款分散式的版控系統(Distributed Version Control),雖然通常也會有共同的伺服器,但即使在沒有伺服器或是沒有網路的環境,依舊可以使用 Git 來進行版控,待伺服器恢復正常運作或是在有網路的環境後再進行同步,不會受影響。而且,事實上在使用 Git 的過程中,大多的 Git 操作也都是在自己電腦本機就可以完成。 **Git 的缺點** **1.易學難精** > [git維基百科](https://zh.wikipedia.org/wiki/Git) > [為你自己學 Git](https://gitbook.tw/) > [git 指令](https://silverwind1982.pixnet.net/blog/post/286048390) ___ ### 2 [Mac] 安裝Github Desktop ___ ### 3 [Windows] 安裝Github Desktop **1.到[Github Desktop](https://desktop.github.com/)下載** ![](https://i.imgur.com/8Vv3oGo.png) **2.skip this step(跳過這一步)** ![](https://i.imgur.com/L7QS2L8.png) **3.設定** ![](https://i.imgur.com/h2CMzzM.png) **4.finish(結束)** ![](https://i.imgur.com/aCDpEUP.png) ___ ### 4 .git介紹與建立Repo **1.本地端新增repository(倉庫)** ![](https://i.imgur.com/m4IfqSz.png) **2.設定** ![](https://i.imgur.com/iVhPri2.png) ___ ### 5 提交第一個commit與介紹Readme **1.新增檔案,修改內容,存檔** ![](https://i.imgur.com/ODgNsF3.png) **2.提交commit(遞交)** ![](https://i.imgur.com/yjjKf5x.png) **3.查看history(歷史)** ![](https://i.imgur.com/VzEDL38.png) ___ ### 6 反悔、還原(revert)commit 的幾種方式與解決conflict **方法一 : 1.revert(還原),回到上一步,會新增一個history,commit紀錄,適合多人專案** ![](https://i.imgur.com/mxaYPIB.png) **2.1.如revert(還原)失敗,處理步驟如下** ![](https://i.imgur.com/Jsou1LU.png) **2.2取消changes** ![](https://i.imgur.com/fifqW0x.png) **3.1.如revert(還原)後出現衝突(Conflict),處理步驟如下** ![](https://i.imgur.com/ZP3hUv8.png) **3.2.到檔案編輯程式** ![](https://i.imgur.com/HcgskEH.png) **3.3.確定程式內容** ![](https://i.imgur.com/QVwHP1i.png) **3.4.重新提交commit** ![](https://i.imgur.com/h10k9mo.png) **方法二 : 1.Undo(撤消),可以回到上一步,但不會有history,多人專案不建議用** ![](https://i.imgur.com/hoQVpY4.png) ___ ### 7 簡述遠端Repo與Github的關係 將repository,放置在遠端的網路空間,最簡單的方式,是把它壓縮成zip檔,直接放在網站上,讓大家下載zip檔,那zip檔有沒有包含history過往的歷史紀錄,就要看有沒有把.git檔壓縮進去,如果有壓縮進去,zip檔就會大一點,不過有時候,我們並不喜歡做打包這個動作,並放置在網路空間上,比如google的雲端硬碟,就會有權限上的設置等等,如需要綁定email,可能需要對方的email之類的事情,那並不是很方便去控管程式,所以在多人協作上,是不方便的。所有的git資料都存在.git資料夾裡面,那如果這個檔案夾是在遠端的話,那大家都能共用那一個資料夾,只要大家都去看遠端的.git資料夾,就知道大家的工作到哪裡,當然還是會有衝突(Conflict)會發生,但整理情況比用zip檔去做管理好,git的開發本來就是為了處理團隊開發的問題,說那麼多只是要帶出Github,這網站的功能,所以Github就是可將.git放在在遠端的平台,網路空間(遠端伺服器),***Github就是提供遠端服務的廠商***。 ___ ### 8 建立Github帳號、方案介紹與變更主要Email **1.建立Github帳戶** ![](https://i.imgur.com/v602Jxa.png) **2.填寫相關資訊,username會影響自己的網址** **![](https://i.imgur.com/rUm5AT6.png) 3.選擇免費,送出,其他設定都可以之後,調整** ![](https://i.imgur.com/KoUMKYK.png) **4.驗證email** ![](https://i.imgur.com/l3ptKUJ.png) **5.如要改email,去Settings** ![](https://i.imgur.com/l7nYxXM.png) **6.新增email** ![](https://i.imgur.com/tQoLczU.png) **8.驗證email** ![](https://i.imgur.com/bjPwIXh.png) **9.改email** ![](https://i.imgur.com/vxAWJLL.png) **10.改email** ![](https://i.imgur.com/xsb3sGN.png) **10.修改成功** ![](https://i.imgur.com/fxiuJP4.png) >[github](https://github.com/) ___ ### 9 使用Github Desktop上傳建立Git Repository到Github上 **1.將本端repository,上傳到Github,Github Desktop先連線設置** ![](https://i.imgur.com/S3IxwrY.png) **2.登入Github帳號** ![](https://i.imgur.com/RqRBuFG.png) **3.確定登入Github** ![](https://i.imgur.com/7IB8J1Q.png) **4.如需修改本地端email,去下圖設定修改** ![](https://i.imgur.com/rk8UEK8.png) ___ ### 10 使用Github Desktop Clone 與加入共同開發者(Collaborators) **1.將Github上的資料Clone下來,複製url** ![](https://i.imgur.com/ICgiBy5.png) **2.使用Github Desktop Clone下來,填寫相關資訊,Clone** ![](https://i.imgur.com/hirlk2n.png) **3.加入共通開發者,輸入開發者名稱** ![](https://i.imgur.com/sOlGRCf.png) **4.開發者email驗證** ![](https://i.imgur.com/201uG6b.png) **5.查看開發者名稱** ![](https://i.imgur.com/ROFOj9V.png) ___ ### 11 使用Fetch 與 Pull 同步專案 **1.點選Fetch,查看是Pull或push** ![](https://i.imgur.com/EBd5p8s.png) **2.點選Pull下來到本地端** ![](https://i.imgur.com/2KuXMtm.png) **3.如何移除repo** ![](https://i.imgur.com/gp2IPYo.png) **4.點選remove** ![](https://i.imgur.com/LUuagGZ.png) ___ ### 12 分支Branch介紹、本地端與遠端的新增與刪除 **1.新增branch** ![](https://i.imgur.com/PzTFsQ1.png) **2.填寫資訊** ![](https://i.imgur.com/lj3ybVV.png) **3.如要刪除branch,如下圖** ![](https://i.imgur.com/fhGslas.png) ___ ### 13 合併(Merge)目標分支(Branch),解決簡易合併衝突(Conflict) **1.master合併dev,大魚吃小魚** ![](https://i.imgur.com/DtAanW5.png) **2.點選dev,合併** ![](https://i.imgur.com/QdW1OIk.png) **3.按undo,可以回上一步** ![](https://i.imgur.com/dnBK23v.png) ___ ## 使用Git終端機指令發揮所有威力 ### 1 [Mac]在Mac上面安裝Git指令 ___ ### 2 [Mac] 基本終端機介面使用與git指令測試 ___ ### 3 [Windows]在Windows上面安裝Git指令 **1.下載git** ![](https://i.imgur.com/dhuIuw2.png) **2.點選檔案開啟** ![](https://i.imgur.com/YQNHJzH.png) **3.下一步** ![](https://i.imgur.com/qazZRSN.png) **4.下一步** ![](https://i.imgur.com/FWvQWXN.png) **5.下一步** ![](https://i.imgur.com/79zaJ5X.png) **6.下一步** ![](https://i.imgur.com/qdYyNpu.png) **7.下一步** ![](https://i.imgur.com/s5c14uK.png) **8.下一步** ![](https://i.imgur.com/arg7EEu.png) **9.下一步** ![](https://i.imgur.com/kCLqDNt.png) **10.下一步** ![](https://i.imgur.com/Kyjs4Ym.png) **11.下一步** ![](https://i.imgur.com/oaUxOF9.png) **12.下一步** ![](https://i.imgur.com/nT0PZR9.png) **13.安裝完成** ![](https://i.imgur.com/S98cB7U.png) >[git](https://git-scm.com/) ___ ### 4 [Windows] git與mingw64基本指令 ![](https://i.imgur.com/U1XTYeo.png) ``` //基本指令 pwd //知道所在位址 ls //知道資料夾有甚麼內容 ls -a //知道資料夾有甚麼內容外,跟列出隱藏檔 clear //清空指令畫面 touch //touch後加,新增的檔案名,新增檔案 cd Desktop/ //cd後加要切換的資料夾名稱,比如Desktop/,切換到桌面 git config --global user.name "gh_tony" //設定使用者 git config --global user.email "tony@demo.com" //設定email git config --global --list //查看使用者跟email git init //初始化 git log //查看git紀錄,會把所有commit列出來,後面有可以加入commit號 git status //查看檔案狀態,哪個有加入版本管理,哪個沒有 git add . (全部檔案) or git add <file> //設定準備上傳的檔案 git commit -m "first added commit" //設定確定上傳的檔案 ``` ___ ### 5 Git倉庫(Repo)版本紀錄原理與.git內容簡介 ___ ### 6 Git Object影片說明與使用方式 ___ ### 7 [補充] 深入探討Git Object與Git原理 ___ ### 8 [補充] 傳統Diff與Path版本管理和Git在根本上的不同,以及為何Git不擅長處理大型檔案 ___ ### 9 Git config 為不同的Repo設定不同的Author ``` mkdir //建立資料夾,指令後新增資料夾的名稱 //global代表全域,如無特別設定,會預設使用它 git config -l //可以看到git許多的設定 git config user.name "gh_tony" //會更改個別repo的,提交(commit)作者 ``` ___ ### 10 重做復原git reset(包含 --hard 與--soft) **git reset在Github Desktop上就是Undo功能,但在使用上有許多不同的情境 1.情境1,單純回到上一步** ![](https://i.imgur.com/WpTy2DZ.jpg) **2.情境2,整理commit,分開commit等等** ![](https://i.imgur.com/E26b3Fh.jpg) **3.情境3,不要這個commit時,回上一步,如下圖指令`git reset --hard HEAD^`** ![](https://i.imgur.com/fzP7dbI.png) 4.情境4,不要這個commit時,回上一步,但突然又要了`git reset --hard 輸入那個commit的身分證號` ![](https://i.imgur.com/JgbI6UV.png) **5.情境5,回到前幾個commit** ![](https://i.imgur.com/H99psno.png) **6.情境6,回到一個commit,但修改的程式繼續保留`git reset --soft HEAD^`** ![](https://i.imgur.com/H9tfOTh.png) ``` //指令 git reset //全部回到上一步 git reset <file> //將<file>回到前一步 git reset --hard HEAD^ or git reset --hard HEAD~1 //回到第一步前 git reset --hard HEAD~n //回到第n步 git reset --hard <commit_id> //回到<commit_id>,這一步 git reset --soft HEAD^ //回到一個commit,但修改的程式繼續保留 ``` ___ ### 11 git checkout 與建立branch (包含 git reflog) **git checkout常用在有branch時,git checkout本身是在將目前HEAD切到某commit_id狀態下,可以為commit_id取名,會新增一個branch(分支)** **1.將當下HEAD切到某commit_id狀態下** ![](https://i.imgur.com/ADt84kE.jpg) **2.為此HEAD取名,會新增一個branch** ![](https://i.imgur.com/6egwsvs.jpg) **3.假設切到master下** ![](https://i.imgur.com/wirtnpQ.jpg) **4.查看目前是哪一個branch** ![](https://i.imgur.com/H6T7jND.jpg)** 5.查看HEAD做了甚麼動作** ![](https://i.imgur.com/13KMLR7.jpg) ``` //指令 git checkout <commit_id> //切到<commit_id>下 git checkout -b <new_branch_name> //為當下HEAD取名 git checkout <branch_name> //切到某branch下 git branch //查看當下branch git reflog //查看HEAD做過甚麼動作 ``` ___ ### 12 簡單的Git分支策略與合併、刪除分支(branch) **1.新增一個branch** ![](https://i.imgur.com/4Czkvw6.jpg) **2.刪除一個branch** ![](https://i.imgur.com/WlTGfFl.jpg) **3.將shooping(merge)合併到dev** ![](https://i.imgur.com/J1Z3BF2.jpg) ``` //指令 git checkout -b <new_branch_name> //新增branch git checkout -D <branch_name> //刪除branch git merge branch_name<> //合併branch ``` ___ ### 13 多重檔案的commit 原則以及追查變化方式(log, show, status, diff) ``` git log //在本機端,顯示目前已經 commit 的所有修改內容,後續可以增加參數來進行過濾 git show //在本機端,顯示目前 branch 最新 commit 的修改內容 git status //在本機端,顯示目前的檔案修改情況(包含新增及刪除) git diff //在本機端,顯示目前在 工作目錄 的修改內容 git diff --cached //在本機端,顯示目前在 暫存區 的修改內容 ``` ___ ### 14 利用專案來介紹.gitignore寫法 ``` //git紀錄的是檔案 .keep //無內容隱藏檔,為了將空資料夾也記錄到版控 .gitignore //忽略檔案,有些檔案我不想放在 Git 裡面,可以放跟目錄或子目錄 ! //相反 * //所有 ``` ___ ### 15 git mv 與 方便追蹤commit的方式 ``` git mv <file> <new_name> //重新命名檔案,或搬移檔案 ``` ___ ### 16 git rm 與刪掉檔案讓gitignore運作正常(git rm --cached) ``` git rm <file> //來告知 git,哪些是我們將要刪除的檔案 git rm --cached //刪掉檔案讓gitignore運作正常 ``` ___ ### 17 用 git rebase 來整理commit ``` //用來調整commit順序跟commit的內容 git rebase ``` ___ ### 18 利用git cherry-pick來合併單次commit ``` git cherry-pick //單次合併commit ``` ___ ### 19 git tag的使用意義,與Branch之間的差異 ``` //標籤,應該要是固定版本,比如是在master git tag -a <tag_name> -m "tag_敘述" ``` ___ ### 20 git stash的使用方式與概念 ``` git stash //暫存被修改的檔案 ``` ___ ## Git 遠端版本管理 ### 1 Git remote章節簡介 **Git remote只的是遠端,可以是github,也可以是一個資料夾** ___ ### 2 git 遠端倉庫的介紹與實作(git init --bare) ### 3 git remote 新增、改名、改路徑 ### 4 git push 與 pull 分支,解決merge commit 問題 ### 5 git pull多重分支,與同步遠端紀錄 ### 6 git pull與fetch的差異與使用時機,git pull -f 的注意事項 ### 7 為什麼要用git init --bare ### 8 git ls-remote, git remote show 與 git remote prune ## 利用Github做遠端倉庫與解決疑難雜症 ## 常見的Git團隊方式 ## GitKraken GUI介面教學