**Quick fixes for "Oh no!" moments**
Provides emergency Git recovery commands for common mistakes like wrong commits, merges, and deleted work.
/plugin marketplace add Data-Wise/craft/plugin install data-wise-craft@Data-Wise/craftgit/docs/Quick fixes for "Oh no!" moments
ā ļø BEFORE PANICKING: Git is designed to be hard to break. You probably didn't lose anything.
Just committed, message is wrong:
# Option 1: Amend the message
git commit --amend -m "correct message here"
# Option 2: Interactive amend (opens editor)
git commit --amend
# Edit message, save, close editor
Already pushed?
# If on feature branch and no one else using it:
git commit --amend -m "correct message"
git push --force-with-lease origin feature-branch
# ā ļø Only if:
# - You're NOT on main/master
# - You're the only one working on this branch
# - You noticed within minutes
Committed several commits ago?
# Interactive rebase (advanced)
git rebase -i HEAD~3 # Edit last 3 commits
# In the editor:
# Change 'pick' to 'reword' for the commit you want to fix
# Save and close
# Edit the message when prompted
Oh no, I'm on main but wanted feature-branch!
# 1. Note the commit (just in case)
git log -1 # Copy the commit hash (e.g., a3f9d2e)
# 2. Undo the commit (keeps changes)
git reset --soft HEAD~1
# 3. Switch to correct branch
git checkout feature-branch
# Or: /branch switch feature-branch
# 4. Commit again (now on right branch)
/commit
If you already pushed to wrong branch:
# On the wrong branch (e.g., main):
git log -1 # Note commit hash
git reset --hard HEAD~1 # Undo commit
git push --force-with-lease # ā ļø Dangerous! Only if you're sure
# Switch to right branch:
git checkout feature-branch
git cherry-pick <commit-hash> # Apply commit here
git push
# Undo commit, keep files as uncommitted changes:
git reset HEAD~1
# Or keep files as staged changes:
git reset --soft HEAD~1
When to use: "I committed too early, want to add more files"
# Undo commit AND delete changes:
git reset --hard HEAD~1
# ā ļø This DELETES your work! Only use if you're 100% sure.
When to use: "That commit was a bad experiment, throw it away"
# Undo last 3 commits (keep changes):
git reset HEAD~3
# Undo last 3 commits (discard changes - dangerous!):
git reset --hard HEAD~3
# 1. Undo locally
git reset --hard HEAD~1 # Or HEAD~2 for 2 commits, etc.
# 2. Force push (overwrites remote)
git push --force-with-lease origin feature-branch
# ā ļø Only safe if:
# - You're on YOUR feature branch
# - No one else is using this branch
# - You're okay overwriting remote history
# DON'T force push to main!
# Instead, create a revert commit:
git revert HEAD # Creates new commit undoing last one
git push origin main # Safe push
# This preserves history while undoing changes
Revert multiple commits:
# Revert last 3 commits:
git revert HEAD~2..HEAD # Reverts HEAD, HEAD~1, HEAD~2
git push origin main
Git keeps a 90-day history of EVERYTHING:
# 1. See all operations
git reflog
# Output shows:
# a3f9d2e HEAD@{0}: reset: moving to HEAD~1
# b4e8c1a HEAD@{1}: commit: my deleted commit ā This one!
# c5f7a2d HEAD@{2}: commit: previous commit
# 2. Find the deleted commit (note the hash)
# In example above: b4e8c1a
# 3. Restore it
git cherry-pick b4e8c1a # Bring back one commit
# Or
git reset --hard b4e8c1a # Jump back to that point
# 1. Find the branch in reflog
git reflog | grep branch-name
# 2. Find the last commit on that branch
# e.g., a3f9d2e HEAD@{5}: checkout: moving from branch-name
# 3. Recreate branch
git branch branch-name a3f9d2e
git checkout branch-name
If you deleted files but haven't committed:
# Restore one file:
git checkout HEAD -- file.txt
# Restore all deleted files:
git checkout HEAD -- .
Don't panic! Conflicts are normal.
git status
# Shows:
# Unmerged paths:
# both modified: src/file.js
cat src/file.js
# You'll see:
<<<<<<< HEAD (Your changes)
const API_URL = "https://api.staging.com";
=======
const API_URL = "https://api.prod.com";
>>>>>>> origin/main (Their changes)
Open src/file.js in your editor and decide:
Option A: Keep yours
const API_URL = "https://api.staging.com";
Option B: Keep theirs
const API_URL = "https://api.prod.com";
Option C: Combine both
const API_URL = process.env.API_URL || "https://api.staging.com";
Remove the conflict markers (<<<<<<<, =======, >>>>>>>).
git add src/file.js # Mark as resolved
# If rebase:
git rebase --continue
# If merge:
git merge --continue
# Or use /sync which will guide you
# During merge:
git merge --abort
# During rebase:
git rebase --abort
# This returns you to before the operation
# Discard changes to one file:
git checkout HEAD -- src/file.js
# Or restore from specific commit:
git checkout a3f9d2e -- src/file.js
# Discard ALL changes (dangerous!):
git reset --hard HEAD
# This returns working directory to last commit
# Copy file from main branch:
git checkout main -- src/file.js
# Now src/file.js matches main branch version
If everything is completely broken and you just want to start over:
# Throw away all local changes, match remote exactly:
git fetch origin
git reset --hard origin/main # Or origin/feature-branch
# ā ļø This deletes all uncommitted work!
# Hide all changes (can bring back later):
git stash save "emergency stash before reset"
git reset --hard HEAD
# Later, if you want them back:
git stash pop
# Create new branch from clean main:
git checkout main
git pull origin main
git checkout -b new-feature-branch-attempt-2
# Your old branch still exists if you need it
PRIORITY 1: Remove from history NOW
# If not pushed yet:
git reset --soft HEAD~1 # Undo commit
# Remove secret from file
git commit -m "add feature" # Commit again without secret
# If already pushed (URGENT):
# 1. Remove secret from file
# 2. Change the secret/password immediately (it's compromised!)
# 3. Contact team lead
# 4. Force push only if approved:
git commit --amend
git push --force-with-lease
# Better: Use git-filter-repo or BFG Repo-Cleaner (search for guides)
# Before pushing:
git reset --soft HEAD~1
# Move large files out
git commit
# After pushing:
# Use BFG Repo-Cleaner to remove from history
# (Search: "remove large file from git history")
# Reset feature branch to before merge:
git reflog # Find commit before merge
git reset --hard <commit-before-merge>
# Then do proper rebase:
/sync # Or git rebase origin/main
These commands LOOK at things without changing anything:
git status # What's changed?
git log # Commit history
git log --oneline -10 # Last 10 commits, compact
git diff # What changed in working directory?
git diff HEAD # Diff vs last commit
git diff --stat # Summary of changes
git reflog # Everything you've done (90 days)
git show <commit> # Show specific commit
git branch -a # All branches
Use these to investigate before fixing.
Get help if:
Better to ask than to make it worse!
# Create safety branch:
git branch backup-before-risky-thing
# If things go wrong:
git checkout backup-before-risky-thing
# Push to remote daily:
/done # End of day
> y # Commit
> y # Push
# Your work is backed up to GitHub
# Never work directly on main:
/branch new feature-name
# Experiment freely, can always delete branch
| Command | Commits | Staging | Working Directory |
|---|---|---|---|
--soft | Reset | Keep | Keep |
| (default) | Reset | Reset | Keep |
--hard | Reset | Reset | Reset ā ļø |
Examples:
git reset --soft HEAD~1 # Undo commit, keep everything
git reset HEAD~1 # Undo commit, unstage, keep files
git reset --hard HEAD~1 # Undo commit, delete everything ā ļø
Remember: Git keeps 90 days of history in reflog.
You can undo almost anything:
git reflog # See everything
git reset --hard <hash> # Jump to any point
Even if you:
Reflog has it all!
Print this and keep it handy:
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā GIT UNDO QUICK REFERENCE ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Wrong message: ā
ā git commit --amend -m "new message" ā
ā ā
ā Undo commit (keep changes): ā
ā git reset HEAD~1 ā
ā ā
ā Undo commit (delete changes): ā
ā git reset --hard HEAD~1 ā
ā ā
ā Find deleted commits: ā
ā git reflog ā
ā ā
ā Restore file: ā
ā git checkout HEAD -- file.txt ā
ā ā
ā Abort merge/rebase: ā
ā git merge --abort ā
ā git rebase --abort ā
ā ā
ā When stuck: git status ā
ā When lost: git reflog ā
ā When unsure: ASK FOR HELP! ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
You've got this! Take a breath, read the guide, fix it step by step.
Created: 2025-12-14 Keep this handy during Weeks 1-4