第一篇文章指令:
git --version
git config --global user.name "你的姓名"
git config --global user.email "your.email@example.com"
git config --global core.editor "code --wait"
git config --global core.editor "subl -n -w"
git config --global core.editor vim
git config --global core.editor nano
git config --global core.autocrlf true
git config --global core.autocrlf input
git config --list
git config user.name
git config user.email
touch ~/.gitignore_global
echo. > %USERPROFILE%\.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
git status
第二篇文章指令:
mkdir my-first-project
cd my-first-project
git init
git clone https://github.com/username/repository-name.git
cd repository-name
echo "Hello, Git!" > README.txt
git status
git add README.txt
git add file1.txt file2.txt
git add .
git add *.txt
git add src/
git commit -m "新增 README 檔案"
git log
git log --oneline
git log --oneline -3
git log --stat
echo "這是我的第一個 Git 專案" >> README.txt
git diff
git commit -am "快速提交已追蹤檔案的變更"
touch .gitignore
git restore README.txt
git restore .
git restore --staged README.txt
git restore --staged .
git commit --amend -m "修正後的提交訊息"
git add forgotten-file.txt
git commit --amend --no-edit
git diff --staged
git diff README.txt
git log README.txt
git blame README.txt
mkdir src docs tests
touch src/main.py docs/README.md tests/test_main.py
cat > .gitignore << EOF
git reset --soft HEAD~1
git reset HEAD unwanted-file.txt
git ls-files --others --exclude-standard
git fsck
Git 基本命令番外篇:最常用指令完全解析
作為一位資深工程師,我發現新手學習 Git 時最困惑的就是「到底哪些指令最重要?」今天我們來聚焦於日常開發中最常用的 Git 指令,讓你快速掌握 80% 的使用場景。
檢查類指令(隨時使用)
git status – 你的 Git 羅盤
這是我每天使用最頻繁的指令,沒有之一。
git status
什麼時候用:
- 忘記目前在哪個分支
- 不確定哪些檔案被修改了
- 想知道有哪些檔案在暫存區
- 任何 Git 操作前的「安全檢查」
輸出範例:
On branch feature-login
Your branch is up to date with 'origin/feature-login'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: src/auth.js
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
new-feature.js
解讀技巧:
- 綠色文字:已暫存,準備提交
- 紅色文字:已修改但未暫存
- 灰色文字:未追蹤的新檔案
git log – 查看歷史記錄
# 基本用法
git log
# 簡潔一行顯示
git log --oneline
# 查看最近 5 次提交
git log -5
# 圖形化顯示分支
git log --oneline --graph
# 查看特定作者的提交
git log --author="張小明"
# 查看特定時間範圍
git log --since="2024-01-01" --until="2024-01-31"
實用技巧:
# 我最常用的組合
git log --oneline --graph --decorate -10
git diff – 查看變更內容
# 查看工作區與暫存區的差異
git diff
# 查看暫存區與最後一次提交的差異
git diff --staged
# 查看特定檔案的變更
git diff README.md
# 比較兩個提交之間的差異
git diff HEAD~1 HEAD
# 比較兩個分支的差異
git diff main feature-branch
基本操作指令(每日必用)
git add – 加入暫存區
# 加入特定檔案
git add README.md
# 加入多個檔案
git add file1.js file2.css
# 加入所有變更
git add .
# 加入所有 JavaScript 檔案
git add *.js
# 互動式加入(可選擇部分變更)
git add -i
# 加入部分變更(推薦給進階使用者)
git add -p
最佳實踐:
# 我建議的工作流程
git status # 先檢查狀態
git diff # 確認變更內容
git add 特定檔案 # 明確加入檔案
git status # 再次確認
git commit -m "..." # 提交
git commit – 提交變更
# 基本提交
git commit -m "修復使用者登入問題"
# 跳過暫存區直接提交已追蹤檔案
git commit -am "快速修復樣式問題"
# 修改最後一次提交訊息
git commit --amend -m "正確的提交訊息"
# 在最後一次提交中加入忘記的檔案
git add forgotten-file.js
git commit --amend --no-edit
提交訊息範本:
# 功能類
git commit -m "新增使用者註冊功能"
git commit -m "實作購物車結帳流程"
# 修復類
git commit -m "修復登入頁面 CSS 排版問題"
git commit -m "解決 API 回應超時問題"
# 重構類
git commit -m "重構使用者驗證邏輯"
git commit -m "優化資料庫查詢效能"
# 文件類
git commit -m "更新 API 文件"
git commit -m "新增安裝說明"
git restore – 復原變更
這是 Git 2.23 後推薦的復原指令,取代了複雜的 reset 用法。
# 復原工作區的變更(丟棄修改)
git restore README.md
git restore .
# 取消暫存(檔案回到工作區)
git restore --staged README.md
git restore --staged .
# 從特定提交復原檔案
git restore --source HEAD~1 README.md
使用場景:
# 情境 1:改壞了檔案,想要丟棄修改
git status # 看到 modified: app.js
git restore app.js # 回到最後一次提交的狀態
# 情境 2:不小心加入了不想提交的檔案
git add . # 全部加入暫存區
git status # 發現加入了不該加入的檔案
git restore --staged secret.key # 只取消特定檔案的暫存
分支操作指令(團隊必備)
git branch – 分支管理
# 查看所有本地分支
git branch
# 查看所有分支(包含遠端)
git branch -a
# 建立新分支
git branch feature-payment
# 刪除分支
git branch -d feature-old
# 強制刪除分支(有未合併的提交)
git branch -D feature-experimental
# 重新命名目前分支
git branch -m new-branch-name
git checkout / git switch – 分支切換
# 傳統方式(仍然可用)
git checkout main
git checkout -b feature-new
# 新推薦方式(Git 2.23+)
git switch main
git switch -c feature-new
# 切換到上一個分支
git switch -
git merge – 合併分支
# 基本合併
git switch main
git merge feature-payment
# 查看可合併的分支
git branch --merged
git branch --no-merged
遠端操作指令(協作必備)
git remote – 遠端倉庫管理
# 查看遠端倉庫
git remote -v
# 新增遠端倉庫
git remote add origin https://github.com/username/repo.git
# 移除遠端倉庫
git remote remove origin
# 重新命名遠端倉庫
git remote rename origin upstream
git push – 推送到遠端
# 推送到預設遠端分支
git push
# 首次推送並設定上游分支
git push -u origin feature-branch
# 推送所有分支
git push --all
# 推送標籤
git push --tags
# 強制推送(危險,需謹慎使用)
git push --force-with-lease
git pull – 從遠端拉取
# 基本拉取
git pull
# 指定遠端和分支
git pull origin main
# 拉取但不自動合併
git fetch
git merge origin/main
實用組合指令
日常開發流程
# 開始新功能
git switch main
git pull
git switch -c feature-user-profile
# 開發過程中
git add .
git commit -m "實作使用者個人資料頁面"
git push -u origin feature-user-profile
# 完成功能
git switch main
git pull
git merge feature-user-profile
git push
git branch -d feature-user-profile
快速修復流程
# 發現 bug 需要緊急修復
git switch main
git pull
git switch -c hotfix-login-error
# 修復完成
git add .
git commit -m "修復登入驗證錯誤"
git switch main
git merge hotfix-login-error
git push
git branch -d hotfix-login-error
救援指令(犯錯時使用)
git reflog – 找回遺失的提交
# 查看所有操作記錄
git reflog
# 回到特定操作
git reset --hard HEAD@{2}
git reset – 回到過去
# 軟重設:保留變更在暫存區
git reset --soft HEAD~1
# 混合重設:保留變更在工作區(預設)
git reset HEAD~1
# 硬重設:丟棄所有變更(危險)
git reset --hard HEAD~1
設定別名讓指令更簡短
# 設定常用別名
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.sw switch
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.ps push
git config --global alias.pl pull
git config --global alias.lg "log --oneline --graph --decorate"
# 使用別名
git st # 相當於 git status
git lg # 相當於 git log --oneline --graph --decorate
我的日常 Git 工作流程
作為一個資深工程師,我每天的 Git 使用模式大致如下:
# 早上開始工作
git st # 檢查狀態
git sw main # 切換到主分支
git pl # 拉取最新程式碼
# 開始新功能
git sw -c feature-new-api # 建立並切換到功能分支
# 開發過程中(重複多次)
git st # 檢查狀態
git diff # 查看變更
git add src/api.js # 加入特定檔案
git cm "實作新的 API 端點" # 提交
# 功能完成
git ps -u origin feature-new-api # 推送到遠端
# 然後在 GitHub/GitLab 建立 Pull Request
小結
這些指令涵蓋了日常開發中 90% 的使用場景:
每日必用(前5名):
git status
– 隨時檢查狀態git add
– 暫存變更git commit
– 提交變更git push
– 推送到遠端git pull
– 拉取遠端變更
週常用:
git log
– 查看歷史git diff
– 查看變更內容git switch/checkout
– 切換分支git merge
– 合併分支
救援用:
git restore
– 復原變更git reset
– 重設提交git reflog
– 找回遺失的提交
記住,熟練使用 Git 的關鍵不是記住所有指令,而是理解 Git 的工作原理,然後在需要時查找對應的指令。建議你把這篇文章收藏起來,作為日常開發的快速參考手冊!