📚 Git 速查表
開發人員完整的 Git 命令參考。搜索並複製 git 命令,包括提交、推送、拉取、分支、合併等。
💡 使用提示: 點擊命令旁邊的"複製"按鈕快速複製,點擊類別標題可展開/摺疊內容。
⚙️ 配置
▼
git config --global user.name "[name]"
設置全局用户名
git config --global user.email "[email]"
設置全局郵箱
git config --global core.editor "[editor]"
設置默認編輯器
git config --global color.ui true
啓用彩色輸出
git config --global alias.co checkout
設置命令別名 (checkout → co)
🚀 創建倉庫
▼
git init
初始化新的 Git 倉庫
git clone [url]
克隆遠程倉庫到本地
git clone -b [branch] [url]
克隆指定分支
💾 基本提交
▼
git status
查看工作目錄狀態
git add [file]
添加文件到暫存區
git add .
添加所有修改的文件
git commit -m "[message]"
提交暫存的內容
git commit -am "[message]"
提交所有跟蹤的更改
git commit --amend
修改最後一次提交
git commit --amend --no-edit
修改提交但不改變消息
🌿 分支管理
▼
git branch
查看本地分支列表
git branch -a
查看所有分支(包括遠程)
git branch [branch-name]
創建新分支
git checkout [branch-name]
切換到指定分支
git checkout -b [branch-name]
創建並切換到新分支
git branch -d [branch-name]
刪除已合併的分支
git branch -D [branch-name]
強制刪除分支
git branch -m [old-name] [new-name]
重命名分支
☁️ 遠程操作
▼
git remote -v
查看遠程倉庫信息
git remote add origin [url]
添加遠程倉庫
git pull [remote] [branch]
拉取遠程更新
git push [remote] [branch]
推送本地分支
git push -f [remote] [branch]
強制推送(覆蓋遠程)
git fetch [remote]
獲取遠程更新(不合併)
git remote remove [remote]
刪除遠程倉庫
🔀 合併與變基
▼
git merge [branch]
合併指定分支到當前分支
git merge --no-ff [branch]
合併但創建合併提交
git rebase [branch]
變基到指定分支
git rebase --continue
繼續變基操作
git rebase --abort
中止變基操作
↩️ 撤銷與回滾
▼
git reset HEAD~1
撤銷最後一次提交(保留更改)
git reset HEAD~[N]
撤銷最後 N 次提交
git reset --hard HEAD~1
撤銷提交並丟棄更改
git reset --hard [commit-hash]
回滾到指定提交
git checkout -- [file]
丟棄本地文件更改
git revert [commit-hash]
創建新提交來撤銷指定提交
git clean -fd
刪除未跟蹤的文件和目錄
🔍 查看歷史
▼
git log
查看提交歷史
git log --oneline
簡潔的單行歷史
git log --graph --oneline
圖形化分支歷史
git log --stat
查看提交統計
git diff
查看工作目錄差異
git diff --staged
查看暫存區差異
git show [commit-hash]
查看指定提交詳情
🏷️ 標籤管理
▼
git tag
查看標籤列表
git tag [tag-name]
創建輕量標籤
git tag -a [tag-name] -m "[message]"
創建附註標籤
git push [remote] [tag-name]
推送標籤到遠程
git push [remote] --tags
推送所有標籤
git tag -d [tag-name]
刪除本地標籤
📦 儲藏與暫存
▼
git stash
儲藏當前更改
git stash save "[message]"
帶消息儲藏
git stash list
查看儲藏列表
git stash apply
恢復儲藏(不刪除)
git stash pop
恢復儲藏並刪除
git stash drop
刪除最新儲藏
git stash clear
清空所有儲藏
🧩 子模塊
▼
git submodule add [url] [path]
添加子模塊
git submodule init
初始化子模塊
git submodule update
更新子模塊
⚡ 高級技巧
▼
git cherry-pick [commit-hash]
挑選並應用指定提交
git bisect start [bad] [good]
二分查找定位問題
git blame [file]
查看文件修改歷史
git reflog
查看所有操作記錄
git clean -xdf
徹底清理工作目錄(謹慎使用)
git gc --aggressive
壓縮並優化倉庫