If you've been using Git for a while, chances are you've had at least
one moment where your heart skipped a beat.
Maybe you reset the wrong commit, deleted a branch, or switched branches
without saving your work. Your first thought is usually, "I just lost
everything."
I've been there too.
The good news is that Git is much more forgiving than it looks. Most of
the time, your work is still there---you just need to know which command
to use.
In this article, I'll share the Git commands I rely on whenever
something goes wrong. They aren't magic tricks; they're practical tools
that have saved me more than once.
1. Check what's happening first
Before trying to fix anything, see the current state of your repository.
git status
I run this command almost every time before committing. It quickly tells
me which files have changed, which ones are staged, and whether I've
forgotten anything.
2. Review your changes
Before creating a commit, take a quick look at what actually changed.
git diff
If you've already staged files:
git diff --staged
This simple habit has saved me from committing debug code and accidental
edits more times than I'd like to admit.
3. Read your commit history
When you're trying to understand how your project reached its current
state, a clean commit history is incredibly useful.
git log --oneline --graph --decorate
The graph view makes it much easier to understand branches and merges.
4. The lifesaver: git reflog
If you only remember one command from this article, make it this one.
git reflog
Even after a reset or an accidental checkout, Git usually keeps a record
of where your HEAD has been.
Found the commit you need?
git checkout <commit-id>
or restore it completely:
git reset --hard <commit-id>
Many "lost" commits aren't actually lost.
5. Undo a commit without losing your work
Forgot to include a file?
git reset --soft HEAD~1
The commit disappears, but all your changes stay exactly where they are,
ready for another commit.
6. Remove a commit completely
Sometimes you really do want to go back.
git reset --hard HEAD~1
Use this carefully. It removes both the commit and your local changes.
7. Pause your work with git stash
Imagine you're halfway through a feature when someone asks you to fix an
urgent production bug.
Instead of making a messy temporary commit, simply stash your work.
git stash
When you're ready to continue:
git stash pop
See every saved stash:
git stash list
8. Restore a deleted file
Deleted a tracked file by mistake?
git restore app.py
Need an older version?
git restore --source <commit-id> app.py
9. Find who changed a line
Ever wondered who introduced a particular line of code?
git blame app.py
This command shows who last modified each line and in which commit.
10. Search your history
Looking for the commit that fixed authentication?
git log --grep="authentication"
Looking for where a function was added?
git log -S "loginUser"
These searches save a lot of scrolling.
11. Clean untracked files
First, preview what Git will remove.
git clean -n
If everything looks correct:
git clean -fd
Always preview before deleting.
12. Visit an older version
Sometimes you just want to test an older commit.
git switch --detach <commit-id>
This lets you explore older versions without affecting your current
branch.
- Check
git statusbefore every commit. - Review your changes with
git diff. - Write meaningful commit messages.
- Work on feature branches instead of
main. - Push your work regularly.
- Learn
git reflogbefore you actually need it.
Final Thoughts
Every developer makes Git mistakes. I still do.
The difference is that, over time, you realize most mistakes are
recoverable. Once you're comfortable with commands like git reflog,
git stash, and git restore, those "I think I broke everything"
moments become much less stressful.
I hope this guide gives you a few commands you'll remember the next time
Git surprises you.
If there's a Git command that has saved your day, I'd love to hear about
it in the comments.
Top comments (0)