2025-08-10 Git 練習

2025-08-10 Git 練習

下面我會給你 一份「接案工程師必備 Git 實戰循序練習地圖」
讓你從最常發生的狀況開始練、每一個狀況都有原因、指令、練習方式。

你只要一步一步照著做,練完後就能處理案子上 90% 的 Git 問題。


🧭 接案必備 Git 練習順序(從最常遇到 → 進階)

我會照 你實際在接案時最常遇到的情境 排序:
1️⃣ 基本功能 Flow → 2️⃣ 多人協作 Flow → 3️⃣ 錯誤修復 Flow → 4️⃣ 大型案件 Flow

每一項我會跟你說 為什麼會遇到、要練什麼、要怎麼練


Level 1:個人開發一定會遇到的 Git 流程

① 初始化本地專案

🔹 原因: 不管新案子、Side Project、測試,都要初始化
🔹 要學指令:

1
2
3
git init
git add .
git commit -m "initial commit"

🔹 練習:

  • 新建一個資料夾 practice-git1
  • 寫個 index.html
  • 做一次 commit

② 連接 GitHub + push

🔹 原因: 你接案一定要把程式上傳給客戶
🔹 要學指令:

1
2
3
git remote add origin <repo-url>
git branch -M main
git push -u origin main

🔹 練習:

  • 在 GitHub 建 repo
  • push 上去

③ 修改檔案 → commit → push

🔹 原因: 每天的基本工作就是這個
🔹 要學:

1
2
3
4
git status
git add <file>
git commit -m ""
git push

Level 2:接案一定會遇到的多人協作衝突 & 流程

④ 從 GitHub 拉別人寫的 code

🔹 原因: 專案通常不是你一個人寫
🔹 指令:

1
git pull

🔹 練習:

  • 用你另一台電腦,改同一個檔案
  • 在本機 pull

⑤ pull 會衝突(最常發生)

🔹 原因:
你改了 A 檔案、別人也改了 A 檔案 → 就衝突。

🔹 要練:

  • 怎麼看衝突 <<<<<<< HEAD
  • 怎麼解決
  • 怎麼重新 commit

🔹 練習方法:

  1. 你在 main 分支修改 index.html
  2. 你在另一台電腦 or GitHub 上修改 index.html
  3. 回本機 pull
    → 一定會衝突
    → 解決後 commit + push

⑥ 分支開發(接案必備)

🔹 原因: 你做功能要開 branch,不然 main 被你弄壞會出事
🔹 要學指令:

1
2
3
4
git branch feature/login
git checkout feature/login
// or
git switch -c feature/login

🔹 練習:

  • 建一個 feature/login
  • 改點東西
  • commit

⑦ merge 分支(超常見)

🔹 原因: 功能做完要 merge 回 main
🔹 指令:

1
2
git switch main
git merge feature/login

🔹 練習:

  • 在 feature/login 修改
  • merge 回 main
  • 看看會不會衝突(通常會)

⑧ rebase(接案時 PM 或 Lead 說要整理 commit)

🔹 原因: merge 會有複雑 history
rebase 可以變乾淨

🔹 指令:

1
2
git switch feature/login
git rebase main

🔹 練習:

  • 同樣製造衝突
  • 解 conflict
  • 儲存 rebase

Level 3:接案常遇到的失誤 & 錯誤修復

⑨ commit 寫錯訊息或漏上傳檔案

🔹 指令:

1
git commit --amend

🔹 練習:

  • 故意 commit 錯訊息
  • 用 amend 修正

⑩ 不小心 checkout 到別的 branch 修改(接案超常發生)

🔹 指令:

1
2
3
git stash
git switch 正確分支
git stash pop

🔹 練習:

  • 在 main 修改
  • 發現你其實應該改 feature/login
  • 把修改移過去

⑪ 想丟掉本地修改

🔹 指令:

1
git reset --hard

⑫ 想恢復不小心刪掉的 commit

🔹 指令:

1
2
git reflog
git reset --hard <commit-id>

Level 4:接案時 Lead 或公司流程會要求的

⑬ 開 Pull Request(PR)

🔹 原因: Code Review 是業界標準
🔹 練習事項:

  • 開 branch → push → GitHub 開 PR

⑭ review 後被要求修改

🔹 練習事項:

  • 修改程式碼再 push,PR 自動更新

⑮ release / tag

🔹 原因: 常要給客戶「版本」
🔹 指令:

1
2
git tag v1.0
git push origin v1.0

📘 我建議的練習順序(照這樣學一定會上手)

階段 練習內容 目的
Lv1 init → commit → push → pull 一般個人開發
Lv2 branch → merge → conflict → rebase 多人協作
Lv3 stash → amend → reset → reflog 錯誤修復
Lv4 PR → tag → release 真實接案流程

📌 下一步:

