π The Git Gud Cheatsheet β
Your lightning-fast reference for mastering the daily Git workflow. Click any action below to reveal the magic behind it.
π± Start a new branch from latest main β
π‘ Show me the commands
Perfect for: Starting any new feature, bugfix, or task.
Step 1: Switch to main
git checkout mainStep 2: Sync with remote
git reset --hard
git pullStep 3: Create new branch
git checkout -b feature/GG-12_storyNameModern alternative:
git switch -c feature/GG-12_storyNameWhat happens:
- β Switch to main branch
- π§Ή Discard any local changes and sync with remote
- β¬οΈ Pull latest updates
- π― Create and switch to new feature branch
π Squash multiple commits into one β
π‘ Show me the commands
Perfect for: Before opening a PR or after making several "work-in-progress" commits.
Command:
git rebase -i HEAD~3What happens: Opens interactive rebase to combine the last 3 commits. Leave the first one as pick (keeps the commit as-is), and set the next ones to squash (combines with the previous commit and lets you edit the message) or fixup (combines with the previous commit without editing the message) for commits you want to merge.
π Push branch for the first time β
π‘ Show me the commands
Perfect for: Sharing your branch with the team and opening a pull request.
git push -u origin feature/GG-12_storyNameWhat happens:
- π€ Pushes your branch to the remote repository
- π The
-uflag (shorthand for--set-upstream) sets up tracking so futuregit pushcommands work without specifying the remote
π Update branch with latest main β
π‘ Show me the commands
Perfect for: Before finalizing your PR or when main has new changes you need.
Rebase approach:
git pull --rebase origin mainWhat happens: Fetches the latest main and replays your commits on top of it, maintaining a linear history.
Merge approach:
git pull --merge origin mainWhat happens: Fetches the latest main and merge on your branch with a merge commit, turning your history into spaghetti.
πͺ Force-push safely after rewriting history β
π‘ Show me the commands
Perfect for: After squashing commits, rebasing, or any history rewriting operation.
Safe force-push:
git push --force-with-leasePro workflow combo:
Step 1: Squash commits
git rebase -i HEAD~3Step 2: Update with main
git pull --rebase origin mainStep 3: Force-push safely
git push --force-with-lease --force-if-includesWhat happens: Safely force-pushes your rewritten branch. The --force-with-lease --force-if-includes flags prevents accidentally overwriting changes from teammates. π‘ Make it faster to type with the git pf alias.
π Discard local changes and match remote β
π‘ Show me the commands
Perfect for: When your local branch is messed up and you want to match what's on the remote.
Step 1: Fetch latest
git fetch originStep 2: Reset to remote
git reset --hard origin/feature/GG-12_storyNameWhat happens:
- π₯ Fetches latest remote information
- π₯ Brutally resets your local branch to match the remote version
- β οΈ Warning: This destroys any uncommitted local changes!
π Switch to an existing branch β
π‘ Show me the commands
Perfect for: Switching between different feature branches or back to main.
Classic command:
git checkout branch-nameModern alternative:
git switch branch-nameWhat happens: Changes your working directory to the specified branch.
π Check status of working directory β
π‘ Show me the commands
Perfect for: Checking what files are modified, staged, or untracked.
Command:
git statusWhat happens: Shows you:
- π Modified files
- β Staged files ready for commit
- β Untracked files
- π·οΈ Current branch information
πΎ Stage and commit changes β
π‘ Show me the commands
Perfect for: Saving your work as a commit.
Stage all changes:
git add .Stage specific files:
git add file1.js file2.cssCommit with message:
git commit -m "feat: add user authentication"Alternative: Stage and commit in one go:
git commit -am "feat: add user authentication"What happens:
- π¦
git addstages files for the next commit - β¨
git commitcreates a new commit with your staged changes β οΈ-amis faster but won't stage newly created files. ``
π View commit history β
π‘ Show me the commands
Perfect for: Reviewing recent work or finding specific commits.
Simple one-line view:
git log --onelineWith visual graph:
git log --oneline --graphWhat happens: Shows a condensed view of your commit history with one line per commit.
β©οΈ Undo last commit but keep changes β
π‘ Show me the commands
Perfect for: When you want to modify your last commit or split it into multiple commits.
Command:
git reset --soft HEAD~1What happens: Removes the last commit but keeps all the changes staged and ready to commit again.
ποΈ Discard uncommitted changes β
π‘ Show me the commands
Perfect for: When you want to throw away all current modifications and start fresh.
Discard all changes:
git reset --hardDiscard specific file:
git checkout HEAD -- filename.jsWhat happens: Permanently removes all uncommitted changes. β οΈ Use with caution!
π°οΈ Time travel to a previous state (undo almost anything) β
π‘ Show me the commands
Perfect for: When you've made a huge mistake (like a bad rebase or deleting a branch) and need to go back in time.
Step 1: Find your past action
git reflogStep 2: Reset to a previous state
git reset --hard aef5de(Replace aef5de with the reflog entry you want to restore)
What happens:
git reflogshows you a log of every action you've taken (commits, merges, resets, etc.).git reset --hardforce-resets your entire repository to that point in time. It's the ultimate undo button.- β οΈ Warning: This is a powerful command. It will destroy any uncommitted changes and rewrite your history.