10 Git Commands You Keep Googling (Finally Explained)
Stop searching for 'how to undo git commit' every week. Here's what these commands actually do and why they work.
You’ve been using Git for years. You’ve pushed thousands of commits. And yet somehow, you still Google “git undo last commit” at least once a month.
It’s not your fault. Git’s commands are powerful but cryptic—and most tutorials show you what to type without explaining why it works. Once you understand the mental model, these commands stop being incantations and start making sense.
Here are ten Git commands you’ll finally remember.
1. Undo the Last Commit (Keep Your Work)
The command:
git reset --soft HEAD~1
What it means:
reset— move HEAD (your current position) somewhere else--soft— keep changes stagedHEAD~1— one commit before now
Why it sticks: “Go back one commit, but keep my work staged.” Your changes don’t disappear—they’re just uncommitted.
Know the three modes:
git reset --soft HEAD~1 # keep changes staged
git reset --mixed HEAD~1 # unstage changes, keep files (default)
git reset --hard HEAD~1 # delete everything (dangerous!)
2. Undo a Pushed Commit (Safely)
The command:
git revert HEAD
What it means: Create a new commit that undoes the previous one.
Why it matters: Unlike reset, revert doesn’t rewrite history. Your teammates won’t hate you.
The decision tree:
# Haven't pushed yet? Reset is cleaner.
git reset --soft HEAD~1
# Already pushed? Revert is safer.
git revert HEAD
3. See What Changed
The command:
git diff
What you’re actually asking: “Show me unstaged changes.”
The variations you need:
git diff --staged # staged changes (about to commit)
git diff main # your branch vs main
git diff HEAD~3 # last 3 commits
git diff --stat # just the summary
Mental model: git diff A B = “what changed going from A to B.”
4. Save Work Without Committing
The command:
git stash
When you need it: Your branch is dirty, but you need to switch branches RIGHT NOW.
The workflow:
git stash # hide your work
git checkout other-branch # do something urgent
git checkout original # come back
git stash pop # restore your work
Pro moves:
git stash list # see all stashes
git stash -m "WIP: login fix" # name it (you'll thank yourself)
git stash apply # restore but keep stash (safer)
git stash drop # delete most recent stash
5. Fix the Last Commit
Typo in your commit message?
git commit --amend -m "Better message"
Forgot a file?
git add forgotten-file.js
git commit --amend --no-edit
The catch: Don’t amend commits you’ve pushed—unless you’re ready to force-push and explain yourself.
6. View History (Without the Noise)
The command:
git log --oneline
Why it’s better: You see the story, not a novel.
Useful variations:
git log --oneline -10 # last 10 commits
git log --graph --oneline # branch structure (visual)
git log --author="name" # who did what
git log -- path/to/file # history of one file
git log --since="2 weeks" # recent only
7. Find the Commit That Broke Everything
The command:
git bisect start
git bisect bad # current is broken
git bisect good abc123 # this one worked
What happens: Git binary-searches your history. You test each commit, mark it good or bad, and Git narrows down to the culprit.
Why “bisect”: It cuts the range in half each time. A 1000-commit search takes ~10 checks.
Full workflow:
git bisect start
git bisect bad
git bisect good v1.0
# Git checks out a commit. Test it. Then:
git bisect good # or: git bisect bad
# Repeat until Git finds the first bad commit
git bisect reset # done, return to normal
8. Grab One Commit From Another Branch
The command:
git cherry-pick abc123
The scenario: There’s one fix on develop you need on main. You don’t want to merge everything.
Why “cherry-pick”: You’re picking one cherry from the tree, not harvesting the whole branch.
git checkout main
git cherry-pick abc123 # grab that commit
git cherry-pick abc123 def456 # grab multiple
9. Delete Untracked Files
The command:
git clean -fd
What it does: Removes files Git doesn’t know about.
Always dry-run first:
git clean -n # preview what would be deleted
git clean -fd # actually delete files and directories
git clean -fdx # also delete ignored files (node_modules, etc.)
Warning: This is permanent. No recycle bin. -n first, always.
10. Rebase vs. Merge
Merge:
git checkout main
git merge feature
Creates a merge commit. Preserves exact history. Can get messy.
Rebase:
git checkout feature
git rebase main
Replays your commits on top of main. Linear history. Looks cleaner.
The golden rule: Never rebase commits that others have pulled.
Safe rebase workflow:
git checkout feature
git rebase main # update your branch
git checkout main
git merge feature # fast-forward, no merge commit
Bonus: The Emergency Escape Hatch
Made a terrible mistake? git reflog is your friend.
git reflog
This shows every place HEAD has been—even “deleted” commits. Find the hash before you messed up, and:
git reset --hard abc123
Git almost never truly deletes anything. reflog is your undo history for undo.
The Mental Model
Git has three areas:
| Area | What’s There |
|---|---|
| Working directory | Files on disk |
| Staging area | Marked for next commit |
| Repository | Committed history |
Commands move changes between them:
[Working] --git add--> [Staging] --git commit--> [Repository]
[Working] <--git restore-- [Staging/Repository]
[Staging] <--git reset-- [Repository]
Once you see Git as “moving changes between buckets,” everything clicks.
Want 101 concepts like this, each on one page? Command Line Confidence covers Git workflows, shell scripting, and the terminal fundamentals most developers skip—explained once, remembered forever.
Want more tips like this?
Our book covers 101 essential commands—one per page, each with examples you can run immediately.
Check out Command Line Confidence