Cheatsheet
Git Cheatsheet
Every Git command you actually type. Plus the recovery commands you need when a rebase goes wrong.
Git has hundreds of commands and porcelain options. About twenty of them are what you use daily. This cheatsheet covers the daily set plus the recovery commands you need when something breaks.
Daily workflow
`git status`, `git add .`, `git commit -m 'msg'`, `git push`, `git pull`.
`git checkout -b feature/x` to branch. `git switch main` to move (newer alias).
`git log --oneline --graph` to see history compactly.
Branching and merging
`git merge feature/x` for merge commits. `git rebase main` to replay commits on top of main.
Squash merge: `git merge --squash feature/x` then `git commit`.
Fast-forward vs no-ff: `git merge --no-ff` forces a merge commit even when linear.
Fixing mistakes
The most-used recovery commands.
- `git commit --amend` — fix the last commit message or add forgotten files
- `git reset --soft HEAD~1` — undo commit, keep changes staged
- `git reset --hard HEAD~1` — undo commit and changes (destructive)
- `git restore --staged file` — unstage a file
- `git restore file` — discard changes to a file (destructive)
- `git reflog` — see every HEAD movement, recover 'lost' commits
- `git revert <hash>` — safely undo a public commit
Stash and cherry-pick
`git stash` — save uncommitted work. `git stash pop` — restore.
`git cherry-pick <hash>` — apply a commit from another branch.
Rewriting history (interactive rebase)
`git rebase -i HEAD~5` — interactively edit last 5 commits (reorder, squash, edit messages). Never rebase commits already pushed to a shared branch.
Frequently asked questions
Merge or rebase?
Team preference. Merge preserves history; rebase creates linear history. Never rebase shared branches.
I lost commits after a bad reset — can I recover?
Yes, use `git reflog`. It logs every HEAD movement for about 90 days. Find the commit hash and `git reset --hard <hash>` to it.
checkout vs switch vs restore?
Newer Git split `checkout` into two: `switch` for branches, `restore` for file changes. `checkout` still works for both.
How do I clean untracked files?
`git clean -n` (dry run) to see what would be removed, then `git clean -fd` to remove untracked files and directories.
What is the difference between git pull and git fetch?
`fetch` downloads. `pull` = `fetch` + `merge` (or `rebase` if configured).
Keep exploring
Made for exam season
Pass that exam.
Turn your notes into flashcards and quizzes in seconds. Study smarter — start free today.
