為什麼要用指令?
建議學習 Git 指令而非只依賴 GUI 工具(如 SourceTree),因為:
GUI 工具可能會在背景執行你不知道的操作(如自動 fetch)
了解底層指令能幫助理解 Git 的運作原理
指令操作更精確、可控
初始化與設定
指令 說明 git init建立新的本地 repo git clone <url>複製遠端 repo git config -l列出 git config 清單 git config --global user.name "Your Name"設定使用者名稱 git config --global user.email "your@email.com"設定使用者 email
日常操作
指令 說明 git status確認目前工作區狀態 git add .將所有變更加入暫存區 git add -A將所有變更(含刪除/更名)加入暫存區 git commit -m "message"提交暫存區的變更 git commit --amend修改最後一次 commit git log檢視提交紀錄
分支操作
指令 說明 git checkout -b <branch>建立並切換到新分支 git branch列出本地分支 git push -u origin <branch>推送分支到遠端 git push origin :<branch>刪除遠端分支
還原操作
指令 說明 git reset HEAD --hard還原所有未提交的變更 git revert <commit>建立新 commit 來還原指定版本 git rm --cached <file>從 Git 追蹤中移除檔案
標籤操作
指令 說明 git tag列出所有標籤 git tag <name>建立輕量標籤 git tag -a "<name>" -m "<message>"建立附註標籤 git show <tag>檢視標籤內容 git tag -d <name>刪除本地標籤 git push --delete origin <tag>刪除遠端標籤
推薦的 Alias 設定
編輯 ~/.gitconfig 加入以下設定:
[alias]
br = branch
cm = commit
co = checkout
df = diff
ft = fetch
l = log --oneline --graph
lg = log --all --graph --decorate --oneline
pl = pull
ps = push
st = status
ss = status -s
延伸閱讀