第 12 章 · 後端網站開發工作流程

1. 後端開發與 IDE

1.1 後端開發概述

後端負責應用程式的核心邏輯、資料庫互動與伺服器維護。要提升開發效率,整合開發環境(IDE) 是最重要的工具之一:它涵蓋程式編輯、除錯與專案管理。

1.2 IDE 的作用

IDE 用於撰寫、除錯與測試程式,提供自動補全、語法高亮、內建版本控制等功能,讓開發者專注於業務邏輯;現代 IDE 多半還支援多語言與框架。

1.3 熱門後端 IDE 比較

IDE 定位 主要特點 適合場景
VS Code 微軟 2015 推出的輕量、跨平台開源編輯器 擴展豐富、內建 Git 與終端、除錯強大 JS/Node.js 等;初學到資深皆宜
IntelliJ IDEA JetBrains,主打 Java/Kotlin 智能補全、Spring 等框架支援、重構強 企業級 Java、大型專案
PyCharm JetBrains,主打 Python Python 專屬支援、Django/Flask 整合、資料庫工具、科學運算 Python 後端、資料分析
Eclipse 老牌開源(IBM 起家) 多語言、插件生態大、大型專案穩定 企業/傳統 Java 專案
NetBeans Oracle 支持、開源 Java 支援完整、介面簡潔、跨平台 教學與中小型 Java 專案

雲端 IDE:如 GitHub Codespaces、AWS Cloud9,直接在瀏覽器開發,免本地設定、利於團隊協作。

1.4 選擇 IDE 的考量

語言/框架支援、編譯與除錯功能、插件擴展性、效能與資源佔用、社群支援。全端開發者常選 VS Code;專注 Java 的團隊多選 IntelliJ IDEA。

1.5 未來趨勢

IDE 朝 自動化、雲端化、協作化 發展;開發者宜持續關注新工具與技術。

2. Git 版本控制

2.1 版本控制概念

2.2 Git 簡介

Git 是 分散式 版本控制系統(由 Linus Torvalds 於 2005 年開發)。每個克隆都是一份完整倉庫,離線也能操作;相較集中式(SVN)更快、分支更輕量。

2.3 安裝 Git

依作業系統從官網安裝(Windows/macOS/Linux),安裝後檢查:

git --version

2.4 Git 配置

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --global core.editor "vim"

~/.gitconfig 常見內容:

[user]
    name = Your Name
    email = your.email@example.com
[core]
    editor = vim
    autocrlf = input
[alias]
    st = status
    co = checkout
    ci = commit
    br = branch

2.5 初始化與克隆

mkdir myproject && cd myproject
git init                                     # 建立新倉庫(產生 .git)
git clone https://github.com/user/repo.git   # 克隆遠端倉庫

2.6 基本操作

git add filename            # 加入暫存區(git add . 加入全部)
git commit -m "提交信息"     # 提交
git status                  # 查看狀態
git log                     # 查看日誌(--oneline 簡化)
git diff                    # 比較工作目錄與暫存區
git diff --staged           # 比較暫存區與上次提交

2.7 分支管理

分支讓多人/多功能並行開發、隔離風險、便於版本回溯。

git checkout -b new-branch      # 建立並切換(= git branch + git checkout)
git checkout branch-name        # 切換分支
git merge branch-name           # 合併到當前分支
git merge --no-ff branch-name   # 保留分支歷史(不快進合併)

合併衝突時手動解決,再:

git add resolved-file
git commit -m "解決合併衝突"

2.8 遠端倉庫

git remote add origin https://github.com/user/repo.git
git push origin branch-name
git pull origin branch-name
git fetch

2.9 標籤管理

git tag tag-name                 # 輕量標籤
git tag -a tag-name -m "描述"     # 附註標籤
git tag                          # 列出所有標籤
git push origin tag-name         # 推送單一標籤
git push origin --tags           # 推送全部標籤
git tag -d tag-name              # 刪除本地標籤
git push origin :refs/tags/tag-name   # 刪除遠端標籤

2.10 忽略文件(.gitignore)

在倉庫根目錄建立 .gitignore:

# 忽略所有 .log 文件
*.log
# 忽略 node_modules 目錄
node_modules/
# 忽略特定文件
secret.txt

檢查:git status 應看不到被忽略的檔案。

2.11 撤銷與回滾

git checkout -- filename         # 撤銷工作目錄中的修改
git reset HEAD filename          # 撤銷暫存(保留工作目錄修改)
git reset --soft HEAD~1          # 撤銷最近一次提交、保留變更
git reset --hard HEAD~1          # 完全撤銷最近一次提交與變更
git revert commit-hash           # 以新提交回滾特定提交
git commit --amend -m "新訊息"    # 修改最近一次提交訊息
git checkout commit-hash         # 回到某歷史版本(git checkout main 回最新)

2.12 Stash(暫存工作進度)

