Skip to content

πŸš€ 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

sh
git checkout main

Step 2: Sync with remote

sh
git reset --hard
git pull

Step 3: Create new branch

sh
git checkout -b feature/GG-12_storyName

Modern alternative:

sh
git switch -c feature/GG-12_storyName

What happens:

  1. βœ… Switch to main branch
  2. 🧹 Discard any local changes and sync with remote
  3. ⬇️ Pull latest updates
  4. 🎯 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:

sh
git rebase -i HEAD~3

What 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.

sh
git push -u origin feature/GG-12_storyName

What happens:

  • πŸ“€ Pushes your branch to the remote repository
  • πŸ”— The -u flag (shorthand for --set-upstream) sets up tracking so future git push commands 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:

sh
git pull --rebase origin main

What happens: Fetches the latest main and replays your commits on top of it, maintaining a linear history.

Merge approach:

sh
git pull --merge origin main

What 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:

sh
git push --force-with-lease

Pro workflow combo:

Step 1: Squash commits

sh
git rebase -i HEAD~3

Step 2: Update with main

sh
git pull --rebase origin main

Step 3: Force-push safely

sh
git push --force-with-lease --force-if-includes

What 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

sh
git fetch origin

Step 2: Reset to remote

sh
git reset --hard origin/feature/GG-12_storyName

What happens:

  1. πŸ“₯ Fetches latest remote information
  2. πŸ’₯ Brutally resets your local branch to match the remote version
  3. ⚠️ 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:

sh
git checkout branch-name

Modern alternative:

sh
git switch branch-name

What 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:

sh
git status

What 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:

sh
git add .

Stage specific files:

sh
git add file1.js file2.css

Commit with message:

sh
git commit -m "feat: add user authentication"

Alternative: Stage and commit in one go:

sh
git commit -am "feat: add user authentication"

What happens:

  1. πŸ“¦ git add stages files for the next commit
  2. ✨ git commit creates a new commit with your staged changes ⚠️ -am is 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:

sh
git log --oneline

With visual graph:

sh
git log --oneline --graph

What 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:

sh
git reset --soft HEAD~1

What 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:

sh
git reset --hard

Discard specific file:

sh
git checkout HEAD -- filename.js

What 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

sh
git reflog

Step 2: Reset to a previous state

sh
git reset --hard aef5de

(Replace aef5de with the reflog entry you want to restore)

What happens:

  1. git reflog shows you a log of every action you've taken (commits, merges, resets, etc.).
  2. git reset --hard force-resets your entire repository to that point in time. It's the ultimate undo button.
  3. ⚠️ Warning: This is a powerful command. It will destroy any uncommitted changes and rewrite your history.