> For the complete documentation index, see [llms.txt](https://docs.xiwind-corp.com/tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.xiwind-corp.com/tech/git/git-kai-shi-shi-yong.md).

# Git 開始使用

## Git

***

Git 是一個版本控制系統(VCS)，被廣泛用於軟體發展專案。它是由 Linus Torvalds 在2005年創建的，作為他用來管理 Linux kernel 開發的專有 BitKeeper 工具的替代。

有了 Git，開發者可以追蹤他們的程式碼隨時間的變化，與他人合作，並管理他們程式碼的多個版本。Git採用分散式模型，這意味著每個開發者在他們的本地電腦上都有自己的程式碼副本，可以在本地進行修改和提交，而不需要網路連接。這些修改可以與其他開發者的修改合併。

Git 提供強大的分支和合併功能，允許開發者同時進行多個功能或錯誤修復，完成後再將它們合併到一起。這有助於減少衝突，確保程式碼在整個開發過程中保持穩定和功能。

總結，Git是一個強大而靈活的工具，已經成為現代軟體發展的一個重要組成部分。它的易用性、可靠性和廣泛的功能使它成為軟體行業版本控制的事實上的標準。

<figure><img src="/files/xQDZmscnC4l5sfr1N2qN" alt="Git 版本控制系統"><figcaption><p>Git 版本控制系統</p></figcaption></figure>

1. **下載並安裝** [Git](https://git-scm.com/)

<div><figure><img src="/files/sGFvueXEbzTr8UkldekU" alt="步驟 1" width="375"><figcaption><p>步驟 1</p></figcaption></figure> <figure><img src="/files/PLFa2BU4g6E63v2dg5vx" alt="步驟 2" width="375"><figcaption><p>步驟 2</p></figcaption></figure></div>

### .gitconfig 設定

1. **可以透過以下命令查看 .gitconfig 所有的配置以及它們所在地。**

{% code title="SHELL" overflow="wrap" %}

```powershell
git config --list --show-origin
```

{% endcode %}

<figure><img src="/files/Hs2sAcq1k3HXeOkdPYAD" alt="查看 .gitconfig" width="375"><figcaption><p><strong>查看 .gitconfig</strong></p></figcaption></figure>

2. **安裝完 Git 之後，第一件事就是設定你的使用者名稱和電子信箱。**(通常使用 GitHub 個人名稱, GitHub 的通知信箱)

{% code title="SHELL" overflow="wrap" %}

```powershell
git config --global user.name <使用者名稱>
git config --global user.email <電子信箱>
```

{% endcode %}

<figure><img src="/files/PH3Yv2ACaGKtrzeevM82" alt="設定使用者名稱和電子信箱" width="375"><figcaption><p><strong>設定使用者名稱和電子信箱</strong></p></figcaption></figure>

## Repository

***

在軟體開發的上下文中，Repository(存儲庫) 或 Repo 通常指的是開發人員可以存儲和管理其程式碼庫的集中位置。 在協作環境中，多個人可能在同一個項目上工作並且需要協調他們的工作。

GitHub 是一個基於 Web 的平台，它為使用 Git 的軟體開發和版本控制提供託管服務。 它允許開發人員存儲他們的程式碼存儲庫並與世界各地的其他開發人員協作。 GitHub 已成為許多開源項目的熱門選擇，因為它為貢獻者提供了一個集中位置來協作和管理程式碼更改。 除了程式碼託管和協作功能外，GitHub 還提供問題追蹤、項目管理工具和文件託管。

<figure><img src="/files/4afark8v1lQdRDTPvdcv" alt="GitHub repository" width="563"><figcaption><p>GitHub repository</p></figcaption></figure>

### 建立 Remote Repository

***

1. **登入**[**GitHub**](https://github.com/)**、選擇 New Repository。**

<figure><img src="/files/IF7iE6CTgMMazVeM9aI7" alt="New Repository" width="375"><figcaption><p><strong>New Repository</strong></p></figcaption></figure>

2. **填寫 Remote Repository 資料。**&#x20;

<figure><img src="/files/lQLBeAtYrNrpZz8tX4aQ" alt="填寫 Remote Repository 資料" width="375"><figcaption><p><strong>填寫 Remote Repository 資料</strong></p></figcaption></figure>

* **Repository name**: Repository 的名稱。
* **Description**: 簡短此 Repository 描述。
* **Public, Private**：此 Repository 是否公開。
* **Add a README file**: 於 Repository 首頁顯示第一個頁面，敘述此 Repository ，支援Markdown 語法。打勾時會生成一個檔案。
* **Add .gitignore**：是否不跟蹤特定檔案。打勾時會生成一個檔案。
* **Choose a license**：在 Public 的時候，此 Repository 的授權許可。可以選擇MIT。選擇時會生成一個檔案。

3. **建立完後的 Repository。**&#x20;

<figure><img src="/files/Y54XTX5sb3lPqSm375HC" alt="建立完後的 Repository" width="375"><figcaption><p><strong>建立完後的 Repository</strong></p></figcaption></figure>

### Repository 儲存到電腦

1. **選擇一個本地路徑和一個 Repository 的網址，然後執行以下命令，把 Repository 複製到電腦。**

{% code title="SHELL" overflow="wrap" %}

```powershell
cd 'C:\Users\AAA\Desktop\新增資料夾'
git clone https://github.com/tuhlnaa/111111.git
```

{% endcode %}

<div><figure><img src="/files/ebUlmw21u64r2wpUz6za" alt="複製一個 Repository 的網址" width="375"><figcaption><p>複製一個 Repository 的網址</p></figcaption></figure> <figure><img src="/files/RmIQ3MassMp0fHpxTgEM" alt="執行命令" width="375"><figcaption><p>執行命令</p></figcaption></figure></div>

<details>

<summary>每一個 Repository 都會有一個 .git 資料夾，負責做 Git 版本控制。</summary>

<img src="/files/aS7MuodCVPRSlcDgcb2w" alt="" data-size="original">

</details>

### 提交本地 Repository

***

1. **建立本地專案。** (這裡使用 Visual Studio 2022)

<div><figure><img src="/files/dCGF4NfrMnoCkGi6p2p3" alt="使用 Visual Studio 2022 建立程式專案"><figcaption><p>使用 Visual Studio 2022 建立程式專案</p></figcaption></figure> <figure><img src="/files/1QIfOofQDe4DWFhiQyzc" alt="選擇本地 Repository 路徑"><figcaption><p>選擇本地 Repository 路徑</p></figcaption></figure></div>

2. **確認本地 Repository 正常運作。** 其中 main 代表主要 branch (分支)，資料夾含有Git版本控制。

{% code title="SHELL" overflow="wrap" %}

```powershell
cd C:\Users\AAA\source\repos\111111
```

{% endcode %}

<figure><img src="/files/W2TjoPkxfX2Nn9iFhFE1" alt="" width="375"><figcaption></figcaption></figure>

3. **顯示本地 Repository 當前狀態。**\
   **狀態有**：Untracked(未追蹤的新檔案)、be Commited(需提交)、 not Staged(未進入整備區)...。

{% code title="SHELL" overflow="wrap" %}

```powershell
git status
```

{% endcode %}

<figure><img src="/files/Xchp7t0EmYhDvaRILTMK" alt="" width="375"><figcaption></figcaption></figure>

4. **將 Untracked(未追蹤的新檔案) 和 Tracked(已追蹤的舊檔案) 加入到 Staging area (整備區)進行追蹤。** (可以指定單個檔、目錄、或者使用 `.` 追蹤所有更改。) `?`: 代表 Untracked，`+`: 代表新增(Tracked)，`~`: 代表修改(Tracked)。

{% code title="SHELL" overflow="wrap" %}

```powershell
git add .
git add <路徑>
```

{% endcode %}

<figure><img src="/files/xuZwVVWX94EWEkbBU0c9" alt="" width="375"><figcaption></figcaption></figure>

5. **顯示本地 Repository Staging area 當前狀態。**\
   **狀態有**：Untracked(未追蹤的新檔案)、be Commited(需提交)、 not Staged(未進入整備區)、Publish(待發布 push)...。

{% code title="SHELL" overflow="wrap" %}

```powershell
git status
```

{% endcode %}

<figure><img src="/files/NQPkVj2ixJQSkqs1B8av" alt="" width="375"><figcaption></figcaption></figure>

6. **將Staging area commit (提交)到本地 Repository。** (必須使用 `-m` 提供一個 commit 資訊，描述所做的修改。)

{% code title="SHELL" overflow="wrap" %}

```powershell
git commit -m 'commit 資訊描述'
```

{% endcode %}

{% hint style="success" %}
**提供確切的 commit 資訊描述，有助於專案的審查人員或是開發人員。**
{% endhint %}

<figure><img src="/files/fvVE7tszvpHkVmnJjwaS" alt="" width="375"><figcaption></figcaption></figure>

7. **將Commit Push(推送)到Remote Repository。** (如果回到GitHub網頁，就會看到在電腦上的專案。)

{% code title="SHELL" overflow="wrap" %}

```powershell
git push
```

{% endcode %}

<figure><img src="/files/GMn45MwHhO9qTU6BEEeT" alt="" width="375"><figcaption></figcaption></figure>

<div><figure><img src="/files/tVDWNn9YaHlRLrPtrD9f" alt="" width="375"><figcaption><p>Remote Repository</p></figcaption></figure> <figure><img src="/files/JJ0pv78q3xmxs14WBqRk" alt="" width="375"><figcaption><p>commit 資訊</p></figcaption></figure></div>

### 建立本地 Repository

***

**建立本地 Repository。** (在此路徑下，不使用 Github 建立)

{% code title="SHELL" overflow="wrap" %}

```powershell
git init
```

{% endcode %}

<div><figure><img src="/files/yTjJlM9xYCUtM2eBYVj1" alt="" width="375"><figcaption><p>git init</p></figcaption></figure> <figure><img src="/files/LFPUe5KxuYO21zpkmbDP" alt="" width="375"><figcaption><p>本地 Repository</p></figcaption></figure></div>

## Visual Studio - .vs 資料夾

***

.vs 資料夾是一個隱藏資料夾，在您打開專案或解決方案時由 Visual Studio 創建。 包括特定於解決方案的設置、調試訊息、 IntelliSense 配置和 metadata。 .vs 資料夾中包含文件因 Visual Studio 版本和您正在處理的項目類型而有所不同。

.vs 資料夾不是您實際原始程式碼的一部分，您通常不應將 .vs 資料夾包含在 Repository 版本控制系統中。 事實上，GitHub 通常會將 .vs 資料夾包含在忽略的文件列表中，正是因為沒有必要。

### 從 Git 中排除 '.vs' 文件夾

1. **將以下命令加入到 .gitignore 文件中，以排除 .vs 資料夾。**

{% code title="TEXT" overflow="wrap" %}

```
# Visual Studio project
.vs/
```

{% endcode %}

<figure><img src="/files/tKNlyl6QgD4WOXIo79SR" alt="" width="375"><figcaption></figcaption></figure>

2. **刪除 .vs 資料夾。**

<figure><img src="/files/KBHbf0kpMGUEAKvF7PGX" alt="" width="375"><figcaption></figcaption></figure>

## 參考資料

***

[Git - 初次設定 Git](https://git-scm.com/book/zh-tw/v2/%E9%96%8B%E5%A7%8B-%E5%88%9D%E6%AC%A1%E8%A8%AD%E5%AE%9A-Git)

[On undoing, fixing, or removing commits in git](https://sethrobertson.github.io/GitFixUm/fixup.html#committedp)

[Git Clone and Push ERROR: Repository not found | by Krishna kushwaha | Medium](https://kushwaha03.medium.com/git-clone-and-push-error-repository-not-found-c004c7c14526)

[Git 與 GitHub - 揭開差異之謎 | 埃杜雷卡](https://www.edureka.co/blog/git-vs-github/)