git stash                 # 保存當前工作進度
git stash list            # 查看列表
git stash apply           # 恢復(保留 stash)
git stash pop             # 恢復並刪除
git stash drop stash@{0}  # 刪除指定
git stash clear           # 刪除全部

2.13 比較與分析

git diff commit1 commit2   # 比較兩個提交
git log -p                 # 顯示每個提交的差異
git blame filename         # 逐行顯示最後修改者

二分搜尋問題提交:

git bisect start
git bisect bad HEAD
git bisect good commit-hash
# ...Git 自動切換提交,找到後:
git bisect reset

2.14 其他常用命令

git rm filename            # 刪除文件並提交
git rm --cached filename   # 只從暫存區移除(保留工作目錄)
git mv old-filename new-filename   # 移動/重命名
git branch -a              # 列出本地與遠端分支
git diff > patch.patch     # 產生補丁
git apply patch.patch      # 套用補丁

2.15 常見工作流程

git checkout main
git checkout -b new-feature
git add .
git commit -m "添加新功能"
git checkout main
git merge new-feature
git branch -d new-feature

2.16 進階技巧

Git Alias(寫在 .gitconfig 的 [alias]):

[alias]
    st = status
    co = checkout
    lg = log --oneline --graph --decorate --all

Git Hooks:在 .git/hooks/ 放腳本,於特定事件自動執行(如 pre-commit):

#!/bin/sh
# pre-commit hook example
echo "Running pre-commit hook"
./run-tests.sh
chmod +x .git/hooks/pre-commit

Git Submodules:把另一個倉庫放進子目錄:

git submodule add https://github.com/user/submodule-repo.git path/to/submodule
git submodule init
git submodule update

2.17 GitHub 與 GitLab

git clone https://github.com/username/repository.git
git clone https://gitlab.com/username/repository.git

2.18 最佳實踐

2.19 小結

本章介紹後端開發與 IDE 選擇,以及 Git 的完整基礎:配置、初始化、基本操作、分支、遠端、標籤、忽略文件、撤銷與回滾、Stash、比較分析、工作流程、進階技巧與最佳實踐。

3. Git 進階操作

3.1 分支策略與工作流程

分支策略

合併方式

git merge --ff-only branch-name   # 快進
git merge branch-name             # 三方合併
git merge --no-ff branch-name     # 無快進

常見工作流程

master/maindevelopfeature 分支releasehotfix
圖 3 Git Flow:長期分支 main/develop,短期分支 feature、release、hotfix 各自合併回對應的長期分支。

Git Flow 工具:

brew install git-flow
git flow init
git flow feature start new-feature
git flow feature finish new-feature
git flow release start 1.0.0
git flow release finish 1.0.0
git flow hotfix start 1.0.1
git flow hotfix finish 1.0.1

3.2 變基(Rebase)

概念:把一個分支的提交「重新套用」到另一個分支之上,產生更整潔的線性歷史。

與合併的差異

merge:留下一個合併 commit(菱形)feature 分支rebase:歷史攤平成一直線原 commit 被重寫(虛線)
圖 merge 會在兩條歷史交會處留下一個合併 commit(菱形);rebase 則把分支 commit 重寫到目標分支之後,歷史變成一直線。
git rebase branch-name      # 基本變基
git rebase -i branch-name   # 交互式(pick / reword / edit / squash / fixup / drop)
git rebase --continue       # 解決衝突後繼續
git rebase --abort          # 中止變基

3.3 Cherry-pick 與 Reflog

git cherry-pick commit-hash     # 把特定提交套用到當前分支
git reflog                      # 查看引用更新歷史
git reset --hard commit-hash    # 用 reflog 找到的提交恢復

3.4 標籤、Stash 與 Submodule(進階)

git show tag-name                 # 查看標籤資訊

git stash push -m "描述"           # 帶描述保存
git stash apply stash@{0}         # 恢復特定進度

git submodule update --remote     # 同步子模組
git rm path/to/submodule          # 移除子模組(再清 .gitmodules 與 .git/modules)

3.5 Git Hooks 與 Git LFS

Hooks:在特定事件(提交、合併、推送)自動執行的腳本;常用 pre-commit、commit-msg、pre-push。

#!/bin/sh
# .git/hooks/pre-commit
echo "Running pre-commit hook"
./run-tests.sh
chmod +x .git/hooks/pre-commit

Git LFS(大檔案儲存):

git lfs install
git lfs track "*.psd"
git add .gitattributes
git commit -m "Add Git LFS tracking for PSD files"

3.6 大型倉庫效能

git fetch -j4        # 並行抓取

搭配淺克隆、partial clone、定期 git gc 等,可降低大型倉庫的操作負擔。

4. 遠端協作與程式碼託管平台

4.1 GitHub

全球最大的代碼託管平台;提供 Pull Request、Issues、Actions 等協作功能。

  1. 建立倉庫:登入 → New repository → 輸入名稱與描述、選公開或私有 → Create repository。
  2. 發起 Pull Request:推送功能分支 → New pull request → 選目標與來源分支 → 填描述 → Create。
