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>

建议练习顺序

  1. git status 看懂当前状态
  2. git addgit commit 完成一次本地提交
  3. git switch -c 创建任务分支
  4. git diff 检查提交前改动
  5. git push 推送分支并创建 PR
  6. git restore 撤销本地改动

高风险命令先别急着用

新手阶段谨慎使用:

git reset --hard
git push --force
git clean -fd

这些命令可能直接丢弃本地改动或改写远端历史。

如果只是想撤销普通工作区改动,优先学习 Undo Anything

延伸阅读