Git 速查表
可搜尋、可列印的 Git 參考手冊——設定、分支、合併、變基、遠端儲存庫、暫存、標籤以及還原變更。免費。
設定與組態
10git config --global user.name "Jane Doe"
設定提交所附帶的名稱
git config --global user.email "jane@example.com"
設定提交所附帶的電子郵件
git config --global init.defaultBranch main
讓新儲存庫從 "main" 分支開始
git config --global core.editor "code --wait"
將 VS Code 設為預設 Git 編輯器
git config --global pull.rebase true
拉取時改用 rebase 而非合併
git config --global alias.co checkout
建立快捷別名 (git co)
git config --global --list
列出所有全域組態值
git config user.email
印出目前儲存庫的電子郵件
git config --global color.ui auto
啟用彩色命令列輸出
git help <command>
開啟指令的手冊頁面
建立與複製儲存庫
9git init
在目前資料夾初始化新儲存庫
git init my-project
在新目錄建立新儲存庫
git clone https://example.com/repo.git
透過 HTTPS 複製遠端儲存庫
git clone git@example.com:user/repo.git
透過 SSH 複製遠端儲存庫
git clone <url> my-dir
複製到指定目錄名稱
git clone --depth 1 <url>
淺層複製,只取最新提交
git clone --branch dev <url>
複製並簽出指定分支
git clone --recurse-submodules <url>
複製並初始化所有子模組
git remote add origin <url>
為現有本機儲存庫附加遠端
暫存與提交
11git add file.txt
暫存單一檔案以供下次提交
git add .
暫存目前目錄的所有變更
git add -A
暫存所有變更,含刪除
git add -p
互動式暫存選定的程式碼區塊
git commit -m "Add login form"
提交已暫存的變更並附訊息
git commit -am "Fix typo"
一步暫存已追蹤檔案並提交
git commit --amend
編輯最近一次提交
git commit --amend --no-edit
將暫存變更加入上次提交
git rm file.txt
移除檔案並暫存其刪除
git mv old.txt new.txt
重新命名或移動檔案並暫存
git reset file.txt
取消暫存檔案但保留其變更
分支
10git branch
列出所有本機分支
git branch -a
列出本機與遠端追蹤分支
git switch -c feature/login
建立新分支並切換過去
git switch main
切換到現有分支
git checkout -b hotfix
建立並切換分支的傳統方式
git branch -m old-name new-name
重新命名分支
git branch -d feature/login
刪除已合併的本機分支
git branch -D feature/login
強制刪除未合併的本機分支
git push origin --delete feature/login
刪除遠端的分支
git switch -
切換回前一個分支
合併與 rebase
10git merge feature/login
將分支合併到目前分支
git merge --no-ff feature/login
合併並一律建立合併提交
git merge --squash feature/login
將分支併為一個暫存變更
git merge --abort
取消有衝突的合併
git rebase main
將目前分支提交重放到 main
git rebase -i HEAD~3
互動式編輯最近 3 次提交
git rebase --continue
解決衝突後繼續 rebase
git rebase --abort
取消進行中的 rebase
git cherry-pick <hash>
將單一提交套用到目前分支
git mergetool
啟動工具解決合併衝突
遠端與同步
11git remote -v
列出已設定的遠端及其網址
git remote add upstream <url>
新增名為 upstream 的第二個遠端
git remote set-url origin <url>
變更現有遠端的網址
git fetch
下載遠端變更但不合併
git fetch --all --prune
抓取所有遠端並清除過時分支
git pull
抓取並整合遠端變更
git pull --rebase
拉取並將本機提交 rebase 於其上
git push
將本機提交上傳到遠端
git push -u origin main
推送並設定上游追蹤分支
git push --force-with-lease
安全強制推送,不覆蓋他人變更
git push origin --tags
將所有本機標籤推送到遠端
檢視與比較
10git status
顯示暫存、未暫存與未追蹤檔案
git status -s
以精簡格式顯示狀態
git diff
顯示相對於索引的未暫存變更
git diff --staged
顯示下次提交的已暫存變更
git diff main..feature
比較兩個分支
git diff HEAD~1 HEAD
比較最後一次提交與其父提交
git show <hash>
顯示單一提交的詳情與差異
git show HEAD:file.txt
顯示某提交中的檔案內容
git log --stat
顯示提交及變更檔案摘要
git shortlog -sn
依作者分組統計提交數
復原變更
10git restore file.txt
捨棄檔案中的未暫存變更
git restore --staged file.txt
取消暫存檔案但保留其變更
git restore --source=HEAD~1 file.txt
從較早的提交還原檔案
git checkout -- file.txt
捨棄本機變更的傳統方式
git reset --soft HEAD~1
復原上次提交,保留變更於暫存區
git reset --mixed HEAD~1
復原上次提交,變更保持未暫存
git reset --hard HEAD~1
復原上次提交並捨棄變更
git revert <hash>
建立新提交以還原某次提交
git clean -fd
刪除未追蹤的檔案與目錄
git clean -nd
預覽 clean 將移除的內容
暫存 (stash)
10git stash
儲存未提交變更並清空工作樹
git stash push -m "wip"
儲存變更並附描述訊息
git stash -u
儲存變更,含未追蹤檔案
git stash list
列出所有暫存的變更集
git stash show -p
顯示最新 stash 的差異
git stash apply
重新套用最新 stash 並保留
git stash pop
重新套用最新 stash 並移除
git stash apply stash@{2}
依索引重新套用指定 stash
git stash drop stash@{0}
刪除單一 stash 項目
git stash clear
刪除所有 stash 項目
標籤
9git tag
列出所有標籤
git tag v1.0.0
在 HEAD 建立輕量標籤
git tag -a v1.0.0 -m "Release 1.0.0"
建立帶訊息的附註標籤
git tag -a v1.0.0 <hash>
為某個過去提交加標籤
git show v1.0.0
顯示標籤的詳情
git push origin v1.0.0
將單一標籤推送到遠端
git push origin --tags
將所有本機標籤推送到遠端
git tag -d v1.0.0
在本機刪除標籤
git push origin --delete v1.0.0
刪除遠端的標籤
記錄與歷史
12git log
顯示完整提交歷史
git log --oneline
顯示每提交一行的精簡記錄
git log --oneline --graph --all
以 ASCII 圖呈現分支
git log -p
顯示提交歷史及差異
git log --author="Jane"
依作者篩選歷史
git log --since="2 weeks ago"
依日期範圍篩選歷史
git log --pretty=format:"%h %an %s"
自訂記錄輸出格式
git blame file.txt
顯示每行最後修改者
git reflog
顯示 HEAD 的歷史軌跡
git bisect start
開始二分搜尋有問題的提交
git bisect good <hash>
在 bisect 中標記提交為正常
git bisect bad
標記目前提交為有問題
沒有條目符合「:q」。
需要協助?
使用此工具時遇到問題?請告訴我們的團隊。