DEV Community

Cover image for 5 Git Commands Every Developer Should Know (That Save Hours of Headaches)
Hizba
Hizba

Posted on

5 Git Commands Every Developer Should Know (That Save Hours of Headaches)

If you've been coding for more than a week, you've probably typed git add ., git commit -m "fixed stuff", and git push a million times. But what happens when things get messy?

Here are 5 powerful Git commands that will save you from panic moments and clean up your workflow instantly.

1. git stash (Your Instant Pause Button)

Imagine you are halfway through coding a feature, but suddenly you need to switch branches to fix an urgent bug. You don't want to commit half-baked code.

Instead, save your changes temporarily without committing them:

Bash
git stash

When you switch back to your branch later, restore your work instantly
with:

Bash
git stash pop

2. git log --oneline --graph (The Visual Timeline)

Tired of looking at a massive wall of commit history that makes no sense? This command gives you a clean, beautiful ASCII-art graph of your branch history right in your terminal.

Bash
git log --oneline --graph --decorate --all

Tip: Make this an alias in your terminal to save typing it out every time!

3. git checkout - (The Quick Switch Back)

Are you constantly jumping back and forth between two different branches? Instead of typing out the full branch name every time, use a hyphen:

Bash
git checkout -

This automatically toggles you back to the previous branch you were just on.

4. git commit --amend (The "Oops" Fixer)

You just committed your code, but you immediately realized you forgot to add a file or made a tiny typo in your commit message. Don't make a brand new commit—just fix the last one:

Bash
git add forgotten_file.js
git commit --amend -m "Correct message here"

This updates your most recent commit cleanly as if you did it right the first time.

5. git reset --soft HEAD~1 (Undo Commit, Keep Code)

Need to undo your last commit because you want to change how it's structured, but you don't want to lose your actual code changes?

Bash
git reset --soft HEAD~1

This rolls back the commit, but leaves all your modified files safely
sitting in your staging area.

Mastering just a few of these intermediate Git commands can save you hours of undoing mistakes and rewriting history.

What is your go-to Git command when things go wrong? Let me know in the comments below! 👇

Top comments (1)

Collapse
 
hizba_31d77c41803163b8ff0 profile image
Hizba

What is your go-to Git command when things go wrong? Let me know in the comments below! 👇