---
title: "08 - Git 指令速查表（Cheat Sheet）"
type: note
specialty: Programming
tags: [git-github, 08-指令速查表]
---

# 08 - Git 指令速查表（Cheat Sheet）

← [[07-常見問題與解法]] | 回到 → [[00-Index]]

---

## 🔧 設定（一次性）

```bash
git config --global user.name "你的名字"
git config --global user.email "email@example.com"
git config --global init.defaultBranch main
git config --global --list           # 查看所有設定
```

---

## 📁 初始化與克隆

```bash
git init                             # 初始化新 repo
git clone [URL]                      # 克隆遠端 repo
git clone [URL] [資料夾名]            # 克隆到指定資料夾
```

---

## 📋 狀態與紀錄

```bash
git status                           # 查看目前狀態
git log                              # 查看完整歷史
git log --oneline                    # 每個 commit 一行
git log --oneline --graph --all      # 圖形化顯示
git diff                             # 查看未暫存的修改
git diff --staged                    # 查看已暫存的修改
git show [commit hash]               # 查看某個 commit 的內容
```

---

## ➕ 暫存（Staging）

```bash
git add [檔案]                       # 加入單一檔案
git add .                            # 加入所有修改
git add -p                           # 互動式選擇
git restore --staged [檔案]          # 從暫存區移除
git restore --staged .               # 清空暫存區
```

---

## 💾 提交（Commit）

```bash
git commit -m "訊息"                 # 提交並附說明
git commit --amend -m "新訊息"       # 修改最後一次 commit
git reset --soft HEAD~1              # 撤銷 commit（保留修改）
git reset --hard HEAD~1              # 撤銷 commit（丟棄修改）⚠️
```

---

## ↩️ 撤銷修改

```bash
git restore [檔案]                   # 撤銷工作區修改 ⚠️
git revert [commit hash]             # 安全地撤銷某個 commit
```

---

## 🌿 分支（Branch）

```bash
git branch                           # 查看本地分支
git branch -a                        # 查看所有分支（含遠端）
git switch -c [分支名]               # 建立並切換新分支
git switch [分支名]                  # 切換分支
git branch -d [分支名]               # 刪除分支（已合併）
git branch -D [分支名]               # 強制刪除分支
```

---

## 🔗 合併與 Rebase

```bash
git merge [分支名]                   # 合併分支
git merge --no-ff [分支名]           # 強制建立 merge commit
git rebase [分支名]                  # Rebase（進階）
git rebase --abort                   # 中止 rebase
```

---

## 🌐 遠端（Remote）

```bash
git remote add origin [URL]          # 加入遠端 repo
git remote -v                        # 查看遠端設定
git remote set-url origin [新URL]    # 更新遠端 URL
git remote remove origin             # 移除遠端

git fetch                            # 抓取遠端更新（不合併）
git pull                             # 抓取並合併
git push -u origin main              # 第一次 push
git push                             # 之後的 push
git push --force                     # 強制覆蓋遠端 ⚠️
git push origin --delete [分支名]    # 刪除遠端分支
```

---

## 🔖 標籤（Tag）

```bash
git tag                              # 列出所有 tag
git tag v1.0                         # 建立輕量 tag
git tag -a v1.0 -m "版本說明"        # 建立有說明的 tag
git push origin --tags               # Push 所有 tag
```

---

## 🏷️ Stash（暫存抽屜）

```bash
git stash                            # 暫時收起現在的修改
git stash pop                        # 取回最後一次 stash
git stash list                       # 查看所有 stash
git stash drop                       # 刪除最後一次 stash
```

---

## 💡 常用工作流程一覽

### 日常更新筆記

```bash
git add .
git commit -m "更新 [主題] 筆記"
git push
```

### 多台電腦同步

```bash
# 開始前
git pull

# 結束後
git add .
git commit -m "..."
git push
```

### 建立新功能分支

```bash
git switch -c feature/new-topic
# ... 做修改 ...
git add .
git commit -m "..."
git switch main
git merge feature/new-topic
git push
```

---

← [[07-常見問題與解法]] | 回到 → [[00-Index]]
