> For the complete documentation index, see [llms.txt](https://acezxn.gitbook.io/vex-ji-qi-ren-cheng-shi-jiao-xue/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://acezxn.gitbook.io/vex-ji-qi-ren-cheng-shi-jiao-xue/jin-jie-jiao-xue/git-ban-ben-guan-li.md).

# git 版本管理

每位程式設計師都該會的版本管理架構

git 是一個開源架構，可以記錄一個專案在開發時的各個版本。結合Github，可以達到備份版本、多人開發、版本分枝並融合的功能。

## Repository

Repository的概念就像是一個專案，會隨著開發不斷更新。用git管理的repository可以進行十分有用的版本控制。

## Github

Github是一個git服務的提供者。可以在Github網站上創造並上傳repository，達到備份版本、多人開發等的功能。

## 使用 Git 大致上的工作流程

<figure><img src="https://www.getdbt.com/ui/img/guides/analytics-engineering/git-workflow-1.png" alt="" width="563"><figcaption><p>Image referenced from <a href="https://www.getdbt.com/analytics-engineering/transformation/git-workflow/">https://www.getdbt.com/analytics-engineering/transformation/git-workflow/</a></p></figcaption></figure>

## 連接專題到Github

創造新的repository：

```bash
git init
```

載入更新：

```bash
git add [files]

# add all files in the project
git add *
```

確認更新：

```bash
git commit -m "commit message"
```

讓git repository與Github連結（只有第一次連接時需要）：

```bash
git remote add origin [github repository link]
```

創造主分枝（只有第一次連接時需要）：

```bash
git branch -M main
```

上傳更新到所在的分枝（目前是主分枝）：

```bash
git push origin
```

複製github上的repository：

```bash
git clone [github repository link]
```

下載更新：

```bash
git pull
```

## Merge

如果是一個人開發，那一切會簡單很多。但是如果有兩人開發，情況就會變得複雜。假如Ａ在他更動的部分想下載更新，但是Github上不但沒有他更動的部分，還有Ｂ更動的部分，那會怎麼樣？這時就需要融合Ａ的版本和Github上的版本。融合的過程通常是自動完成的，但是如果有一個檔案同時被兩人改動，就會有衝突（Conflict），需要人決定哪些更動要保留，哪些要捨棄。

## Branches

這個功能可以在專案開啟版本分枝，也可以合併分枝。 :warning: 注意：不同分枝可能會有不同的相容性。

顯示git狀態：

```bash
git status
```

創造新的Branch：

```bash
git checkout -b [branch name]
```

切換Branch：

```bash
git checkout [branch name]
```

Merge Branch：

```bash
git merge [destination branch] [source branch]
```
