The Dev Log › Tips & Cheat Codes
Git Commands That Will Save You at 2 AM
By Jezer Niel Blanca, Full Stack Developer ·
·
4 min read
The Git recovery commands I reach for when a deploy goes sideways, a branch disappears, or a commit lands in the wrong place.
Every developer eventually has that moment: it's late, something is broken, and Git seems to have eaten your work. The good news is that Git almost never truly loses anything. You just need to know where to look. These are the commands I keep in muscle memory for exactly those moments.
Stay Calm and Check the State
Before you run anything destructive, look around:
git status
git log --oneline --graph --decorate -20
git stash list
Half of all Git emergencies are solved by simply understanding which branch you're on and what's staged.
The Reflog Is Your Time Machine
The reflog records every place HEAD has pointed, including commits you "lost" through a reset or a bad rebase:
git reflog
git switch -c rescue-branch HEAD@{3}
If you committed it at any point, it's almost certainly still in the reflog. Breathe.
Undo the Last Commit (But Keep the Changes)
Committed too early or with the wrong message?
git reset --soft HEAD~1
Your changes stay staged, ready to recommit. To just fix the message or add a forgotten file:
git add forgotten-file.php
git commit --amend --no-edit
Only amend commits you haven't pushed to a shared branch.
Undo a Pushed Commit Safely
Once a commit is on a shared branch, don't rewrite history. Create a new commit that reverses it:
git revert <commit-sha>
For a merge commit, tell Git which parent to keep:
git revert -m 1 <merge-sha>
This is the command I reach for when a deploy needs to be rolled back fast.
Committed to the Wrong Branch
It happens constantly. Move the commit to where it belongs:
git switch correct-branch
git cherry-pick <commit-sha>
git switch wrong-branch
git reset --hard HEAD~1
Double-check with git log before the hard reset, and make sure the wrong branch wasn't already pushed.
Discard Local Changes
Modern Git has restore, which is clearer than the old checkout overloads:
git restore path/to/file.js
git restore --staged path/to/file.js
git clean -fdn
git clean -fdn is a dry run that shows which untracked files would be deleted. Drop the n only when you're sure.
Stash Like a Pro
Need to switch context mid-task?
git stash push -u -m "wip: checkout refactor"
git stash list
git stash pop stash@{0}
The -u flag includes untracked files, which is usually what you want.
Find the Commit That Broke Things
When something worked last week and doesn't now, bisect does a binary search through history:
git bisect start
git bisect bad
git bisect good v1.4.0
Git checks out a commit halfway between. Test it, mark it good or bad, and repeat until Git names the culprit. Finish with git bisect reset. If you have a test that reproduces the bug, git bisect run can automate the whole thing.
Who Changed This Line?
git blame -L 40,60 app/Services/BillingService.php
git log -S "calculateTax" --oneline
The -S flag, sometimes called the pickaxe, finds commits that added or removed a specific string. It's incredibly useful for tracking down when a function appeared or vanished.
Force Push Without Hurting Anyone
If you must force push to your own feature branch, use the safer variant:
git push --force-with-lease
It refuses to overwrite the remote if someone else has pushed since your last fetch.
A Few Aliases Worth Adding
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.undo "reset --soft HEAD~1"
git config --global alias.wip "commit -am 'wip'"
Habits That Prevent 2 AM
- Commit small and often; small commits are easy to revert.
- Pull with rebase on feature branches to keep history clean.
- Protect your main branch and require reviews.
- Tag every release so rollbacks have a clear target.
Git rewards the developers who understand it. Want help setting up a workflow your team can trust? Let's talk.
Tags: Git, CLI, Productivity, DevOps