WeHelp
Git 版本控制介紹
2022-09-25 09:15:38
本篇文章主要介紹版本控制軟體 Git,以及 Git、 GitHub 和 GitHub Desktop 的關聯性。 - 版本控制介紹 - Git 簡介 - Git 重要名詞介紹 - Git 功能介紹 - Git、GitHub 和 GitHub Desktop 的關係 - 參考資料 ### 版本控制介紹 #### 什麼是版本控制 控制文件在不同時候的內容,主要功能有**儲存當前版本**、**恢復到特定版本**、**合併不同版本** ... #### 什麼時候使用 在編輯任何事物時,想要參考或使用過去版本或不同版本 #### 為什麼使用版本控制 版本控制幫助我們更靈活操作檔案 1. 操作失誤時,可以恢復版本 2. 共同開發時,可以個別開發後合併版本 #### 哪裡有版本控制 以下有一些生活中版本控制的例子,並不侷限在軟體開發 1. Word 當機重啟,詢問是不是要恢復這個版本 2. 當你有個資料夾 ```MyDocument``` 的目錄結構如下, ``` MyDocument ├─ MyDocument-20220923.doc ├─ MyDocument-20220923-1.doc └─ MyDocument-20220924.doc ``` 3. 使用 30 顆高級球卻沒抓到神奇寶貝時,關掉重開 Gameboy 再抓一次 ### Git 簡介 #### 什麼是 Git Git 是一個**使用指令操作**的版本控制軟體 #### 那 Github 又是誰 Github 是一個**原始碼託管網站**,他使用 Git 作為他的版本控制工具,但是 Github 不只做版本控制一件事, 還可以讓我們架設靜態網站 Git Pages 等,在底下會更詳細描述他們之間的關係 #### 那 Github Desktop 和 Git 是什麼關係 Github Desktop 是 Git 的**圖形化介面客戶端**,可以更直覺使用 Git 的各種功能,但使用圖形化介面前, 更重要的是要了解 Git 能做什麼,才知道要怎麼用 Github Desktop ### Git 重要名詞介紹 在 Git 中有四個重要的區域概念,分別是工作目錄、暫存區、儲存庫和遠端儲存庫,這些區域的階層關係如下圖 | | | :-: | | ![git-glossary](https://vkmouse.github.io/img/post/Introduction_to_Git_Version_Control/git-glossary.svg) | | 圖1 Git 名詞介紹| #### 工作目錄 (Working Directory) - 描述: 要進行版本控制的空間 - 功能: 放置一個專案的最外層 (根目錄) - 位置: 位於個人電腦的資料夾,例如: ```C:/repositories/repo``` #### 暫存區 ([Staging Area](https://git-scm.com/about/staging-area)) - 描述: 一個提交到儲存庫之前,暫時存放的區域 - 功能: 可以在這個階段進行**審查** - 位置: 位於工作目錄的 .git 資料夾,例如: ```C:/repositories/repo/.git``` #### 儲存庫 (Repository) - 描述: 一個儲存各個版本的空間 - 功能: 可以從儲存庫調用版本,或上傳版本到儲存庫 - 位置: 位於工作目錄的 .git 資料夾,例如: ```C:/repositories/repo/.git``` #### 遠端儲存庫 (Remote Repository) - 描述: 一個遠端空間 - 功能: 用來儲放 .git 資料夾 - 位置 1. 可以在 GitHub 上,例如: ```https://github.com/USERNAME/repo.git``` 2. 可以在其他伺服器 (個人電腦) ### Git 功能介紹 利用[官方文件](https://git-scm.com/docs)的功能分類方式,描述其中三種類型的部分功能,包含**取得或新建儲存庫**、**基本操作**和**遠端操作** | | | :-: | | ![git-features](https://vkmouse.github.io/img/post/Introduction_to_Git_Version_Control/git-features.svg) | | 圖2 Git 功能介紹| #### 取得或新建儲存庫 1. 新增一個空白的儲存庫 ([git init](https://git-scm.com/docs/git-init)) 2. 下載遠端儲存庫到新的工作目錄 ([git clone](https://git-scm.com/docs/git-clone)) #### 基本操作 3. 新增**檔案的變更內容**到暫存區 ([git add](https://git-scm.com/docs/git-add)) 4. 提交**暫存區的變更內容**到儲存庫 ([git commit](https://git-scm.com/docs/git-commit)) 5. 顯示工作目錄、暫存區和儲存庫的**檔案路徑差異** ([git status](https://git-scm.com/docs/git-status)) 6. 顯示工作目錄、暫存區和儲存庫的**檔案內容差異** ([git diff](https://git-scm.com/docs/git-diff)) 7. 復原工作目錄的檔案 ([git restore](https://git-scm.com/docs/git-restore)) #### 遠端操作 8. 管理遠端儲存庫 ([git remote](https://git-scm.com/docs/git-remote)) 9. 上傳儲存庫到遠端儲存庫 ([git push](https://git-scm.com/docs/git-push)) 10. 下載遠端儲存庫到儲存庫並合併至工作目錄 ([git pull](https://git-scm.com/docs/git-pull)) ### Git、GitHub 和 GitHub Desktop 的關係 1. Git: 負責的事情是「版本控制」,**提交版本**、**恢復版本**或**比對版本差異**等,都是 Git 的任務。 2. GitHub: 一個「原始碼託管網站」,提供一個雲端空間儲存 Git 的儲存庫,並且**呈現版本差異**,除此之外還有很多功能 3. GitHub Desktop: 一個 Git 的「圖形化介面客戶端」,提供圖形化介面的**提交版本**、**恢復版本**或**比對版本差異** 版本控制 (Version control)、[原始碼託管網站 (Source code hosting website)](https://en.wikipedia.org/wiki/Comparison_of_source-code-hosting_facilities) 和[圖形化介面客戶端 (GUI client)](https://git-scm.com/downloads/guis/) 的關係如下圖。在託管網站中比較特別的是 Gitea,他是一個免費自架的原始碼託管網站,可以架在自己網域或是內網。在 GUI 中 GitKraken 是以介面美觀且功能完整而聞名。 | | | :-: | | ![relationship](https://vkmouse.github.io/img/post/Introduction_to_Git_Version_Control/relationship.svg) | | 圖3 Git、GitHub 和 GitHub關係圖| ### 參考資料 [About remote repositories](https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories) [Comparison of source-code-hosting facilities](https://en.wikipedia.org/wiki/Comparison_of_source-code-hosting_facilities) [Git Documentation](https://git-scm.com/doc) [Git-GUI Clients](https://git-scm.com/downloads/guis/) [工作區、暫存區與儲存庫](https://gitbook.tw/chapters/using-git/working-staging-and-repository)
git