DEV Community

Cover image for Unlock Git’s Hidden Powers: 4 Commands That Will Save Your Code and Your Sanity
Tahsin Abrar
Tahsin Abrar

Posted on • Edited on

Unlock Git’s Hidden Powers: 4 Commands That Will Save Your Code and Your Sanity

If you’re like me, you probably use Git every day with commands like git add, git commit, and git push. But Git has superpowers-commands that can save hours, rescue broken codebases, and help you understand who wrote what and why.

In this post, I’ll break down four powerful (but often underused) Git commands:

  • git bisect
  • git stash -p
  • git blame
  • git cherry-pick

For each command, I’ll share:
✅ What problem it solves
📖 The story behind why you’d use it
🛠️ How to use it with real examples

Let’s dive in!


🕵️‍♂️ 1. git bisect - Hunt Down the Bug Like a Detective

✅ What Problem It Solves:

You’re working in a large codebase. Something used to work, and now it’s broken. You don’t know which commit caused the bug.

📖 The Story:

Let’s say you run a test that used to pass:

npm run test
Enter fullscreen mode Exit fullscreen mode

Now it’s failing. But you have hundreds of commits. Manually checking them one-by-one is a nightmare. That’s where git bisect becomes your bug detective!

🛠️ How to Use It:

git bisect start
git bisect bad                    # current commit is bad
git bisect good v1.0.0           # last known good commit
Enter fullscreen mode Exit fullscreen mode

Git now checks out a commit in the middle. You test it. If it’s good:

git bisect good
Enter fullscreen mode Exit fullscreen mode

If it’s bad:

git bisect bad
Enter fullscreen mode Exit fullscreen mode

Git will narrow it down-binary search style-until it tells you the exact commit that introduced the bug.

To finish:

git bisect reset
Enter fullscreen mode Exit fullscreen mode

🚀 Great for: bug hunters, testers, QA engineers, or developers fixing regressions.


🧺 2. git stash -p - Stash Only What You Want

✅ What Problem It Solves:

You’re working on multiple changes but only want to temporarily save part of it -not everything.

📖 The Story:

You’re fixing a bug and suddenly get a Slack message:

"Hey, can you quickly switch branches and check this issue?"

You’ve edited 10 files but only want to stash 2.

🛠️ How to Use It:

git stash -p
Enter fullscreen mode Exit fullscreen mode

Git will go file-by-file, hunk-by-hunk, asking:

Stage this hunk [y,n,q,a,d,/,e,?]?
Enter fullscreen mode Exit fullscreen mode

Press:

  • y to stash this hunk
  • n to skip
  • q to quit

Now your working directory is clean, and you can safely switch branches.

🧠 Pro tip: Later, use git stash pop to bring it back.

🚀 Great for: multitaskers, bug-fixers, or anyone working with large diffs.


👤 3. git blame - Find Out Who Wrote What (And Why)

✅ What Problem It Solves:

You’re staring at a confusing line of code and wondering:

Who wrote this? When? And maybe… why?

📖 The Story:

You find this suspicious line:

if (isActive && data.length > 0) {
Enter fullscreen mode Exit fullscreen mode

You’re not sure if it’s safe to remove. Time to investigate.

🛠️ How to Use It:

git blame src/utils/isActive.js
Enter fullscreen mode Exit fullscreen mode

It will show:

a1b2c3d4 (Alice 2023-11-12) if (isActive && data.length > 0) {
Enter fullscreen mode Exit fullscreen mode

Now you know who, when, and which commit it came from. Want to dig deeper?

git show a1b2c3d4
Enter fullscreen mode Exit fullscreen mode

🚀 Great for: understanding legacy code, reviewing changes, or contacting the original author for context.


🍒 4. git cherry-pick - Pick Just One Commit and Apply It Elsewhere

✅ What Problem It Solves:

You want to move just one commit from one branch to another-without merging everything.

📖 The Story:

You fixed a bug on feature/login. Your teammate says:

"That fix is perfect! Can you apply it to main too?"

Instead of re-writing the code or merging the whole branch, you can cherry-pick that one commit.

🛠️ How to Use It:

  1. Find the commit hash:
git log --oneline
Enter fullscreen mode Exit fullscreen mode

Example output:

b7f4d1f Fixed the login redirect bug
Enter fullscreen mode Exit fullscreen mode
  1. Switch to main:
git checkout main
Enter fullscreen mode Exit fullscreen mode
  1. Cherry-pick the commit:
git cherry-pick b7f4d1f
Enter fullscreen mode Exit fullscreen mode

Boom. That single fix is now on main.

🚀 Great for: hotfixes, backporting features, or syncing branches surgically.


🧠 Summary: When to Use What

Command Use Case Best For
git bisect Find which commit broke something Bug fixers, testers
git stash -p Temporarily save only part of your changes Multitaskers, hotfix responders
git blame See who wrote a specific line and why Legacy explorers, reviewers
git cherry-pick Move a specific commit to another branch Feature syncing, hotfixing

Top comments (0)