Git Basic Commands
June 3, 2026 · View on GitHub
English | 中文
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>
Recommended Practice Sequence
- Use
git statusto understand the current state. - Use
git addandgit committo complete one local commit. - Use
git switch -cto create a task branch. - Use
git diffto check changes before committing. - Use
git pushto push the branch and create a PR. - Use
git restoreto undo local changes.
Don't Rush to Use High-Risk Commands
Use with caution during the novice stage:
git reset --hard
git push --force
git clean -fd
These commands may directly discard local changes or rewrite remote history.
If you just want to undo ordinary workspace changes, prioritize learning Undo Anything.