DEV Community

Franciele Stefani da Costa
Franciele Stefani da Costa

Posted on

Going Back to a Previous Commit: How I Turned a Simple Git Reset Into a Horror Show (and What I Learned)

The day I almost deleted my dignity along with my code

You know that day when you think: “This will be quick, just install Husky, configure commitlint, and done”?

Yeah… it was supposed to be that simple.

The request was easy: install Husky (pre-commit, pre-push, and commit-msg) and commitlint. I installed everything, set it up nicely… but, being a beginner, I had to make a few test commits to see if it was working.

Result? The commit history ended up messier than a drawer full of random cables that nobody knows what they’re for.

Then came the new mission:

"Franciele, we need to clean up the commit history."

And I thought: “Piece of cake, I saw this on Google.”


The fatal mistake

I went straight to a git reset… and friend, that was the beginning of the tragedy.

The command I chose deleted all the changes on my local machine.

And no, of course, I didn’t have a backup. Who hasn’t, right? (Seriously, always have one.)

In desperation, I dug around and discovered that, luckily, the original branch with the correct code was still intact. I breathed, cloned it, and this time, I actually researched before typing commands like I was some hacker in The Matrix.


What I learned about resetting commits

There are three main ways to use git reset, and each one can completely change your day (or ruin it):

  • git reset --hard <commit>

    Returns your project to the state of that commit and deletes all uncommitted changes.

    Perfect for losing code with no backup, not recommended.

  • git reset --soft <commit>

    Moves the HEAD to that commit but keeps your changes staged. Your code is safe, only the history goes back in time.

  • git reset --mixed <commit> (default)

    Like soft, but keeps the changes unstaged. You need to git add them again.


My problem and the solution

What I wanted:

  • Remove only the test commits.
  • Keep my current code intact.
  • Make a new, clean commit with a proper message.

Step by step:

  1. Check which branch I was on:
git branch
Enter fullscreen mode Exit fullscreen mode
  1. Make sure I’m on the correct branch:
git checkout branch_name
Enter fullscreen mode Exit fullscreen mode
  1. See the commits:
git log
Enter fullscreen mode Exit fullscreen mode
  1. Copy the commit number before the test commits and go back to it using soft reset:
git reset --soft commit_number
Enter fullscreen mode Exit fullscreen mode
  1. Make the new commit:
git commit -m "chore: configure husky, lint-staged and commitlint properly"
Enter fullscreen mode Exit fullscreen mode
  1. Force push:
git push origin your-branch -f
Enter fullscreen mode Exit fullscreen mode

The most valuable lesson
In the end, I realized that daily backups are your best friend.
Create a private GitHub repository and push your project at the end of each day.
Don’t trust that your computer will last forever or that you’ll remember every command.

Because losing a bit of code hurts, but losing everything because you didn’t back up…
It’s like spilling coffee on your keyboard and thinking you can dry it with a hairdryer: it’s gonna go badly.


And you? Have you ever made a “small disaster” in Git? Share it with me so I can learn from your mistakes too. 😅

Top comments (0)