# Git Commands That Saved My Life (And My Codebase)

### The "Oh No" Moment

We have all been there. You are working on a new feature for your MERN stack application—maybe refactoring the entire backend folder structure. You type git status, you see a wall of red text, and you realize: *I have absolutely no idea what I just did, and nothing works anymore.*

Or worse, you committed a password to GitHub.

In the early days of building my portfolio ([**uzairalam.me**](https://www.google.com/url?sa=E&q=https%3A%2F%2Fuzairalam.me)), I treated Git like a save button. I did git add . and git commit -m "update" and prayed nothing broke.

But over time, I learned that Git is actually a time machine. Here are the 5 specific commands that saved me when I thought I had ruined everything.

### 1\. The "I Committed Too Early" Fix

**Scenario:** You hit commit, but then realized you forgot to delete that console.log(user\_password) or you forgot to add one specific file.

Instead of making a new commit called "fixing previous commit" (which looks messy), use:

```javascript
git reset --soft HEAD~1
```

**What it does:** It undoes the last commit but—and this is crucial—**keeps your changes staged**.  
It moves you back in time to right *before* you hit commit. You can now add the missing file, delete the console log, and commit again properly.

### 2\. The "Typo Fixer"

**Scenario:** You typed git commit -m "addd new featre" and hit enter. Now your project history looks unprofessional.

You don't need to reset. Just run:

```javascript
git commit --amend -m "Add new feature"
```

**What it does:** It overwrites the *last* commit message with the new one. No one will ever know you made a typo.

### 3\. The "Context Switcher" (git stash)

**Scenario:** I’m deep in the zone working on a React component. Suddenly, I notice a critical bug in the backend API. I need to switch branches to fix it, but I don't want to commit my half-finished React code.

If I try to switch branches, Git will yell at me. The solution?

```javascript
git stash
```

**What it does:** It takes all your uncommitted changes and stores them in a temporary storage area (the "stash"). Your working directory is now clean.

1. Fix the backend bug.
    
2. Come back to your branch.
    
3. Run git stash pop.
    

Boom. Your half-finished React code is back exactly where you left it.

### 4\. The "Nuclear Option" (git reflog)

**Scenario:** You tried to be clever. You did a hard reset, or you rebased, and now your commits are *gone*. You try git log and they are missing. You feel the cold sweat of panic.

Relax. Nothing in Git is ever truly lost immediately.

```javascript
git reflog
```

**What it does:** git log shows the history of the code. git reflog shows the history of **your actions**.  
It lists every single thing you have done (commit, reset, checkout). Find the index of the commit before you messed up (e.g., HEAD@{5}) and reset to it:

```javascript
git reset --hard HEAD@{5}
```

This command has saved me more times than I can count.

### 5\. The "Visualizer"

**Scenario:** You are working with a team (or just on multiple devices) and the history is a mess of merging branches. git log is just a wall of text.

I have this alias saved because I use it daily:

```javascript
git log --graph --oneline --all --decorate
```

**What it does:** It draws a text-based ASCII graph of your branches in the terminal. You can see exactly where branches split off and merged back in. It makes understanding the history of uzairalam.me much easier than reading raw hashes.

### Summary

Git is powerful, but it's also intimidating. The best way to learn these isn't to memorize them, but to know they exist so you can Google them when the panic sets in.

If you are building a project, do yourself a favor: stop fearing the terminal.

### Thanks for reading!

If you enjoyed this breakdown, you might like my project **Portfolio V1**. It’s a portfolio site built with the MERN stack.

🚀 **Check out the Live Demo on my Portfolio at** [**uzairalam.me**](https://uzairalam.me)

*Connect with me on* [*Twitter/X*](https://x.com/robertdrowninjr) *or* [*LinkedIn*](https://linkedin.com/in/uzair1723) *for more updates.*
