๐ Cheat Sheet โ
Setup and Configuration โ
| Command | Description |
|---|---|
| git init | Initialize a new repository |
| git clone <url> | Clone a remote repository |
| git config --global user.name "name" | Set global username |
| git config --global user.email "email" | Set global email |
| git config --list | View all configuration |
Daily Operations โ
| Command | Description |
|---|---|
| git status | View current status |
| git add <file> | Add file to staging area |
| git add . | Add all changes to staging area |
| git add -p | Interactive add (select by chunk) |
| git commit -m "message" | Commit staged content |
| git commit -am "message" | Add tracked file changes and commit |
| git diff | View working directory vs staging area diff |
| git diff --staged | View staging area vs last commit diff |
| git log --oneline --graph --all | View nicely formatted commit history |
Branch Operations โ
| Command | Description |
|---|---|
| git branch | List local branches |
| git branch <name> | Create a new branch |
| git branch -d <name> | Delete a merged branch |
| git branch -D <name> | Force delete a branch |
| git switch <branch> | Switch to a branch |
| git switch -c <name> | Create and switch to a new branch |
| git merge <branch> | Merge specified branch into current branch |
| git merge --no-ff <branch> | Force create a merge commit |
| git rebase <branch> | Rebase onto specified branch |
| git rebase -i HEAD~n | Interactive rebase on last n commits |
Remote Operations โ
| Command | Description |
|---|---|
| git remote -v | View remote repositories |
| git remote add origin <url> | Add a remote repository |
| git fetch origin | Download remote updates (don't merge) |
| git pull | Download and merge remote updates |
| git pull --rebase | Download and rebase |
| git push | Push to remote |
| git push -u origin <branch> | Push and set tracking relationship |
| git push --tags | Push all tags |
Undo and Fix โ
| Command | Description |
|---|---|
| git restore <file> | Discard working directory changes |
| git restore --staged <file> | Unstage from staging area |
| git reset --soft HEAD~1 | Roll back commit (keep staging area and working directory) |
| git reset --mixed HEAD~1 | Roll back commit and staging area (keep working directory) |
| git reset --hard HEAD~1 | Roll back everything (โ ๏ธ irreversible) |
| git revert <commit> | Create a new commit to undo the specified commit |
| git reflog | View all HEAD movement records |
| git stash | Temporarily save current changes |
| git stash pop | Restore and delete the most recent stash |
Advanced Operations โ
| Command | Description |
|---|---|
| git cherry-pick <commit> | Precisely transplant a specific commit |
| git bisect start | Start binary search for bug hunting |
| git blame <file> | View the last modifier of each line in a file |
| git grep <pattern> | Search within code |
| git tag -a <tag> -m "..." | Create an annotated tag |
| git submodule add <url> | Add a submodule |
| git gc | Garbage collection and packing |
| git clean -fd โ ๏ธ Use git clean -n first to preview | Delete untracked files and directories |
| git diff <a>..<b> --stat | View file change statistics between two commits |
| git shortlog -sn | View commit count per author |
Final Advice for Beginners โ
๐ก Tip:
- Commit often โ Commit after completing each small feature, don't accumulate. Smaller commits make it easier to locate problems.
- Write good commit messages โ Three months from now, you'll thank yourself for writing clear commit messages.
- Pull before push โ Build good habits, reduce conflicts.
- Don't be afraid of mistakes โ Almost everything in Git can be undone (except
git push --forceandgit reset --hard). Experiment boldly!- Learn to use reflog โ It's your "regret medicine." No matter how badly you mess up, reflog can help you recover.
- Practice, practice, practice โ Git is like swimming; you can't learn it from tutorials alone. You need to dive in and try it yourself.