Git 常用命令速查表
提交、分支、撤銷、遠程等九類命令,含可直接套用的示例
共 69 條命令
git init配置與初始化在當前目錄創建倉庫
git init
git clone <url>配置與初始化克隆遠程倉庫到本地
git clone git@github.com:user/repo.git
git config --global user.name "Ada"配置與初始化設置提交者姓名,一次配置長期生效
git config --global user.name "Ada"
git config --global user.email "ada@example.com"配置與初始化設置提交者郵箱,提交記錄裡會帶上
git config --global user.email "ada@example.com"
git config --global alias.st status配置與初始化自定義命令別名,少敲幾個字
git config --global alias.lg "log --oneline --graph"
git remote add origin <url>配置與初始化給本地倉庫關聯遠程地址
git remote add origin git@github.com:user/repo.git
git status提交與暫存查看工作區與暫存區當前狀態
git status -sb
git add <file>提交與暫存把指定文件的改動放入暫存區
git add src/app.vue
git add -p提交與暫存交互式挑選代碼塊暫存,適合拆細提交
git add -p
git commit -m "msg"提交與暫存提交暫存區內容
git commit -m "fix: 修正登录跳转"
git commit --amend提交與暫存改寫最近一次提交(信息或內容)
git commit --amend --no-edit
git restore --staged <file>提交與暫存把文件移出暫存區但保留改動
git restore --staged src/app.vue
git rm --cached <file>提交與暫存停止跟蹤但保留本地文件(補 .gitignore 後常用)
git rm --cached .env
git mv <old> <new>提交與暫存重命名文件並讓 Git 記錄這次改名
git mv old.ts new.ts
git branch分支與合併列出本地分支,帶 * 的是當前分支
git branch -vv
git switch -c <name>分支與合併新建並切換到新分支
git switch -c feature/login
git switch <name>分支與合併切換到已有分支
git switch main
git merge <branch>分支與合併把目標分支合併進當前分支
git merge feature/login
git merge --no-ff <branch>分支與合併禁用快進,保留合併提交便於回溯
git merge --no-ff feature/login
git merge --squash <branch>分支與合併把分支上的多個提交壓成一個
git merge --squash feature/login
git rebase <branch>分支與合併把自己的提交搬到目標分支之後,歷史更線性
git rebase main
git rebase -i HEAD~3分支與合併交互式整理最近三次提交:合併、改序、改信息
git rebase -i HEAD~3
git rebase --onto <new> <old> <branch>分支與合併把一段提交整體搬到新基點
git rebase --onto main old-base topic
git cherry-pick <commit>分支與合併把某個提交摘到當前分支
git cherry-pick a1b2c3d
git branch -d <name>分支與合併刪除已合併的分支,-D 可強制刪除
git branch -d feature/login
git restore <file>撤銷與回滾丟棄工作區改動(改動未提交時最常用)
git restore src/app.vue
git reset --soft HEAD~1撤銷與回滾撤銷最近一次提交,改動留在暫存區
git reset --soft HEAD~1
git reset HEAD~1撤銷與回滾撤銷提交併把改動退回工作區
git reset HEAD~1
git reset --hard HEAD~1撤銷與回滾徹底丟棄最近一次提交與改動(危險,不可恢復)
git reset --hard HEAD~1
git revert <commit>撤銷與回滾生成一個反向提交來抵消已有提交,不改寫歷史
git revert a1b2c3d
git revert -m 1 <merge-commit>撤銷與回滾撤銷一次合併提交,-m 指定保留哪一側
git revert -m 1 8f3c2a1
git checkout <commit> -- <file>撤銷與回滾取回某個歷史版本裡的單個文件
git checkout HEAD~2 -- config/nginx.conf
git reflog撤銷與回滾查看 HEAD 移動歷史,誤刪分支與提交靠它找回
git reflog
git clean -fd撤銷與回滾刪除未跟蹤的文件與目錄(危險,先加 -n 試跑)
git clean -fdn
git stash暫存與清理把當前改動臨時收起,工作區回到乾淨狀態
git stash push -m "wip: 登录页"
git stash list暫存與清理查看所有暫存條目
git stash list
git stash pop暫存與清理恢復最近一次暫存並從列表移除
git stash pop
git stash apply stash@{2}暫存與清理恢復指定暫存但保留列表條目
git stash apply stash@{2}git stash drop暫存與清理丟棄最近一次暫存
git stash drop
git fetch --prune遠程協作拉取遠端更新並清理已被刪除的遠程分支
git fetch --all --prune
git pull --rebase遠程協作以變基方式拉取,避免多餘的合併提交
git pull --rebase
git push -u origin <branch>遠程協作推送新分支並建立跟蹤關係
git push -u origin feature/login
git push --force-with-lease遠程協作變基後強推,但遠端有新提交時會拒絕
git push --force-with-lease
git push origin --delete <branch>遠程協作刪除遠程分支
git push origin --delete feature/login
git remote -v遠程協作查看遠程倉庫地址
git remote -v
git remote set-url origin <url>遠程協作修改遠程倉庫地址,換倉庫或換協議時用
git remote set-url origin git@github.com:user/repo.git
git log --oneline --graph查看歷史單行圖形化歷史,快速看清分支結構
git log --oneline --graph --decorate --all
git log -S "text"查看歷史查找引入或刪除過某段文本的提交
git log -S "checkToken" --oneline
git show <commit>查看歷史查看某次提交的詳情與改動
git show a1b2c3d --stat
git diff查看歷史工作區與暫存區的差異
git diff --stat
git diff --staged查看歷史已暫存內容與上次提交的差異
git diff --staged
git blame <file>查看歷史逐行顯示最後修改人與提交,定位責任代碼
git blame -L 20,40 src/app.vue
git shortlog -sn查看歷史按作者統計提交數量
git shortlog -sn --no-merges
git bisect start查看歷史二分查找引入 bug 的提交:標記好與壞後逐步逼近
git bisect start # git bisect bad / git bisect good <commit>
git tag -a v1.0.0 -m "release"標籤與發佈創建帶說明的附註標籤
git tag -a v1.0.0 -m "first release"
git tag標籤與發佈列出全部標籤
git tag -l "v1.*"
git push origin <tag>標籤與發佈推送單個標籤到遠端
git push origin v1.0.0
git worktree add ../hotfix main排錯與高級一份倉庫檢出多個工作目錄,可並行開發
git worktree add ../repo-hotfix main
git submodule update --init --recursive排錯與高級初始化並拉取子模塊
git submodule update --init --recursive
git archive --format=zip HEAD -o src.zip排錯與高級導出某個提交的快照,不含 .git
git archive --format=zip HEAD -o src.zip
git gc排錯與高級整理並壓縮倉庫,倉庫變大時可跑
git gc --aggressive --prune=now
git fsck排錯與高級檢查對象完整性,排查倉庫損壞
git fsck --full
git range-diff排錯與高級對比兩次變基前後的差異,確認改動沒丟
git range-diff main old-topic topic
git format-patch <range>排錯與高級把提交導出為補丁文件,便於郵件或其他倉庫應用
git format-patch HEAD~3
git apply <patch>排錯與高級應用補丁文件,不產生提交
git apply --check fix.patch
git filter-repo --path <dir>排錯與高級改寫歷史以刪除文件或目錄,需先裝 git-filter-repo
git filter-repo --path secrets/ --invert-paths
這個工具不好用,或者遇到 bug?
去回報