Skip to content
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
ActionCommand
See last 10 commits compactlygit log --oneline -10
Who changed a linegit blame -L 40,60 src/app.ts
Changed files vs maingit 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

MistakeSafe fix
Wrong message, not pushedgit commit --amend -m "new message"
Unstage a filegit restore --staged file.ts
Discard unstaged editsgit restore file.ts
Undo last commit, keep workgit reset --soft HEAD~1
Recover deleted branchgit reflog → git switch -c rescue <sha>

Golden rule: never push --force on shared branches. Use git revert <sha> to undo public commits, or --force-with-lease on 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.