Git 使用指南
快速學習Git

Git 使用指南
本文檔提供 Git 版本控制系統的完整命令參考和使用指南。

一、新建代碼庫
初始化倉庫
# 在當前目錄新建一個 Git 代碼庫
git init
# 新建一個目錄,將其初始化為 Git 代碼庫
git init [project-name]
說明:
git init會在當前目錄創建.git隱藏目錄,用於存儲版本控制信息初始化後可以開始追蹤文件變更
克隆遠程倉庫
# 下載一個項目和它的整個代碼歷史
git clone [url]
# 克隆到指定目錄
git clone [url] [directory-name]
# 只克隆最新版本(淺克隆,減少下載量)
git clone --depth 1 [url]
# 克隆特定分支
git clone -b [branch-name] [url]
二、配置
Git 的設置文件為 .gitconfig,可以在用戶主目錄下(全局配置),也可以在項目目錄下(項目配置)。
查看配置
# 顯示當前的 Git 配置
git config --list
# 顯示全局配置
git config --global --list
# 顯示項目配置
git config --local --list
# 查看特定配置項
git config user.name
git config user.email
# 編輯 Git 配置文件
git config -e [--global]
用戶信息配置
# 設置提交代碼時的用戶信息(全局配置)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 設置項目級別配置(僅對當前項目生效)
git config user.name "Your Name"
git config user.email "your.email@example.com"
說明:
-global:全局配置,對所有 Git 倉庫生效不加
-global:項目級配置,僅對當前倉庫生效項目級配置會覆蓋全局配置
其他常用配置
# 設置默認編輯器
git config --global core.editor "vim"
# 設置默認分支名稱
git config --global init.defaultBranch main
# 設置默認合併工具
git config --global merge.tool vimdiff
# 啟用顏色輸出
git config --global color.ui true
# 設置自動轉換行結束符(Windows)
git config --global core.autocrlf true
# 設置自動轉換行結束符(macOS/Linux)
git config --global core.autocrlf input
# 設置推送行為(只推送當前分支)
git config --global push.default simple
# 設置別名(快捷命令)
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
範例:使用別名
# 設置別名後,可以使用
git st # 等同於 git status
git co # 等同於 git checkout
git br # 等同於 git branch
三、增加/刪除文件
添加文件到暫存區
# 添加指定文件到暫存區
git add [file1] [file2] ...
# 添加指定目錄到暫存區,包括子目錄
git add [dir]
# 添加當前目錄的所有文件到暫存區
git add .
# 添加當前目錄的所有文件(包括隱藏文件)
git add -A
# 交互式添加:對於同一個文件的多處變化,可以實現分次提交
git add -p
# 添加所有 .txt 文件
git add *.txt
# 添加所有 .js 文件(遞歸)
git add **/*.js
說明:
git add .:添加當前目錄及子目錄的所有變更git add -A:添加整個倉庫的所有變更(包括刪除的文件)git add -p:交互式添加,可以選擇性地暫存文件的部分變更
刪除和重命名
# 刪除工作區文件,並且將這次刪除放入暫存區
git rm [file1] [file2] ...
# 停止追蹤指定文件,但該文件會保留在工作區
git rm --cached [file]
# 強制刪除(適用於已修改但未提交的文件)
git rm -f [file]
# 改名文件,並且將這個改名放入暫存區
git mv [file-original] [file-renamed]
範例:
# 刪除文件
git rm old-file.txt
# 停止追蹤文件(但保留在工作區,適合添加到 .gitignore)
git rm --cached sensitive-data.txt
# 重命名文件
git mv old-name.txt new-name.txt
四、代碼提交
基本提交
# 提交暫存區到倉庫區
git commit -m [message]
# 提交暫存區的指定文件到倉庫區
git commit [file1] [file2] ... -m [message]
# 提交工作區自上次 commit 之後的變化,直接到倉庫區
git commit -a
# 提交時顯示所有 diff 信息
git commit -v
# 提交時不運行 pre-commit 和 commit-msg 鉤子
git commit --no-verify -m [message]
修改提交
# 使用一次新的 commit,替代上一次提交
# 如果代碼沒有任何新變化,則用來改寫上一次 commit 的提交信息
git commit --amend -m [message]
# 重做上一次 commit,並包括指定文件的新變化
git commit --amend [file1] [file2] ...
# 修改上次提交的作者信息
git commit --amend --author="Name <email@example.com>"
# 修改上次提交但保持原提交信息
git commit --amend --no-edit
說明:
-amend會修改最後一次提交,不會創建新的提交記錄如果已經推送到遠程,需要使用
git push --force(謹慎使用)
提交信息規範
好的提交信息應該:
第一行簡短描述(50 字以內)
空一行
詳細說明(可選)
使用祈使語氣(例如:Fix bug 而非 Fixed bug)
範例:
修復登入頁面的驗證錯誤
修復了當用戶名包含特殊字符時導致的 SQL 注入漏洞。
添加了輸入驗證和參數化查詢。
五、分支
查看分支
# 列出所有本地分支
git branch
# 列出所有遠程分支
git branch -r
# 列出所有本地分支和遠程分支
git branch -a
# 列出已合併的分支
git branch --merged
# 列出未合併的分支
git branch --no-merged
# 查看分支追蹤關係
git branch -vv
創建分支
# 新建一個分支,但依然停留在當前分支
git branch [branch-name]
# 新建一個分支,並切換到該分支
git checkout -b [branch]
# 新建一個分支,指向指定 commit
git branch [branch] [commit]
# 新建一個分支,與指定的遠程分支建立追蹤關係
git branch --track [branch] [remote-branch]
# 基於遠程分支創建本地分支
git checkout -b [local-branch] origin/[remote-branch]
範例:
# 創建功能分支
git checkout -b feature/user-authentication
# 基於特定提交創建分支
git branch hotfix-1.0.1 abc1234
# 從遠程分支創建本地分支
git checkout -b develop origin/develop
切換分支
# 切換到指定分支,並更新工作區
git checkout [branch-name]
# 切換到上一個分支
git checkout -
# 強制切換分支(丟棄本地變更)
git checkout -f [branch-name]
注意:Git 2.23+ 推薦使用 git switch 命令:
# 切換分支
git switch [branch-name]
# 創建並切換到新分支
git switch -c [branch-name]
# 切換到上一個分支
git switch -
分支追蹤
# 建立追蹤關係,在現有分支與指定的遠程分支之間
git branch --set-upstream [branch] [remote-branch]
# 為當前分支設置上游分支
git branch -u origin/[branch]
# 查看分支追蹤關係
git branch -vv
合併分支
# 合併指定分支到當前分支
git merge [branch]
# 合併分支(不使用快進模式,保留分支歷史)
git merge --no-ff [branch]
# 合併分支(僅快進,不創建合併提交)
git merge --ff-only [branch]
# 合併時添加提交信息
git merge [branch] -m "Merge branch 'feature'"
# 選擇一個 commit,合併進當前分支
git cherry-pick [commit]
# 選擇多個 commit
git cherry-pick [commit1] [commit2]
合併策略:
-no-ff:總是創建合併提交,保留分支歷史-ff-only:只在可以快進時合併-squash:將所有變更壓縮為一個提交
刪除分支
# 刪除分支(已合併的分支)
git branch -d [branch-name]
# 強制刪除分支(未合併的分支)
git branch -D [branch-name]
# 刪除遠程分支
git push origin --delete [branch-name]
git push origin :[branch-name] # 舊語法
# 刪除本地追蹤的遠程分支引用
git branch -dr [remote/branch]
六、標籤
標籤(Tag)用於標記特定的提交點,通常用於版本發布。
查看標籤
# 列出所有 tag
git tag
# 列出匹配特定模式的 tag
git tag -l "v1.8.5*"
# 查看 tag 信息
git show [tag]
# 查看 tag 列表及提交信息
git tag -n
創建標籤
# 新建一個 tag 在當前 commit
git tag [tag]
# 新建一個 tag 在指定 commit
git tag [tag] [commit]
# 創建帶註釋的標籤
git tag -a [tag] -m "Tag message"
# 創建帶註釋的標籤(在指定 commit)
git tag -a [tag] [commit] -m "Tag message"
# 使用 GPG 簽名標籤
git tag -s [tag] -m "Tag message"
範例:
# 創建版本標籤
git tag v1.0.0
# 創建帶註釋的版本標籤
git tag -a v1.0.0 -m "Release version 1.0.0"
# 在特定提交上創建標籤
git tag v1.0.0 abc1234
推送標籤
# 提交指定 tag
git push [remote] [tag]
# 提交所有 tag
git push [remote] --tags
# 推送所有標籤到遠程
git push origin --tags
刪除標籤
# 刪除本地 tag
git tag -d [tag]
# 刪除遠程 tag
git push origin --delete [tag]
git push origin :refs/tags/[tagName] # 舊語法
檢出標籤
# 新建一個分支,指向某個 tag
git checkout -b [branch] [tag]
# 檢出標籤(分離 HEAD 狀態)
git checkout [tag]
七、查看信息
狀態查詢
# 顯示有變更的文件
git status
# 顯示簡短狀態
git status -s
git status --short
# 顯示分支追蹤信息
git status -b
提交歷史
# 顯示當前分支的版本歷史
git log
# 顯示 commit 歷史,以及每次 commit 發生變更的文件
git log --stat
# 顯示簡化的一行格式
git log --oneline
# 顯示圖形化的分支合併歷史
git log --graph --oneline --all
# 顯示過去 5 次提交
git log -5 --pretty --oneline
# 搜索提交歷史,根據關鍵詞
git log -S [keyword]
# 顯示某個 commit 之後的所有變動,每個 commit 佔據一行
git log [tag] HEAD --pretty=format:%s
# 顯示某個 commit 之後的所有變動,其"提交說明"必須符合搜索條件
git log [tag] HEAD --grep feature
# 顯示某個文件的版本歷史,包括文件改名
git log --follow [file]
git whatchanged [file]
# 顯示指定文件相關的每一次 diff
git log -p [file]
# 顯示所有提交過的用戶,按提交次數排序
git shortlog -sn
# 顯示指定文件是什麼人在什麼時間修改過
git blame [file]
# 顯示當前分支的最近幾次提交
git reflog
自定義格式:
# 自定義日誌格式
git log --pretty=format:"%h - %an, %ar : %s"
# 常用格式選項
# %h - 縮短的提交哈希
# %an - 作者名字
# %ar - 相對時間(如 "2 hours ago")
# %s - 提交說明
# %ad - 作者日期
# %d - 引用名稱
差異比較
# 顯示暫存區和工作區的差異
git diff
# 顯示暫存區和上一個 commit 的差異
git diff --cached [file]
git diff --staged [file] # 同上
# 顯示工作區與當前分支最新 commit 之間的差異
git diff HEAD
# 顯示兩次提交之間的差異
git diff [first-branch]...[second-branch]
# 顯示兩個提交之間特定文件的差異
git diff [commit1] [commit2] -- [file]
# 顯示今天你寫了多少行代碼
git diff --shortstat "@{0 day ago}"
# 顯示統計信息
git diff --stat
查看提交詳情
# 顯示某次提交的元數據和內容變化
git show [commit]
# 顯示某次提交發生變化的文件
git show --name-only [commit]
# 顯示某次提交發生變化的文件及統計
git show --stat [commit]
# 顯示某次提交時,某個文件的內容
git show [commit]:[filename]
# 顯示最近一次提交
git show HEAD
八、遠程同步
遠程倉庫管理
# 顯示所有遠程倉庫
git remote -v
# 顯示某個遠程倉庫的信息
git remote show [remote]
# 增加一個新的遠程倉庫,並命名
git remote add [shortname] [url]
# 修改遠程倉庫地址
git remote set-url [remote] [url]
# 刪除遠程倉庫
git remote remove [remote]
git remote rm [remote] # 舊語法
# 重命名遠程倉庫
git remote rename [old-name] [new-name]
範例:
# 添加遠程倉庫
git remote add origin <https://github.com/user/repo.git>
# 查看遠程倉庫信息
git remote show origin
# 修改遠程倉庫地址
git remote set-url origin <https://github.com/user/new-repo.git>
獲取和拉取
# 下載遠程倉庫的所有變動(不自動合併)
git fetch [remote]
# 下載特定遠程分支的變動
git fetch [remote] [branch]
# 下載所有遠程倉庫的變動
git fetch --all
# 取回遠程倉庫的變化,並與本地分支合併
git pull [remote] [branch]
# 取回遠程倉庫的變化,並使用 rebase 合併
git pull --rebase [remote] [branch]
# 查看遠程分支變更(不拉取)
git fetch origin
git log HEAD..origin/main # 查看遠程分支的提交
說明:
git fetch:只下載變更,不自動合併git pull:下載並自動合併(等同於git fetch+git merge)
推送
# 上傳本地指定分支到遠程倉庫
git push [remote] [branch]
# 上傳當前分支到遠程倉庫(第一次推送需要設置上游)
git push -u [remote] [branch]
# 強行推送當前分支到遠程倉庫,即使有衝突(謹慎使用)
git push [remote] --force
git push [remote] -f # 簡寫
# 強制推送(更安全,會檢查遠程是否有變更)
git push [remote] --force-with-lease
# 推送所有分支到遠程倉庫
git push [remote] --all
# 推送所有標籤
git push [remote] --tags
# 刪除遠程分支
git push [remote] --delete [branch]
範例:
# 首次推送並設置上游
git push -u origin main
# 之後可以直接使用
git push
# 推送特定分支
git push origin feature-branch
# 強制推送(謹慎使用)
git push --force-with-lease origin main
九、撤銷
撤銷工作區變更
# 恢復暫存區的指定文件到工作區
git checkout [file]
# 恢復某個 commit 的指定文件到暫存區和工作區
git checkout [commit] [file]
# 恢復暫存區的所有文件到工作區
git checkout .
# 丟棄工作區的所有變更
git checkout -- .
# 使用 restore 命令(Git 2.23+)
git restore [file]
git restore .
git restore --staged [file] # 從暫存區移除
撤銷暫存區
# 重置暫存區的指定文件,與上一次 commit 保持一致,但工作區不變
git reset [file]
git restore --staged [file] # Git 2.23+
# 重置暫存區的所有文件
git reset
重置提交
# 重置當前分支的指針為指定 commit,同時重置暫存區,但工作區不變
git reset [commit]
# 重置當前分支的 HEAD 為指定 commit,同時重置暫存區和工作區
git reset --hard [commit]
# 重置到上一個提交(保留工作區)
git reset --soft HEAD~1
# 重置到上一個提交(保留暫存區)
git reset --mixed HEAD~1
git reset HEAD~1 # 同上
# 重置到上一個提交(完全重置)
git reset --hard HEAD~1
# 重置當前 HEAD 為指定 commit,但保持暫存區和工作區不變
git reset --keep [commit]
說明:
-soft:只移動 HEAD 指針,保留暫存區和工作區-mixed(默認):移動 HEAD,重置暫存區,保留工作區-hard:移動 HEAD,重置暫存區和工作區(危險操作)
撤銷提交(創建新提交)
# 新建一個 commit,用來撤銷指定 commit
# 後者的所有變化都將被前者抵消,並且應用到當前分支
git revert [commit]
# 撤銷多個提交
git revert [commit1] [commit2]
# 撤銷上一次提交
git revert HEAD
說明:
git revert會創建新的提交來撤銷變更,不會修改歷史適合已經推送到遠程的提交
git reset會修改歷史,不適合已推送的提交
暫存變更
# 暫時將未提交的變化移除,稍後再移入
git stash
# 暫存變更(包含未追蹤的文件)
git stash -u
git stash --include-untracked
# 暫存變更並添加描述
git stash save "Work in progress"
# 列出所有暫存的變更
git stash list
# 恢復最近一次暫存的變更
git stash pop
# 恢復指定暫存的變更(不刪除)
git stash apply [stash@{n}]
# 恢復指定暫存的變更(並刪除)
git stash pop [stash@{n}]
# 刪除指定暫存
git stash drop [stash@{n}]
# 清空所有暫存
git stash clear
# 查看暫存內容
git stash show
git stash show -p # 顯示完整差異
十、其他
歸檔
# 生成一個可供發佈的壓縮包
git archive
# 創建 tar 歸檔
git archive --format=tar --output=project.tar HEAD
# 創建 zip 歸檔
git archive --format=zip --output=project.zip HEAD
# 歸檔特定分支
git archive --format=zip --output=project.zip main
# 只歸檔特定目錄
git archive --format=zip --output=project.zip HEAD -- src/
清理
# 移除未追蹤的文件
git clean
# 顯示將要移除的文件(乾跑)
git clean -n
git clean --dry-run
# 移除未追蹤的文件
git clean -f
# 移除未追蹤的文件和目錄
git clean -fd
# 交互式清理
git clean -i
子模組(Submodule)
# 添加子模組
git submodule add [url] [path]
# 初始化子模組
git submodule init
# 更新子模組
git submodule update
# 克隆包含子模組的倉庫
git clone --recursive [url]
比較和合併工具
# 配置差異查看工具
git config --global diff.tool vimdiff
# 使用配置的差異工具查看變更
git difftool
# 配置合併工具
git config --global merge.tool vimdiff
# 使用合併工具解決衝突
git mergetool
鉤子(Hooks)
Git 鉤子存放在 .git/hooks/ 目錄:
# 查看可用鉤子
ls .git/hooks/
# 常用的鉤子:
# pre-commit - 提交前執行
# commit-msg - 檢查提交信息
# post-commit - 提交後執行
# pre-push - 推送前執行
常用工作流程
1. 基本工作流程
# 1. 查看狀態
git status
# 2. 添加文件到暫存區
git add .
# 3. 提交變更
git commit -m "描述變更"
# 4. 推送到遠程
git push
2. 功能開發流程
# 1. 創建功能分支
git checkout -b feature/new-feature
# 2. 開發和提交
git add .
git commit -m "實現新功能"
# 3. 推送分支
git push -u origin feature/new-feature
# 4. 合併到主分支
git checkout main
git pull origin main
git merge feature/new-feature
# 5. 推送到遠程
git push origin main
# 6. 刪除本地分支
git branch -d feature/new-feature
# 7. 刪除遠程分支
git push origin --delete feature/new-feature
3. 修復緊急 Bug
# 1. 從主分支創建熱修復分支
git checkout -b hotfix/bug-fix main
# 2. 修復並提交
git add .
git commit -m "修復緊急 bug"
# 3. 合併到主分支和開發分支
git checkout main
git merge hotfix/bug-fix
git push origin main
git checkout develop
git merge hotfix/bug-fix
git push origin develop
# 4. 刪除熱修復分支
git branch -d hotfix/bug-fix
4. 同步遠程變更
# 方法 1:使用 merge
git fetch origin
git merge origin/main
# 方法 2:使用 rebase(保持線性歷史)
git fetch origin
git rebase origin/main
# 方法 3:使用 pull
git pull origin main
最佳實踐
提交規範
提交信息規範:
使用清晰的提交信息
第一行簡短描述(50 字以內)
詳細說明另起一行
提交粒度:
每個提交應該是一個邏輯單元
不要將多個不相關的變更放在一次提交中
頻繁提交,小步前進
分支管理
分支命名:
feature/:新功能bugfix/或fix/:修復hotfix/:緊急修復release/:發布準備
分支策略:
主分支(main/master)保持穩定
功能分支從主分支創建
及時清理已合併的分支
安全性
不要強制推送主分支:
# 避免 git push --force origin main # 使用(更安全) git push --force-with-lease origin main保護敏感信息:
使用
.gitignore忽略敏感文件不要提交密碼、API 密鑰等
如果已提交,使用
git filter-branch或 BFG Repo-Cleaner 清理
性能優化
淺克隆:
git clone --depth 1 [url] # 只克隆最新版本清理:
# 清理未追蹤的文件 git clean -fd # 優化倉庫 git gc
.gitignore 文件
創建 .gitignore 忽略不需要版本控制的文件:
# 依賴目錄
node_modules/vendor/# 構建輸出
dist/build/*.class
*.jar
# 日誌文件
*.log
logs/# 環境變數文件
.env
.env.local
# IDE 配置
.idea/.vscode/*.swp
*.swo
# 系統文件
.DS_Store
Thumbs.db

