git 常用指令指南
Git 常用指令指南
這份指南整理軟體工程師日常最常使用的 Git 指令,並依照實際工作流程分類。
1. 基本設定
# 查看設定
git config --list
# 設定身份
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# 預設分支名稱
git config --global init.defaultBranch main
# 自動清除遠端已刪除分支的追蹤資訊
git config --global fetch.prune true
設定編輯器:
# Vim
git config --global core.editor "vim"
# VS Code
git config --global core.editor "code --wait"
2. 建立與下載 Repository
# 初始化
git init
# Clone
git clone <repository-url>
# 指定目錄
git clone <repository-url> <folder-name>
# 指定分支
git clone -b <branch-name> <repository-url>
3. 查看狀態與差異
# 工作區狀態
git status
# 精簡狀態
git status -sb
# 尚未 Stage 的變更
git diff
# 已 Stage 的變更
git diff --staged
# 特定檔案
git diff -- path/to/file
4. Stage 與 Commit
# 加入單一檔案
git add path/to/file
# 加入目前目錄所有變更
git add .
# 加入新增、修改與刪除
git add -A
# 逐段選擇要加入的修改
git add -p
# Commit
git commit -m "feat: add user login"
修改上一個 Commit:
# 修改訊息
git commit --amend -m "fix: update login validation"
# 把新修改加入上一個 Commit
git add .
git commit --amend --no-edit
已推送到多人共用分支的 Commit,不建議使用
--amend。
5. 查看 Commit 紀錄
# 完整紀錄
git log
# 單行紀錄
git log --oneline
# 分支圖
git log --oneline --graph --decorate --all
# 特定檔案歷史
git log -- path/to/file
# 查看 Commit 內容
git show <commit-hash>
# 查看每一行的修改者
git blame path/to/file
6. 分支管理
# 本地分支
git branch
# 本地與遠端分支
git branch -a
# 建立分支
git branch <branch-name>
# 建立並切換
git switch -c <branch-name>
# 切換分支
git switch <branch-name>
# 返回上一個分支
git switch -
# 重新命名目前分支
git branch -m <new-branch-name>
# 刪除已合併分支
git branch -d <branch-name>
# 強制刪除分支
git branch -D <branch-name>
舊版 Git 也常見:
git checkout -b <branch-name>
git checkout <branch-name>
7. 遠端 Repository
# 查看遠端
git remote -v
# 新增遠端
git remote add origin <repository-url>
# 修改遠端 URL
git remote set-url origin <repository-url>
# 下載遠端資訊,不合併
git fetch
# 拉取並合併
git pull
# 拉取並 Rebase
git pull --rebase
# 第一次推送並設定 Upstream
git push -u origin <branch-name>
# 後續推送
git push
# 刪除遠端分支
git push origin --delete <branch-name>
8. Merge
# 把指定分支合併到目前分支
git merge <branch-name>
# 強制建立 Merge Commit
git merge --no-ff <branch-name>
# 中止 Merge
git merge --abort
範例:
git switch develop
git merge feature/login
解決衝突後:
git add .
git commit
9. Rebase
# 將目前分支接到 develop 最新進度之後
git rebase develop
# 整理最近三個 Commit
git rebase -i HEAD~3
互動式 Rebase 常見動作:
| 動作 | 用途 |
|---|---|
pick |
保留 Commit |
reword |
修改訊息 |
edit |
修改內容 |
squash |
合併並保留訊息 |
fixup |
合併並捨棄訊息 |
drop |
刪除 Commit |
衝突處理:
# 解決衝突後繼續
git add .
git rebase --continue
# 跳過目前 Commit
git rebase --skip
# 中止 Rebase
git rebase --abort
不要對多人共用且已推送的分支任意 Rebase。
10. Stash
# 暫存修改
git stash
# 加上說明
git stash push -m "WIP: login page"
# 包含未追蹤檔案
git stash -u
# 查看清單
git stash list
# 套用但保留 Stash
git stash apply
# 套用並刪除 Stash
git stash pop
# 套用指定 Stash
git stash apply stash@{1}
# 刪除指定 Stash
git stash drop stash@{1}
# 清空全部 Stash
git stash clear
11. 還原工作區與 Stage
# 還原單一未 Stage 檔案
git restore path/to/file
# 還原全部未 Stage 修改
git restore .
# 取消單一檔案 Stage
git restore --staged path/to/file
# 取消全部 Stage
git restore --staged .
12. Reset
# 取消 Commit,但保留 Stage
git reset --soft HEAD~1
# 取消 Commit,並取消 Stage,但保留檔案修改
git reset --mixed HEAD~1
# 完全刪除 Commit 與修改
git reset --hard HEAD~1
同步成遠端狀態:
git fetch origin
git reset --hard origin/main
--hard會刪除尚未提交的修改。
13. Revert
建立一個新的反向 Commit:
# 撤銷指定 Commit
git revert <commit-hash>
# 撤銷最近一次 Commit
git revert HEAD
| 指令 | 特性 | 適合情境 |
|---|---|---|
reset |
改寫歷史 | 尚未推送的本地 Commit |
revert |
建立反向 Commit | 已推送的共享分支 |
14. Cherry-pick
# 套用單一 Commit
git cherry-pick <commit-hash>
# 套用多個 Commit
git cherry-pick <commit-1> <commit-2>
# 套用一段 Commit
git cherry-pick <start-commit>^..<end-commit>
# 中止
git cherry-pick --abort
15. Tag
# 查看 Tag
git tag
# 建立輕量 Tag
git tag v1.0.0
# 建立附註 Tag
git tag -a v1.0.0 -m "Release v1.0.0"
# 推送單一 Tag
git push origin v1.0.0
# 推送全部 Tag
git push origin --tags
# 刪除本地 Tag
git tag -d v1.0.0
# 刪除遠端 Tag
git push origin --delete v1.0.0
16. Git Worktree
Worktree 可以讓同一個 Repository 同時開啟多個分支。
# 查看 Worktree
git worktree list
# 建立新分支 Worktree
git worktree add ../project-feature -b feature/login main
# 使用既有分支
git worktree add ../project-hotfix hotfix/login
# 移除 Worktree
git worktree remove ../project-feature
# 清除失效紀錄
git worktree prune
適合:
- 同時處理多個 Feature
- 緊急 Hotfix
- Code Review
- 一個目錄跑測試,另一個繼續開發
17. 清理檔案與分支
# 預覽要刪除的未追蹤檔案
git clean -n
# 刪除未追蹤檔案
git clean -f
# 刪除未追蹤檔案與目錄
git clean -fd
# 清除失效的遠端追蹤分支
git fetch --prune
# 查看已合併分支
git branch --merged
刪除已合併的本地分支:
git branch --merged |
grep -vE '^\*|main|master|develop' |
xargs -r git branch -d
18. 找回遺失 Commit
# 查看 HEAD 操作歷史
git reflog
找回 Commit:
# 直接回復
git reset --hard <commit-hash>
# 建立救援分支,通常更安全
git switch -c rescue-branch <commit-hash>
git reflog 適合處理:
- 誤用
reset --hard - Rebase 後 Commit 消失
- 誤刪本地分支
- 找回先前的 HEAD
19. 常見情境
Commit 到錯誤分支
# 從目前 Commit 建立正確分支
git switch -c feature/correct-branch
# 回到原分支,移除錯誤 Commit
git switch original-branch
git reset --hard HEAD~1
尚未 Commit,但需要切換分支
git stash -u
git switch other-branch
回來後:
git switch original-branch
git stash pop
本地分支落後遠端
git pull --rebase
完全放棄本地修改,同步遠端
git fetch origin
git reset --hard origin/main
git clean -fd
只取得另一個分支的單一檔案
git restore --source <branch-name> -- path/to/file
比較兩個分支
# 檔案差異
git diff branch-a..branch-b
# Commit 差異
git log branch-a..branch-b --oneline
20. 推薦 Alias
加入 ~/.gitconfig:
[alias]
st = status -sb
co = switch
br = branch
ci = commit
last = log -1 HEAD
lg = log --oneline --graph --decorate --all
unstage = restore --staged
amend = commit --amend --no-edit
Shell Alias:
alias gs='git status -sb'
alias ga='git add'
alias gc='git commit'
alias gd='git diff'
alias gl='git log --oneline --graph --decorate --all'
alias gp='git pull'
alias gps='git push'
alias gsw='git switch'
21. 建議日常開發流程
建立 Feature Branch
git switch develop
git pull --rebase
git switch -c feature/user-login
開發並 Commit
git status
git add -p
git commit -m "feat: add user login"
同步 develop
git fetch origin
git rebase origin/develop
推送分支
git push -u origin feature/user-login
Rebase 後更新自己的遠端 Feature Branch:
git push --force-with-lease
優先使用
--force-with-lease,不要直接使用--force。
22. Commit Message 建議
推薦使用 Conventional Commits:
<type>(optional scope): <description>
| Type | 用途 |
|---|---|
feat |
新功能 |
fix |
Bug 修正 |
docs |
文件修改 |
refactor |
重構 |
test |
測試 |
chore |
維護工作 |
perf |
效能改善 |
ci |
CI/CD 修改 |
build |
建置系統修改 |
git commit -m "feat(auth): add OAuth login"
git commit -m "fix(api): handle timeout response"
git commit -m "docs: update deployment guide"
git commit -m "refactor(user): simplify validation logic"
23. 危險指令提醒
以下指令可能造成資料遺失或改寫歷史:
git reset --hard
git clean -fd
git push --force
git rebase
git commit --amend
執行前建議先確認:
git status
git log --oneline --graph --decorate -10
git reflog
強制推送時優先使用:
git push --force-with-lease
24. 快速查詢表
| 需求 | 指令 |
|---|---|
| 查看狀態 | git status -sb |
| 查看變更 | git diff |
| 互動式 Stage | git add -p |
| 建立 Commit | git commit -m "message" |
| 查看分支 | git branch -a |
| 建立分支 | git switch -c branch-name |
| 切換分支 | git switch branch-name |
| 拉取更新 | git pull --rebase |
| 推送分支 | git push -u origin branch-name |
| 暫存修改 | git stash -u |
| 套用暫存 | git stash pop |
| 取消 Stage | git restore --staged file |
| 還原檔案 | git restore file |
| 撤銷共享 Commit | git revert commit-hash |
| 找回 Commit | git reflog |
| 顯示分支圖 | git log --oneline --graph --decorate --all |
| 建立 Worktree | git worktree add <path> -b <branch> <base> |
延伸閱讀
- Shell 快捷鍵指南
- ripgrep rg 使用教學
- tmux 常用指南
- OpenCode Autocomplete 指南
- Git Merge Policy
- Git Worktree 使用指南
- Conventional Commits
- GitHub Pull Request 工作流程