Skip to content

๐Ÿ“‹ Cheat Sheet โ€‹

Setup and Configuration โ€‹

CommandDescription
git initInitialize 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 --listView all configuration

Daily Operations โ€‹

CommandDescription
git statusView current status
git add <file>Add file to staging area
git add .Add all changes to staging area
git add -pInteractive add (select by chunk)
git commit -m "message"Commit staged content
git commit -am "message"Add tracked file changes and commit
git diffView working directory vs staging area diff
git diff --stagedView staging area vs last commit diff
git log --oneline --graph --allView nicely formatted commit history

Branch Operations โ€‹

CommandDescription
git branchList 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~nInteractive rebase on last n commits

Remote Operations โ€‹

CommandDescription
git remote -vView remote repositories
git remote add origin <url>Add a remote repository
git fetch originDownload remote updates (don't merge)
git pullDownload and merge remote updates
git pull --rebaseDownload and rebase
git pushPush to remote
git push -u origin <branch>Push and set tracking relationship
git push --tagsPush all tags

Undo and Fix โ€‹

CommandDescription
git restore <file>Discard working directory changes
git restore --staged <file>Unstage from staging area
git reset --soft HEAD~1Roll back commit (keep staging area and working directory)
git reset --mixed HEAD~1Roll back commit and staging area (keep working directory)
git reset --hard HEAD~1Roll back everything (โš ๏ธ irreversible)
git revert <commit>Create a new commit to undo the specified commit
git reflogView all HEAD movement records
git stashTemporarily save current changes
git stash popRestore and delete the most recent stash

Advanced Operations โ€‹

CommandDescription
git cherry-pick <commit>Precisely transplant a specific commit
git bisect startStart 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 gcGarbage collection and packing
git clean -fd โš ๏ธ Use git clean -n first to previewDelete untracked files and directories
git diff <a>..<b> --statView file change statistics between two commits
git shortlog -snView commit count per author

Final Advice for Beginners โ€‹

๐Ÿ’ก Tip:

  1. Commit often โ€” Commit after completing each small feature, don't accumulate. Smaller commits make it easier to locate problems.
  2. Write good commit messages โ€” Three months from now, you'll thank yourself for writing clear commit messages.
  3. Pull before push โ€” Build good habits, reduce conflicts.
  4. Don't be afraid of mistakes โ€” Almost everything in Git can be undone (except git push --force and git reset --hard). Experiment boldly!
  5. Learn to use reflog โ€” It's your "regret medicine." No matter how badly you mess up, reflog can help you recover.
  6. Practice, practice, practice โ€” Git is like swimming; you can't learn it from tutorials alone. You need to dive in and try it yourself.