DEV Community

Cover image for Before You Panic: Git Commands That Can Save Your Project.
Asep Sayyad
Asep Sayyad

Posted on • Originally published at asepsayyad007.Medium

Before You Panic: Git Commands That Can Save Your Project.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

If you've already staged files:

git diff --staged
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

or restore it completely:

git reset --hard <commit-id>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

When you're ready to continue:

git stash pop
Enter fullscreen mode Exit fullscreen mode

See every saved stash:

git stash list
Enter fullscreen mode Exit fullscreen mode

8. Restore a deleted file

Deleted a tracked file by mistake?

git restore app.py
Enter fullscreen mode Exit fullscreen mode

Need an older version?

git restore --source <commit-id> app.py
Enter fullscreen mode Exit fullscreen mode

9. Find who changed a line

Ever wondered who introduced a particular line of code?

git blame app.py
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Looking for where a function was added?

git log -S "loginUser"
Enter fullscreen mode Exit fullscreen mode

These searches save a lot of scrolling.


11. Clean untracked files

First, preview what Git will remove.

git clean -n
Enter fullscreen mode Exit fullscreen mode

If everything looks correct:

git clean -fd
Enter fullscreen mode Exit fullscreen mode

Always preview before deleting.


12. Visit an older version

Sometimes you just want to test an older commit.

git switch --detach <commit-id>
Enter fullscreen mode Exit fullscreen mode

This lets you explore older versions without affecting your current
branch.


  • Check git status before 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 reflog before 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)