One-liners for every git disaster you'll inevitably cause.
Because git is powerful, and you are reckless.
# Undo the last commit but keep your changes
git reset HEAD~1
# Move it to the correct branch
git stash
git checkout correct-branch
git stash pop
git add .
git commit -m "your message"# Remove the file from git history (nuclear option)
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch path/to/secret-file" \
--prune-empty --tag-name-filter cat -- --all
# Force push (yes, you have to)
git push origin --force --all
# Then rotate your key immediately. The old one is compromised.
# Git history is cached by GitHub — even after force push,
# the old commits are accessible for ~90 days via SHA.# Create the branch you meant to commit to (keeps your commits)
git branch feature-branch
# Reset main back to before your commits
git reset --hard origin/main
# Switch to your feature branch — your commits are there
git checkout feature-branch# Undo last commit, keep changes staged
git reset --soft HEAD~1
# Undo last commit, keep changes unstaged
git reset HEAD~1
# Undo last commit, destroy changes forever
git reset --hard HEAD~1# Revert creates a NEW commit that undoes the old one (safe for shared branches)
git revert HEAD
git push
# Nuclear option — rewrites history (only if nobody else has pulled)
git reset --hard HEAD~1
git push --force# Find the last commit on the deleted branch
git reflog
# Look for the commit hash, then recreate the branch
git checkout -b recovered-branch abc1234# Amend the most recent commit message
git commit --amend -m "fixed message"
# If already pushed
git push --force# Unstage a specific file (keeps changes)
git reset HEAD filename
# Unstage everything (keeps changes)
git reset HEAD# Abort the merge and go back to before you started
git merge --abort
# If you already committed the merge and it's wrong
git reset --hard ORIG_HEADgit show abc1234# Binary search through commits
git bisect start
git bisect bad # current commit is broken
git bisect good abc1234 # this old commit was working
# Git will checkout commits for you to test
# Mark each as good or bad until it finds the culprit
git bisect reset # when done# Delete everything and re-clone
cd ..
rm -rf your-repo
git clone https://github.com/you/your-repo.git# Remove the large file from the last commit
git rm --cached huge-file.zip
git commit --amend -m "remove large file"
# If it's in older commits, use BFG Repo-Cleaner
# https://rtyley.github.io/bfg-repo-cleaner/
java -jar bfg.jar --strip-blobs-bigger-than 100M
git reflog expire --expire=now --all
git gc --prune=now --aggressive
git push --forcegit cherry-pick abc1234# List all stashes
git stash list
# Show what's in a specific stash
git stash show -p stash@{0}
# Apply a specific stash
git stash apply stash@{0}
# Apply and delete the stash
git stash pop stash@{0}# You're not on a branch. Create one from where you are
git checkout -b new-branch-name
# Or go back to an existing branch (your uncommitted work may be lost)
git checkout maingit fetch origin
git reset --hard origin/main
git clean -fd# Git already tracks the file. Remove it from tracking (keeps local copy)
git rm --cached filename
git commit -m "stop tracking filename"
# For a whole directory
git rm -r --cached directory/
git commit -m "stop tracking directory"- Before doing anything destructive:
git reflogis your black box recorder. It logs every HEAD movement for 90 days. - When in doubt:
git stashyour changes first. You can alwaysgit stash popto get them back. - Never force push to shared branches unless you've warned everyone on the team.
- Rotate your secrets immediately after removing them from git history. The exposure already happened.
MIT — Because even your mistakes should be open source.
Built by DrCookies84 — who has made every single one of these mistakes at least twice.