指令筆記 系列文章

目錄

【筆記】Git 指令筆記

本文為 Git 操作的常用指令筆記。

常用指令

類別指令參數說明效果
Repositorygit clone <repository><repository>:Repository URL 或路徑將遠端 Repository 複製到本機
狀態git status查看目前 Repository 狀態
差異git diff查看尚未加入 Stage 的修改
Stagegit add <file><file>:指定檔案將指定檔案加入 Staging Area
Stagegit add ..:目前目錄將目前目錄的變更加入 Staging Area
Commitgit commit -m "<message>"-m:Commit 訊息;<message>:訊息內容建立新的 Commit
歷史git log --oneline--oneline:每個 Commit 以單行顯示簡潔查看 Commit 歷史
Branchgit branch查看本機 Branch
Branchgit switch <branch><branch>:Branch 名稱切換至指定 Branch
Branchgit switch -c <branch>-c:create;<branch>:新 Branch 名稱建立並切換至新 Branch
Mergegit merge <branch><branch>:來源 Branch將指定 Branch 合併至目前 Branch
Remotegit 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>:指定檔案丟棄工作區對指定檔案的修改
取消 Stagegit 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 HEADHEAD:目前 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
提示
resetrestorerevert 用途不同: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:使用 RebaseFetch 後以 Rebase 整合本機提交
git pull --no-rebase--no-rebase:使用 MergeFetch 後以 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 upstreamPush 並建立 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 的最新版本