---
title: "04 - GitHub 入門"
type: note
specialty: Programming
tags: [git-github, 04-github入門]
---

# 04 - GitHub 入門

← [[03-基本工作流程]] | 下一篇 → [[05-Push與Pull]]

---

## 📝 建立 GitHub 帳號

1. 前往 [github.com](https://github.com)
2. 點右上角 **Sign up**
3. 填入 Email、密碼、使用者名稱
4. 驗證 Email

---

## 🆕 建立一個新的 Repository（repo）

**Repository（倉庫）** 是 GitHub 上存放你的專案的地方。

### 步驟

1. 登入後，點右上角的 **+** → **New repository**
2. 填入設定：

| 欄位 | 說明 | 建議 |
|------|------|------|
| **Repository name** | repo 的名稱 | 用英文，不要有空格，可以用 `-` 連接 |
| **Description** | 描述（可選） | 簡短說明這個 repo 是什麼 |
| **Public / Private** | 公開或私人 | 個人筆記建議選 **Private** |
| **Initialize with README** | 自動建立 README | 如果本地還沒有 repo，可以勾 |
| **.gitignore template** | 忽略清單模板 | 依你的語言選 |

3. 按 **Create repository**

---

## 🔗 連接本地 repo 到 GitHub

假設你已經在本地用 `git init` 建立了 repo，現在要連接到 GitHub：

### 使用 HTTPS（較簡單，但每次需要輸入密碼或 token）

```bash
# 加入遠端 repo 的網址
git remote add origin https://github.com/username/repo-name.git

# 確認設定成功
git remote -v
```

### 使用 SSH（推薦，設定一次後不需要再輸入密碼）

```bash
# 加入遠端 repo 的 SSH 網址
git remote add origin git@github.com:username/repo-name.git

# 確認設定成功
git remote -v
```

輸出範例：

```
origin  git@github.com:dirtywolf1213/Obsidian-med-note.git (fetch)
origin  git@github.com:dirtywolf1213/Obsidian-med-note.git (push)
```

> `origin` 是遠端 repo 的別名（nickname），可以自己取名，但習慣上都叫 `origin`

---

## 📂 複製別人的 repo（git clone）

如果你想把 GitHub 上的 repo 下載到本地：

```bash
# 使用 HTTPS 複製
git clone https://github.com/username/repo-name.git

# 使用 SSH 複製（需要先設定 SSH key）
git clone git@github.com:username/repo-name.git

# 複製到指定資料夾
git clone https://github.com/username/repo-name.git my-folder
```

`git clone` 會自動：
- 下載完整的 repo（包含所有歷史）
- 設定好 remote（自動叫做 `origin`）

---

## 🔑 GitHub 身份驗證

### 方法一：Personal Access Token（HTTPS 用）

GitHub 在 2021 年後不再接受密碼，需要用 Token：

1. GitHub → Settings → **Developer settings** → **Personal access tokens** → **Tokens (classic)**
2. 點 **Generate new token**
3. 選擇有效期限和權限（至少要勾 `repo`）
4. 生成後立刻複製！（只會顯示一次）
5. 往後 push 時，密碼欄位輸入這個 token

### 方法二：SSH Key（推薦）

見 [[02-安裝與初始設定]] 的 SSH Key 設定章節。

---

## 🌐 GitHub 介面重要功能

| 功能 | 說明 |
|------|------|
| **Code** | 瀏覽 repo 的檔案 |
| **Commits** | 查看所有 commit 歷史 |
| **Issues** | 追蹤問題、待辦事項 |
| **Pull Requests** | 多人協作的合併請求 |
| **Actions** | 自動化工作流程（CI/CD） |
| **Settings** | repo 設定（可設為 Private） |

---

## 📌 小結

| 指令 | 用途 |
|------|------|
| `git remote add origin [URL]` | 連接遠端 repo |
| `git remote -v` | 查看遠端設定 |
| `git clone [URL]` | 下載 GitHub 上的 repo |
| `git remote remove origin` | 移除遠端連接 |

---

← [[03-基本工作流程]] | 下一篇 → [[05-Push與Pull]]
