【Git】常用指令
Git 進階指令速查表(實戰版)
| 類別 | 指令 | 說明 | 常見情境 |
|---|---|---|---|
| Merge | git merge branch |
合併 branch | feature 合併到 main |
| Rebase | git rebase branch |
重排 commit | 整理 commit 歷史 |
| Rebase | git rebase -i HEAD~3 |
互動式 rebase | squash commit |
| Cherry-pick | git cherry-pick <commit> |
套用指定 commit | hotfix |
| Revert | git revert <commit> |
回復某 commit | 線上 rollback |
| Reflog | git reflog |
查看 HEAD 歷史 | 找回被 reset 的 commit |
Rebase vs Merge
| 操作 | 指令 | 結果 |
|---|---|---|
| merge | git merge main |
保留 merge commit |
| rebase | git rebase main |
重新排列 commit |
Merge
A---B---C main
\
D---E feature
\
F (merge)
Rebase
A---B---C main
\
D'---E' feature
📌 結論
| 操作 | 使用情境 |
|---|---|
| merge | 保留歷史 |
| rebase | 整理 commit |
Interactive Rebase(整理 commit)
git rebase -i HEAD~3
會出現
pick a1b2 commit1
pick c3d4 commit2
pick e5f6 commit3
可修改為
pick a1b2 commit1
squash c3d4 commit2
squash e5f6 commit3
結果:
commit1 + commit2 + commit3
變成一個 commit
常用於:
PR 整理 commit
Cherry-pick(套用某 commit)
git cherry-pick <commit-id>
用途:
| 情境 | 範例 |
|---|---|
| hotfix | 從 dev 複製到 main |
| patch | 套用某 bug fix |
例如:
dev
A--B--C--D
main
A--B
git cherry-pick D
結果
main
A--B--D
Revert(安全 rollback)
git revert <commit>
Git 會產生一個 反向 commit
例如:
A--B--C
revert B
A--B--C--D
↑
undo B
📌 適合
production rollback
Reflog(救命指令)
查看 HEAD 歷史
git reflog
例如
a1b2 HEAD@{0}: reset: moving to HEAD~1
c3d4 HEAD@{1}: commit: fix bug
恢復:
git reset --hard c3d4
很多工程師不知道:
Git 幾乎不會真的丟資料
reflog 都能救回
Detached HEAD
發生情況
git checkout <commit-id>
Git 會顯示
You are in 'detached HEAD' state
代表:
HEAD 沒有 branch
解法
建立 branch:
git checkout -b new-branch
Clean(清除未追蹤檔案)
查看將刪除
git clean -n
刪除
git clean -fd
常用於:
CI pipeline
重新 build
Force Push
git push -f
⚠️ 危險
可能覆蓋他人 commit
安全方式:
git push --force-with-lease
這會檢查遠端是否被更新。
Reset / Restore / Checkout 差異
| 指令 | 影響範圍 |
|---|---|
| reset | commit + staging |
| restore | working tree |
| checkout | branch 或檔案 |
Git 三層結構(核心概念)
Git 有三個區域:
Working Directory
↓
Staging Area (Index)
↓
Repository (Commit)
對應指令:
| 操作 | 指令 |
|---|---|
| working → staging | git add |
| staging → repo | git commit |
| repo → working | git checkout |
| repo → staging | git reset |
DevOps 常用 Git 重置流程
很多 CI pipeline 會用:
git fetch origin
git reset --hard origin/main
git clean -fd
作用:
確保 workspace 乾淨
Git Flow(常見分支策略)
常見 branch:
main
develop
feature/*
hotfix/*
release/*
流程:
feature -> develop
develop -> release
release -> main
hotfix -> main
Git Debug 指令
| 指令 | 用途 |
|---|---|
git blame file |
查看誰修改 |
git bisect |
找 bug commit |
git show |
查看 commit |
git diff commit1 commit2 |
比較 commit |
工程師最強 Git 指令 TOP 10
| 指令 | 用途 |
|---|---|
git log --oneline --graph |
查看歷史 |
git reflog |
救 commit |
git rebase -i |
整理 commit |
git cherry-pick |
套 commit |
git revert |
rollback |
git clean -fd |
清 workspace |
git reset --hard |
重置 |
git stash |
暫存 |
git blame |
找修改人 |
git bisect |
找 bug |
💡 給你一個小建議
如果你常用 Git(你目前 DevOps / CI / Jenkins workflow 看起來是這樣),
最推薦記住 這 6 個核心指令:
git log --oneline --graph
git reflog
git rebase -i
git cherry-pick
git reset --hard
git clean -fd
這份會非常接近 資深工程師 Git 操作指南。