Git 常用指令
依 workspace、staging、local、remote 整理日常指令。細節仍請看各主題頁;這裡當速查表。
指令表
| Git 指令 | 區域影響 | 說明 |
|---|---|---|
git init |
local | 初始化儲存庫,建立 .git |
git clone <url> |
workspace, local, remote | 從遠端複製整包到本機 |
git add <file> |
staging | 指定檔進入暫存區 |
git add . |
staging | 目前目錄所有變更進入暫存區 |
git status |
workspace, staging | 看修改、暫存與分支狀態 |
git commit -m "訊息" |
local | 暫存區寫成一次提交 |
git commit --amend |
local | 修改最新一次提交(未 push 時較安全) |
git diff |
workspace | 工作區相對暫存區 |
git diff --staged |
staging | 暫存區相對 HEAD |
git log |
local | 提交歷史 |
git push origin <分支> |
remote | 本機分支推到遠端 |
git pull origin <分支> |
workspace, local, remote | 下載並合併 |
git fetch |
remote | 只下載,不合併 |
git merge <分支> |
local | 把指定分支合進目前分支 |
git switch <分支> |
workspace | 切換分支(取代舊的 git checkout <分支>) |
git switch -c <新分支> |
local, workspace | 建立並切換(取代舊的 git checkout -b) |
git switch - |
workspace | 切回上一個分支 |
git branch |
local | 列出分支 |
git branch <名稱> |
local | 建立分支(不切換) |
git restore --staged <file> |
staging | 取消 add |
git reset --soft <commit> |
local | 移動 HEAD,保留暫存與工作區 |
git reset --mixed <commit> |
local, staging | 移動 HEAD,變更留在工作區 |
git reset --hard <commit> |
local, staging, workspace | 三區都回到該提交,謹慎使用 |
git stash |
stash | 暫存未完成修改 |
git stash apply |
workspace, staging | 取回 stash,保留清單 |
git stash pop |
workspace, staging | 取回並刪除該筆 stash |
git stash list |
stash | 列出 stash |
git stash drop |
stash | 刪除指定 stash |
區域解釋
- Workspace:正在編輯的工作目錄。
- Staging (Index):準備提交的快照。
- Local:本機完整歷史。
- Remote:GitHub、GitLab 等。
- Stash:臨時抽屜,不是正式版本。