Git 常用命令(日常 12 條)
提交、回退、查看歷史最常用的 Git 命令。
範本正文
# 狀態與提交 git status # 看當前改動 git add -p # 逐塊挑選要提交的改動(推薦) git commit -m "fix: 修復登錄超時" # 歷史與差異 git log --oneline --graph -20 # 看最近 20 條提交 git diff --staged # 看已暫存的內容 git show <commit> # 看某次提交改了什麼 # 撤銷 git restore <file> # 丟棄工作區改動 git restore --staged <file> # 取消暫存(保留改動) git reset --soft HEAD~1 # 撤銷上一次提交,改動保留 # 分支 git switch -c feat/xxx # 新建並切換分支 git switch main # 切回主分支 git fetch --prune # 拉取並清理已刪除的遠程分支
怎麼用
貼在備忘錄裡,前兩周基本天天用得到。
使用要點
- 提交前先 git diff --staged 看一遍,能避免提交調試代碼。
- 已經 push 的提交不要用 reset 改寫,用 revert 更安全。