Git 路線圖Git Roadmap單元 06Unit 06
平行開發Parallel development

使用 branch 開發Develop with branches

UNIT 06 · BRANCH WORKFLOW

使用 branch 開發Develop with branches

建立一條安全支線,在不改動 main 的情況下完成新功能。Create a safe branch and finish a feature without changing main.

預計時間Estimated time
20–25 分鐘20–25 minutes
單元類型Unit format
教材+模擬器Lesson + simulator
需要先會Prerequisite
會 add 與 commitKnow add and commit

branch 很輕,它只是指向 commit 的標籤

建立 branch 不會複製整個資料夾。git switch -c feature/login 會在目前 commit 建立新標籤,並讓 HEAD 改為指向它。

git branch
git switch -c feature/login
git add .
git commit -m "新增登入畫面"
git switch main
git merge feature/login
git branch -d feature/login

在 feature branch 建立 commit 時,只有該 branch 標籤向前;main 仍停在原處。這就是「不影響穩定主線」的核心。

git branch -d 是安全刪除:若 branch 還有未合併 commit,Git 會拒絕。刪除 branch 標籤不代表立刻刪除已合併的 commit。

A branch is lightweight: it is only a label pointing to a commit

Creating a branch does not copy the entire folder. git switch -c feature/login adds a label at the current commit and points HEAD to it.

git branch
git switch -c feature/login
git add .
git commit -m "Add login screen"
git switch main
git merge feature/login
git branch -d feature/login

When you commit on the feature branch, only that label moves forward; main stays where it was. That is the core of working without disturbing the stable line.

git branch -d is the safe delete. Git refuses when unmerged commits remain. Deleting a branch label does not immediately delete an already merged commit.

INTERACTIVE LAB

在安全模擬器裡親手操作

輸入指令後,Terminal、四個區域與 Git 樹會從同一份虛擬狀態同步更新。

A

WORKING DIRECTORY

正在編輯的檔案

  • README.md已提交
  • app.js已提交
  • nav.js已提交
B

STAGING AREA

下一版準備內容

  • Staging Area 是空的
C

LOCAL REPOSITORY

電腦裡的版本

HEAD
→ main
目前 commit
c2
commit 數
2
D

REMOTE REPOSITORY

GitHub 示意狀態

origin
尚未設定
遠端 branch
沒有 commit
網路連線
僅記憶體模擬

教學 Terminal

不會執行真實 Git 或 shell

教學模擬器已就緒。這裡不會執行真實 Git 或讀寫你的檔案。

點一下填入:

VIRTUAL FILES

虛擬檔案編輯器

更多情境工具

LIVE GIT TREE

commit 與 branch

HEAD → main
  1. c2
    HEAD → main
    加入導覽列parent:c1
  2. c1
    建立專案parent:無(第一個 commit)

CHECKPOINT

離開前,確認你能用自己的話說明Before you leave, explain it in your own words

  1. 這些指令改變 Git 的哪一個區域?
  2. Git 樹上的 branch 與 HEAD 移到哪裡?
  3. 失敗時 repository 是否保持原狀?
  4. 真實專案操作前,應先查看什麼?
  1. Which Git area does this command change?
  2. Where do the branch and HEAD move in the Git tree?
  3. Does the repository stay unchanged when the command fails?
  4. What should you check before operating on a real project?

免責聲明:本站內容為個人學習、實作與經驗分享紀錄,部分內容透過 AI 協助修正與整理。由於每位使用者的系統環境、軟體版本、權限設定與操作方式不同,實際結果可能有所差異。請在操作前自行評估風險並備份重要資料。因參考本站內容所造成之任何系統異常、資料遺失、設定錯誤、設備故障、服務中斷或其他直接、間接損失,均由使用者自行承擔,本站與作者不負任何相關責任。

Disclaimer: This site records personal learning, implementation, and experience sharing, with some content edited and organised with AI assistance. Results may differ because every user has a different system environment, software version, permission setup, and workflow. Assess risks and back up important data before trying anything. You are responsible for any system errors, data loss, configuration mistakes, equipment faults, service interruptions, or other direct or indirect losses resulting from using this content.