你要我現在從 Level 1 開始一步一步教你操作嗎?
還是你已經知道 Level 1,可以從 分支 / 衝突(Level 2) 開始?
好!我們從 Level 1(個人開發) 現場演練開始 — 每一步都會有「為什麼」、「要打的指令」、「你會看到的結果(如何驗證)」、「練習題目(3 題)」,並附上小測驗 3 題確認理解。照著做就行,不用我再問你問題(除非你要我改變步調)。

🚀 Level 1 — 基本流程一步步做

前提:你已經在電腦裝好 Git(可用 git --version 檢查)。
若還沒:請先安裝 Git(mac 用 Homebrew:brew install git;Windows 用 Git for Windows 安裝程式)。


1. 設定使用者名稱與 Email(第一次必做)

為什麼:每個 commit 都會記錄作者資訊。
指令

1
2
git config --global user.name "你的名字"
git config --global user.email "you@example.com"

驗證

1
git config --global --list

應該會看到剛剛設定的 name 與 email。


2. 建新資料夾並 git init

為什麼:開始一個本地專案並啟用版本控制。
指令

1
2
3
mkdir practice-git1
cd practice-git1
git init

你會看到Initialized empty Git repository ...(或 Reinitialized existing Git repository
驗證

1
2
ls -la .git
git status

git status 應顯示 On branch main(或 master),且 no commits yet。


3. 新增檔案、檢查狀態、第一次 commit

為什麼:學會追蹤檔案與建立快照。
步驟與指令

1
2
3
4
5
echo "<!doctype html><html><body><h1>Hello Git</h1></body></html>" > index.html
git status
git add index.html
git status
git commit -m "Initial commit: add index.html"

驗證

  • git statusgit add 之前會把 index.html 列為 untracked
  • git commitgit status 顯示 nothing to commit, working tree clean
  • git log --oneline 應顯示剛剛的 commit 一行。

4. 修改檔案、再 commit

為什麼:學會基本工作流程:改 → 加入暫存 → commit。
指令

1
2
3
4
5
6
# 編輯 index.html(或使用 echo 追加)
echo "<!-- updated -->" >> index.html
git status
git add index.html
git commit -m "Update: add html comment"
git log --oneline

驗證git log --oneline 會有兩個 commit(最新在上)。


5. 連接遠端(GitHub)並 push(上傳)

為什麼:把本地專案備份到遠端,供你或客戶下載/部署。
步驟

  1. 在 GitHub 建一個新 repo(例如 practice-git1),不選 README。
  2. 回到 terminal:
1
2
3
git remote add origin https://github.com/你的帳號/practice-git1.git
git branch -M main # 如果你的預設分支不是 main 可執行
git push -u origin main

驗證:在 GitHub Repo 頁面可以看到檔案。git remote -v 可檢查遠端 URL。

小提醒:若 GitHub 要求你用 PAT(personal access token)作密碼,輸入 token 當密碼即可;或設定 SSH keys(進階)。


6. 常用檢查指令(隨時用)

  • git status:看目前工作區/暫存區狀態
  • git log --oneline --graph --decorate:看歷史樹狀概覽
  • git diff:看未 staged 的變更
  • git diff --staged:看已 staged 的變更

🧪 Level 1 練習題(做完這些你就能處理 90% 個人開發情境)

練習 1 — 從零到上傳

  1. 新建資料夾 practice-git-level1
  2. git init → 新增一個 readme.md → commit → 在 GitHub 建 repo → push 到遠端。

練習 2 — 多次 commit 練習

  1. 在專案中新增三個不同檔案(例如 a.txtb.txtc.txt),每個檔案各做一次修改並 commit(共 3 次 commit)。
  2. git log --oneline 檢查有 3 個 commit(或更多,包含 initial)。

練習 3 — 查看差異與回到乾淨狀態

  1. 修改 a.txt 但不要 git add,用 git diff 看變更。
  2. git add a.txtgit commit -m "edit a"
  3. 再修改 a.txt,用 git reset --hard 丟掉未 commit 的變更(注意:會刪除未儲存的修改)。

✅ 小測驗(3 題,回答簡短)

  1. git addgit commit 的差別是什麼?
  2. git status 有哪些常見輸出(至少說三種)?
  3. 如果你把檔案改了但不小心切換 branch,你想保留改動該怎麼做?

(你可以把答案直接貼上來,我會幫你檢查)


常見小陷阱(提醒)

  • 忘了 git config user.email → commit 會用空白或錯誤作者。
  • push 到遠端時若分支不一致(例如遠端沒有 main),第一次要用 -u origin main
  • git reset --hard 非常危險,會永久丟棄未 commit 的修改(本地)。小心使用。

若你準備好了,我就一步步帶你「實作」第一個練習(我會列出你要貼進 Terminal 的指令,你照著貼並把輸出複製回來給我,我會幫你檢查)。要我現在直接給第一個練習的完整命令序列嗎?