Git Basic Commands
May 28, 2026 · View on GitHub
Create or clone
git init
git clone <url>
Check status
git status
git log --oneline --decorate
Stage and commit
git add <file>
git commit -m "docs: add git basics"
Sync
git fetch
git pull --rebase
git push
Branch
git switch -c feat/my-task
git switch main
git branch
Undo
git restore <file>
git restore --staged <file>
git revert <commit-sha>
建议练习顺序
- 用
git status看懂当前状态 - 用
git add和git commit完成一次本地提交 - 用
git switch -c创建任务分支 - 用
git diff检查提交前改动 - 用
git push推送分支并创建 PR - 用
git restore撤销本地改动
高风险命令先别急着用
新手阶段谨慎使用:
git reset --hard
git push --force
git clean -fd
这些命令可能直接丢弃本地改动或改写远端历史。
如果只是想撤销普通工作区改动,优先学习 Undo Anything。