git clone https://github.com/username/repository.git

4.2 GitLab

功能類似 GitHub,並內建 CI/CD。

  1. 建立專案:New project → 設定 → Create project。
  2. 發起合併請求(Merge Request):推送功能分支 → Merge Requests → New merge request → Submit。
git clone https://gitlab.com/username/repository.git

4.3 Bitbucket

Atlassian 平台,支援 Git/Mercurial,可與 Jira、Confluence 整合;以 Pull Request 協作。

git clone https://bitbucket.org/username/repository.git

4.4 Pull Request / Merge Request 流程

  1. 在功能分支開發並推送。
  2. 發起 PR/MR,指定目標分支。
  3. 團隊審查、提出建議。
  4. 依意見修改並再推送。
  5. 通過後合併到主分支。

5. Git 圖形化工具與 IDE 整合

5.1 圖形化工具(GUI)

工具 特點
GitKraken 界面簡潔,分支管理、代碼審查、衝突解決
SourceTree Atlassian 出品,Windows/macOS,直觀
GitHub Desktop GitHub 官方,簡單易用

操作大同小異:開啟後登入 → Clone 倉庫 → 建立分支 → 提交 → 合併分支。

5.2 IDE 內建 Git

6. 安全與最佳實踐

6.1 使用者認證

使用 SSH 金鑰或 Token 存取遠端;啟用雙重驗證(2FA)。

6.2 保護敏感資訊

避免提交密碼/金鑰;用秘密掃描工具找出外洩:

brew install gitleaks
gitleaks detect

6.3 分支保護

限制對主分支的直接推送與強制推送,強制要求審查與狀態檢查。

6.4 程式碼審查

透過 Pull Request/Merge Request 審查,發現問題、統一風格、共享知識。

6.5 提交訊息與版本標籤

6.6 備份與保護歷史

git clone --mirror /path/to/repository /path/to/backup   # 備份
git clone /path/to/backup /path/to/repository            # 還原

並在主分支停用強制推送、設定保護規則。

7. 持續整合與持續部署(CI/CD)

7.1 概念

git push推送程式碼CI:安裝pip install -rCI:測試manage.py test建置hexo/容器映像部署與監控Pages/K8s失敗即中斷
圖 CI/CD 管線:push 之後自動安裝依賴、跑測試(失敗即中斷),再建置與部署,最後由監控與日誌接手。

7.2 GitHub Actions

name: CI
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-python@v7
        with:
          python-version: '3.14'
      - run: python -m pip install --upgrade pip && pip install -r requirements.txt
      - run: pytest

7.3 Jenkins Pipeline

pipeline {
    agent any
    stages {
        stage('Build')  { steps { sh 'make build' } }
        stage('Test')   { steps { sh 'make test' } }
        stage('Deploy') { steps { sh 'make deploy' } }
    }
}

7.4 GitLab CI/CD

stages:
  - build
  - test
  - deploy

build:
  stage: build
  script: [make build]
test:
  stage: test
  script: [make test]
deploy:
  stage: deploy
  script: [make deploy]
  only: [main]

7.5 Travis CI(.travis.yml)

language: python
python: ['3.14']
install: [pip install -r requirements.txt]
script: [pytest]
deploy:
  provider: script
  script: bash deploy.sh
  on:
    branch: main

8. 開源專案與團隊協作

8.1 開源專案管理

8.2 團隊協作

8.3 貢獻開源

  1. 找有興趣的專案,讀 README.md/CONTRIBUTING.md。
  2. 用 Issue 回報問題(附詳細資訊與重現步驟)。
  3. Fork → 開功能分支 → 提交 → 發 PR/MR。
  4. 參與討論與程式碼審查。

9. Git 在雲端的應用(DevOps)

9.1 雲計算概述

9.2 基礎設施即程式碼(IaC)

9.3 容器化與編排

Docker:把應用打包成容器,用 Dockerfile 定義。

FROM python:3.14
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

Kubernetes:自動管理容器的部署與擴縮,確保高可用。

kubectl apply -f deployment.yaml

9.4 監控與日誌

Prometheus + Grafana:指標監控與視覺化。

global:
  scrape_interval: 15s
scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

ELK(Elasticsearch + Logstash + Kibana):日誌收集與分析。

input {
  file { path => "/var/log/myapp.log" start_position => "beginning" }
}
output {
  elasticsearch { hosts => ["localhost:9200"] }
}

9.5 雲資源管理最佳實踐

10. 總結

本章從後端開發與 IDE 選擇談起,完整走過 Git:設定、基本操作、分支策略與工作流程、變基與進階操作、遠端協作與平台、GUI 工具與 IDE 整合、安全與最佳實踐、CI/CD,最後延伸到雲端 DevOps(IaC、容器與 K8s、監控與日誌)。掌握這些即可在團隊中建立可靠、可維護的後端開發流程。