Git Commands That Will Save Your Life (Beyond commit and push)

Git is a time machine for your code. While you use commit and push every day, the real power lies in the commands that let you fix mistakes, hunt down bugs, and clean up your project history.

These are the 6 essential, “panic button” Git commands every developer needs to know.

1. The Ultimate Undo Button: git reflog

If you ever feel like you’ve accidentally deleted a branch, lost commits, or messed up a hard reset, git reflog is your safety net.

What it does: git reflog tracks every single action (commit, reset, merge, or checkout) you’ve performed on your local repository. It’s a safety net that lets you find and recover any commit, even those you thought were deleted.

The Life-Saving Scenario: You ran a git reset --hard command and instantly regretted it. The commits seem gone forever.

The Command

  1. Find the lost commit hash:git reflog You’ll see a log of your actions. Find the hash that existed before your mistake (e.g., 0b1f2g8).
  2. Jump back in time (recovery):git reset --hard 0b1f2g8

You are instantly back in time, and the “lost” commits are recovered.

2. The Code Detective: git bisect

Finding the single commit that introduced a bug across weeks of work is tedious. git bisect does the heavy lifting for you.

What it does: git bisect automatically finds the exact commit that introduced a bug. It uses a smart, half-and-half search (binary search) to quickly pinpoint the problem commit, saving you hours of manual checking.

The Life-Saving Scenario: A bug appears in your current code, and you need to find the exact commit that caused it from among hundreds of commits.

The Command

  1. Start the search:git bisect start
  2. Mark current code as bad:git bisect bad
  3. Mark known good code (e.g., last release tag):git bisect good <commit_hash_or_tag>

Git will check out a commit in the middle. You test it, then tell Git the result:

  1. Tell Git the result:
    • If the bug is still there: git bisect bad
    • If the bug is gone: git bisect good

Git keeps narrowing the search until it tells you the exact bad commit.

  1. Clean up:git bisect reset

3. History Cleaner: git rebase -i

Stop committing “WIP,” “oops,” and “fixed typo.” Use interactive rebase to create a clean, professional history before merging.

What it does: git rebase -i lets you clean up your commit history before sharing it. Use it to combine (squash) multiple “WIP” commits into one perfect commit, reword messages, or delete mistakes.

The Life-Saving Scenario: You have five messy commits for one feature and need to turn them into a single, clean commit for your Pull Request.

The Command

Run the command relative to the commit before the sequence you want to edit. To edit the last five commits:

git rebase -i HEAD~5

Change the file that opens, instructing Git what to do (usually pick the first one, then squash the rest).

pick a4g1e2w First commit
squash b3f2d1c Fix minor issue
squash c5e4f3a Add feature details

4. The Surgical Steal: git cherry-pick

When you fix a critical bug on a feature branch, but you can’t merge the whole feature yet, you need to grab just the fix.

What it does: git cherry-pick allows you to take a single commit from one branch and apply it directly onto the tip of your current branch. It’s surgical and precise.

The Life-Saving Scenario: A hotfix is ready on a test branch, but main needs that fix deployed immediately.

The Command

  1. Switch to the target branch (main):git checkout main
  2. Apply the single commit:git cherry-pick <hotfix_commit_hash>

The fix is now applied to main without merging any other feature code.

5. The Shared Safety Net: git revert

If you’ve already pushed a bad commit to a shared branch, DO NOT use git reset. You will cause headaches for your teammates.

What it does: git revert undoes a commit by creating a new commit that contains the inverse of the changes. It doesn’t erase history; it just adds a new, compensating entry. This is the safest way to undo changes on a shared branch.

The Life-Saving Scenario: You merged a commit to the main branch that broke the build, and everyone else has already pulled the broken code.

The Command

  1. Find the hash of the bad commit.
  2. Create the undo commit:git revert <bad_commit_hash>

Git creates a new commit that removes the changes introduced by the bad commit, keeping the history safe for everyone.

6. The Quick Save: git stash

You’re halfway through a feature, but you need to switch branches immediately to handle an emergency request. You don’t want to commit incomplete code.

What it does: git stash temporarily shelves your uncommitted changes (both staged and unstaged) and returns your working directory to a clean state, allowing you to switch context instantly.

The Life-Saving Scenario: An urgent bug report comes in, and you need to switch to the hotfix branch, but your current code is broken and unfinished.

The Commands

  1. Stash the changes (Quick Save):git stash push -m "WIP on Feature X urgent switch"
  2. Bring the changes back (Load Save):git stash pop

Your directory is now clean for the emergency, and your work-in-progress is safely saved for later.