【筆記】Git 指令筆記
目錄
本文為 Git 操作的常用指令筆記。
常用指令
| 類別 | 指令 | 參數說明 | 效果 |
|---|---|---|---|
| Repository | git clone <repository> | <repository>:Repository URL 或路徑 | 將遠端 Repository 複製到本機 |
| 狀態 | git status | 無 | 查看目前 Repository 狀態 |
| 差異 | git diff | 無 | 查看尚未加入 Stage 的修改 |
| Stage | git add <file> | <file>:指定檔案 | 將指定檔案加入 Staging Area |
| Stage | git add . | .:目前目錄 | 將目前目錄的變更加入 Staging Area |
| Commit | git commit -m "<message>" | -m:Commit 訊息;<message>:訊息內容 | 建立新的 Commit |
| 歷史 | git log --oneline | --oneline:每個 Commit 以單行顯示 | 簡潔查看 Commit 歷史 |
| Branch | git branch | 無 | 查看本機 Branch |
| Branch | git switch <branch> | <branch>:Branch 名稱 | 切換至指定 Branch |
| Branch | git switch -c <branch> | -c:create;<branch>:新 Branch 名稱 | 建立並切換至新 Branch |
| Merge | git merge <branch> | <branch>:來源 Branch | 將指定 Branch 合併至目前 Branch |
| Remote | git remote -v | -v:verbose | 查看 Remote URL |
| 更新 | git pull | 無 | 取得遠端更新並整合至目前 Branch |
| 上傳 | git push | 無 | 將本機 Commit 推送至設定好的遠端 Branch |
| 上傳 | git push -u <remote> <branch> | -u:設定 Upstream;<remote>:Remote 名稱;<branch>:Branch 名稱 | Push 並建立 Branch 的 Upstream 關係 |
| 暫存 | git stash | 無 | 暫時保存尚未 Commit 的修改 |
| 恢復暫存 | git stash pop | 無 | 套用最近的 Stash 並刪除該 Stash |
| 還原 | git restore <file> | <file>:指定檔案 | 丟棄工作區對指定檔案的修改 |
| 取消 Stage | git restore --staged <file> | --staged:操作 Staging Area | 將檔案移出 Stage,但保留工作區修改 |
初始化與設定
| 指令 | 參數說明 | 效果 |
|---|---|---|
git --version | 無 | 查看目前 Git 版本 |
git help <command> | <command>:Git 指令 | 查看指定指令的說明 |
git <command> --help | --help:顯示說明 | 查看指定 Git 指令的說明 |
git config --list | --list:列出設定 | 查看目前 Git 設定 |
git config --global user.name "<name>" | --global:套用至目前使用者;user.name:作者名稱 | 設定 Git Commit 作者名稱 |
git config --global user.email "<email>" | --global:套用至目前使用者;user.email:作者 Email | 設定 Git Commit 作者 Email |
git config <key> | <key>:設定項目 | 查看指定 Git 設定 |
git config --global <key> <value> | --global:使用者層級;<key>:設定項目;<value>:設定值 | 設定使用者層級的 Git 設定 |
git config --local <key> <value> | --local:Repository 層級 | 設定目前 Repository 的 Git 設定 |
Repository
| 指令 | 參數說明 | 效果 |
|---|---|---|
git init | 無 | 將目前資料夾建立為 Git Repository |
git init <directory> | <directory>:資料夾名稱 | 建立指定資料夾並初始化 Git Repository |
git clone <repository> | <repository>:Repository URL 或路徑 | 將遠端 Repository 複製到本機 |
git clone <repository> <directory> | <directory>:本機資料夾名稱 | 將 Repository 複製到指定資料夾 |
git clone --branch <branch> <repository> | --branch:指定 Branch;<branch>:Branch 名稱 | Clone 指定 Branch |
git clone --depth <number> <repository> | --depth:Commit 深度;<number>:下載的歷史層數 | 建立 Shallow Clone,只下載有限的 Commit 歷史 |
Status / Diff
| 指令 | 參數說明 | 效果 |
|---|---|---|
git status | 無 | 查看目前 Repository 狀態 |
git status -s | -s:short | 以簡短格式顯示狀態 |
git status -b | -b:branch | 顯示目前 Branch 資訊 |
git status -sb | -s + -b | 以簡短格式顯示檔案與 Branch 狀態 |
git diff | 無 | 查看 Working Directory 尚未 Stage 的修改 |
git diff <file> | <file>:檔案 | 查看指定檔案尚未 Stage 的修改 |
git diff --cached | --cached:比較 Staging Area | 查看已 Stage、但尚未 Commit 的修改 |
git diff --staged | --staged:同 --cached | 查看已 Stage 的修改 |
git diff HEAD | HEAD:目前 Commit | 比較目前工作區與目前 Commit 的差異 |
git diff <commit> | <commit>:Commit | 比較目前工作區與指定 Commit |
git diff <commit1> <commit2> | <commit1>、<commit2>:兩個 Commit | 比較兩個 Commit 的差異 |
git diff <branch1> <branch2> | <branch1>、<branch2>:兩個 Branch | 比較兩個 Branch 的差異 |
git diff --stat | --stat:統計 | 顯示修改檔案與行數統計 |
git diff --name-only | --name-only | 只顯示修改過的檔名 |
Stage / Commit
| 指令 | 參數說明 | 效果 |
|---|---|---|
git add <file> | <file>:檔案 | 將指定檔案加入 Staging Area |
git add <directory> | <directory>:資料夾 | 將指定資料夾的變更加入 Staging Area |
git add . | .:目前目錄 | 將目前目錄下的變更加入 Staging Area |
git add -A | -A:所有變更 | 將新增、修改、刪除全部加入 Staging Area |
git add -u | -u:tracked files | 只 Stage 已追蹤檔案的修改與刪除 |
git add -p | -p:patch | 互動式選擇部分修改進行 Stage |
git add -n <file> | -n:dry run;<file>:檔案 | 預覽會加入哪些檔案,不實際加入 |
git add -f <file> | -f:force;<file>:檔案 | 強制加入通常被 .gitignore 忽略的檔案 |
git commit | 無 | 開啟編輯器輸入 Commit Message 並建立 Commit |
git commit -m "<message>" | -m:message;<message>:訊息 | 使用指定訊息建立 Commit |
git commit -a -m "<message>" | -a:all | 將已追蹤檔案的修改直接 Commit;不包含未追蹤檔案 |
git commit --amend | --amend:修改上一個 Commit | 修改最近一次 Commit |
git commit --amend --no-edit | --no-edit:保留原訊息 | 修改最近一次 Commit,但保留原 Commit Message |
Commit History
| 指令 | 參數說明 | 效果 |
|---|---|---|
git log | 無 | 查看 Commit 歷史 |
git log --oneline | --oneline | 每個 Commit 以一行顯示 |
git log --graph | --graph | 以 ASCII 圖顯示 Branch 結構 |
git log --all | --all | 顯示所有 Branch 的歷史 |
git log --oneline --graph | --oneline + --graph | 以簡潔圖形查看 Commit 歷史與 Branch 結構 |
git log -n <number> | -n:筆數;<number>:數量 | 只顯示指定數量的 Commit |
git log --author="<name>" | --author:作者篩選;<name>:作者名稱 | 查看指定作者的 Commit |
git log -- <file> | --:分隔 Revision 與路徑;<file>:檔案 | 查看指定檔案的 Commit 歷史 |
git show <commit> | <commit>:Commit ID | 查看指定 Commit 的詳細資訊與修改內容 |
git show <commit>:<file> | <commit>:Commit;<file>:檔案 | 查看指定 Commit 中的檔案內容 |
git show --stat <commit> | --stat:統計;<commit>:Commit | 查看指定 Commit 的修改統計 |
Branch
| 指令 | 參數說明 | 效果 |
|---|---|---|
git branch | 無 | 查看本機 Branch |
git branch -a | -a:all | 查看本機與 Remote Branch |
git branch -r | -r:remote | 只查看 Remote Branch |
git branch -v | -v:verbose | 顯示 Branch 最新 Commit |
git branch -vv | -vv:very verbose | 顯示 Branch 與 Upstream 關係及追蹤資訊 |
git branch --show-current | --show-current | 顯示目前所在 Branch |
git branch <branch> | <branch>:新 Branch 名稱 | 建立 Branch,但不切換 |
git switch <branch> | <branch>:Branch 名稱 | 切換至指定 Branch |
git switch -c <branch> | -c:create;<branch>:新 Branch | 建立並切換至新 Branch |
git checkout <branch> | <branch>:Branch 名稱 | 切換 Branch;舊版與相容性工作流程常見 |
git checkout -b <branch> | -b:branch;<branch>:新 Branch | 建立並切換 Branch |
git branch -m <new-name> | -m:move;<new-name>:新名稱 | 重新命名目前 Branch |
git branch -M <new-name> | -M:force move;<new-name>:新名稱 | 強制重新命名 Branch |
git branch -d <branch> | -d:delete;<branch>:Branch | 刪除已合併的 Branch |
git branch -D <branch> | -D:force delete;<branch>:Branch | 強制刪除 Branch |
git branch -u <remote>/<branch> | -u:upstream | 設定目前 Branch 的 Upstream |
Merge 與衝突
| 指令 | 參數說明 | 效果 |
|---|---|---|
git merge <branch> | <branch>:來源 Branch | 將指定 Branch 合併至目前 Branch |
git merge --no-ff <branch> | --no-ff:no fast-forward | 即使可以 Fast-forward,也建立 Merge Commit |
git merge --ff-only <branch> | --ff-only:只允許 Fast-forward | 只有能 Fast-forward 時才合併 |
git merge --abort | --abort:取消 Merge | 發生 Merge Conflict 時取消尚未完成的 Merge |
Restore / Reset / Revert
| 指令 | 參數說明 | 效果 |
|---|---|---|
git restore <file> | <file>:檔案 | 丟棄工作區對指定檔案的修改 |
git restore . | .:目前目錄 | 丟棄目前目錄中尚未 Stage 的工作區修改 |
git restore --staged <file> | --staged:操作 Stage | 將檔案從 Staging Area 移除,但保留工作區修改 |
git restore --source=<commit> <file> | --source:來源 Commit;<file>:檔案 | 從指定 Commit 還原檔案 |
git reset <file> | <file>:檔案 | 將檔案取消 Stage;效果類似 git restore --staged |
git reset --soft <commit> | --soft:保留 Index 與工作區;<commit>:目標 Commit | 移動 HEAD 至指定 Commit,保留 Stage 與工作區修改 |
git reset --mixed <commit> | --mixed:預設模式 | 移動 HEAD 至指定 Commit,取消 Stage,但保留工作區修改 |
git reset --hard <commit> | --hard:重設 HEAD、Index 與工作區 | 回到指定 Commit,並丟棄 Stage 與工作區修改 |
git revert <commit> | <commit>:Commit | 建立新的 Commit 來抵銷指定 Commit 的修改 |
git revert --no-edit <commit> | --no-edit:使用預設訊息 | Revert 指定 Commit 並保留預設 Commit Message |
git revert <commit1>..<commit2> | Commit 範圍 | 反轉指定範圍中的 Commit |
提示
reset 、 restore 、 revert 用途不同:restore 主要處理檔案內容;reset 可移動 Branch/HEAD 並改變歷史;revert 則透過新 Commit 抵銷既有 Commit。Stash
| 指令 | 參數說明 | 效果 |
|---|---|---|
git stash | 無 | 暫存目前尚未 Commit 的工作 |
git stash push -m "<message>" | -m:訊息;<message>:描述 | 暫存修改並建立描述 |
git stash list | 無 | 查看所有 Stash |
git stash show | 無 | 查看最近 Stash 的摘要 |
git stash show -p | -p:patch | 查看 Stash 的詳細差異 |
git stash apply | 無 | 套用最近 Stash,但保留該 Stash |
git stash apply <stash> | <stash>:Stash ID | 套用指定 Stash,但保留該 Stash |
git stash pop | 無 | 套用最近 Stash 並刪除該 Stash |
git stash drop | 無 | 刪除最近 Stash |
git stash clear | 無 | 清除所有 Stash |
git stash branch <branch> | <branch>:新 Branch | 從指定 Stash 建立並切換至新 Branch |
Remote
| 指令 | 參數說明 | 效果 |
|---|---|---|
git remote | 無 | 查看 Remote 名稱 |
git remote -v | -v:verbose | 查看 Remote URL |
git remote show <remote> | <remote>:Remote 名稱 | 查看 Remote 詳細資訊 |
git remote add <name> <url> | <name>:Remote 名稱;<url>:Repository URL | 新增 Remote |
git remote set-url <name> <url> | <name>:Remote 名稱;<url>:新 URL | 修改 Remote URL |
git remote rename <old> <new> | <old>:舊名稱;<new>:新名稱 | 修改 Remote 名稱 |
git remote remove <name> | <name>:Remote 名稱 | 移除 Remote |
Fetch / Pull / Push
| 指令 | 參數說明 | 效果 |
|---|---|---|
git fetch | 無 | 取得 Remote 更新,但不直接修改目前 Branch |
git fetch <remote> | <remote>:Remote 名稱 | 取得指定 Remote 的更新 |
git fetch --all | --all:所有 Remote | 取得所有 Remote 的更新 |
git fetch --prune | --prune:清理 | 取得更新時移除已不存在的 Remote-tracking Branch |
git pull | 無 | 取得遠端更新並整合至目前 Branch |
git pull <remote> <branch> | <remote>:Remote;<branch>:Branch | 從指定 Remote Branch 取得並整合更新 |
git pull --rebase | --rebase:使用 Rebase | Fetch 後以 Rebase 整合本機提交 |
git pull --no-rebase | --no-rebase:使用 Merge | Fetch 後以 Merge 整合本機提交 |
git pull --ff-only | --ff-only:只允許 Fast-forward | 只有能 Fast-forward 時才整合 |
git push | 無 | 將目前 Branch 推送至設定好的 Upstream |
git push <remote> <branch> | <remote>:Remote;<branch>:Branch | 將本機 Branch 推送至指定 Remote |
git push -u <remote> <branch> | -u:set upstream | Push 並建立 Upstream 關係 |
git push --force | --force:強制 | 強制更新 Remote Branch,可能覆寫遠端歷史 |
git push --force-with-lease | --force-with-lease | 在較安全的條件下強制更新 Remote Branch |
git push <remote> --delete <branch> | --delete:刪除;<branch>:Remote Branch | 刪除 Remote Branch |
git push --tags | --tags:所有 Tags | 將所有本機 Tags 推送至 Remote |
Rebase
| 指令 | 參數說明 | 效果 |
|---|---|---|
git rebase <branch> | <branch>:基底 Branch | 將目前 Branch 的 Commit 重新套用到指定 Branch 之上 |
git rebase -i <commit> | -i:interactive;<commit>:起始 Commit | 互動式整理 Commit 歷史 |
git rebase --continue | --continue | 解決 Conflict 後繼續 Rebase |
git rebase --abort | --abort | 放棄目前 Rebase 並恢復原狀 |
git rebase --skip | --skip | 跳過目前正在套用的 Commit |
Tag
| 指令 | 參數說明 | 效果 |
|---|---|---|
git tag | 無 | 列出 Tags |
git tag <tag> | <tag>:Tag 名稱 | 建立 Lightweight Tag |
git tag -a <tag> -m "<message>" | -a:annotated;-m:訊息 | 建立 Annotated Tag |
git show <tag> | <tag>:Tag 名稱 | 查看 Tag 詳細資訊 |
git tag -d <tag> | -d:delete | 刪除本機 Tag |
git push <remote> <tag> | <remote>:Remote;<tag>:Tag | 推送指定 Tag |
git push <remote> --tags | --tags:所有 Tags | 推送所有 Tags |
git push <remote> --delete <tag> | --delete:刪除;<tag>:Tag | 刪除 Remote Tag |
Worktree
| 指令 | 參數說明 | 效果 |
|---|---|---|
git worktree list | 無 | 列出目前 Repository 的 Worktree |
git worktree add <path> <branch> | <path>:Worktree 路徑;<branch>:Branch | 建立額外 Worktree 並 Checkout 指定 Branch |
git worktree remove <path> | <path>:Worktree 路徑 | 移除指定 Worktree |
git worktree prune | 無 | 清理已不存在 Worktree 的管理資訊 |
Submodule
| 指令 | 參數說明 | 效果 |
|---|---|---|
git submodule add <url> | <url>:另一個 Repository URL | 將另一個 Repository 加入目前專案作為 Submodule |
git submodule init | 無 | 初始化已存在的 Submodule 設定 |
git submodule update | 無 | 將 Submodule 更新至父 Repository 指定的 Commit |
git submodule update --init --recursive | --init:初始化;--recursive:遞迴處理 | 初始化並更新包含巢狀 Submodule 的所有 Submodule |
git submodule update --remote | --remote:使用 Remote 追蹤分支 | 將 Submodule 更新至其 Remote Branch 的最新版本 |


