Git Cheat Sheet: Daily Commands, Branching, Undoing Mistakes
· Editorial team · 2 min read
cheatsheetgitversion-controlinterview
Setup and daily flow
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git clone <url>
git status
git add -p # stage chunks interactively
git commit -m "Add login validation"
git pull --rebase
git push -u origin my-branchBranching
| Command | Purpose | |---|---| | `git switch -c feature/x` | Create and switch to a branch | | `git switch main` | Switch branch | | `git branch -d feature/x` | Delete a merged branch | | `git merge feature/x` | Merge into current branch | | `git rebase main` | Replay your commits on top of main | | `git cherry-pick <sha>` | Apply one commit here |
Inspecting
| Command | Purpose | |---|---| | `git log --oneline --graph --all` | Compact history graph | | `git diff` / `git diff --staged` | Unstaged / staged changes | | `git show <sha>` | One commit's contents | | `git blame file` | Who last changed each line | | `git reflog` | Where HEAD has been (recovery tool) |
Undoing things
| Situation | Command | |---|---| | Discard edits to a file | `git restore file` | | Unstage a file | `git restore --staged file` | | Fix last commit message or add files | `git commit --amend` | | Undo a pushed commit safely | `git revert <sha>` (new inverse commit) | | Move branch back, keep changes | `git reset --soft HEAD~1` | | Move back and drop changes | `git reset --hard HEAD~1` (destructive) | | Recover a "lost" commit | `git reflog`, then `git switch -c rescue <sha>` |
Stash and tags
git stash push -m "wip"
git stash list
git stash pop
git tag -a v1.2.0 -m "Release 1.2.0"
git push origin v1.2.0Merge vs rebase
| | Merge | Rebase | |---|---|---| | History | Keeps branch shape, adds a merge commit | Linear history | | Rewrites commits | No | Yes | | Good for | Shared branches | Cleaning up your own local branch |
Stop spending an hour tailoring each resume — get a tailored resume and cover letter for this job in seconds.
Tailor my resumeEditorial team
Writes practical guides for Indian job seekers. Part of the HireStraight editorial team. Subscribe via RSS.