DevOps3 min read
Git Commands Cheatsheet: 40 Workflows That Save You
Essential Git cheatsheet with 40 commands for branching, rebasing, stashing, undoing mistakes, and clean team workflows with real examples.
By IvyIvy TeamPublished Updated
#git#github#workflow#cli#version-control
First-Time Setup
git config --global user.name "IvyIvy Developer"
git config --global user.email "hello@ivyivy.com"
git config --global init.defaultBranch main
git config --global pull.rebase true # cleaner history
git config --global rerere.enabled true # reuse conflict resolutions
Daily Workflow
git status -sb # short, branch-aware status
git add -p # stage hunks interactively
git commit -m "feat(search): add fuzzy filter"
git pull --rebase --autostash # update without merge bubbles
git push -u origin feat/search # first push sets upstream
| Action | Command |
|---|---|
| See last 10 commits compactly | git log --oneline -10 |
| Who changed a line | git blame -L 40,60 src/app.ts |
| Changed files vs main | git diff --stat main...HEAD |
Branching Like a Team
git switch -c feat/json-formatter # create + switch
git fetch --prune # drop deleted remote branches
git branch -vv # tracking info
git switch main && git merge --no-ff feat/json-formatter
Name branches feat/, fix/, chore/, docs/ — CI and changelogs can parse them automatically.
Undo Anything Safely
| Mistake | Safe fix |
|---|---|
| Wrong message, not pushed | git commit --amend -m "new message" |
| Unstage a file | git restore --staged file.ts |
| Discard unstaged edits | git restore file.ts |
| Undo last commit, keep work | git reset --soft HEAD~1 |
| Recover deleted branch | git reflog → git switch -c rescue <sha> |
Golden rule: never
push --forceon shared branches. Usegit revert <sha>to undo public commits, or--force-with-leaseon your own PR branch.
Stash & Worktrees
git stash push -m "wip: half-done filter" # park work
git stash list && git stash show -p stash@{0}
git stash pop # restore + drop
git worktree add ../hotfix main # second checkout, no clone
Rebase Without Fear
git fetch origin
git rebase -i origin/main # squash, reword, drop
# conflict? edit files, then:
git add . && git rebase --continue
# abort anytime:
git rebase --abort
Rebase private feature branches; merge (--no-ff) long-lived branches so history shows intent.
Tags & Releases
git tag -a v1.4.0 -m "JSON formatter launch"
git push origin v1.4.0
gh release create v1.4.0 --generate-notes # GitHub CLI
.gitignore That Works
node_modules/
dist/
.env
.env.local
.DS_Store
*.log
Commit a .gitattributes too (* text=auto eol=lf) so Windows/macOS line endings stop polluting diffs.