Why the Git CLI Still Matters
GUI tools for Git have improved enormously, but the command line remains the most expressive and portable interface for version control. Understanding Git's CLI means you're never blocked by a tool's limitations — and you can work effectively on any server, container, or remote environment. This guide covers the commands that matter most, from daily drivers to powerful utilities many developers overlook.
The Everyday Workflow Commands
These are the commands you'll use on virtually every working day:
git status— See which files are staged, unstaged, or untracked. Your constant companion.git add -p— Interactively stage hunks of changes rather than entire files. This leads to cleaner, more atomic commits.git commit -m "message"— Create a commit. Use clear, imperative-mood messages: "Fix login redirect bug" not "fixed stuff".git log --oneline --graph --decorate— A compact, visual representation of your branch history.git diff --staged— Review exactly what you're about to commit before you commit it.
Branching and Merging
Effective branching is at the heart of modern development workflows:
git switch -c feature/my-feature— Create and check out a new branch (the modern alternative togit checkout -b).git merge --no-ff feature/my-feature— Merge with a merge commit even when a fast-forward is possible, preserving branch history.git rebase main— Replay your branch commits on top of the latest main, creating a linear history.git branch -d feature/my-feature— Delete a merged branch locally.
Undoing Mistakes
One of Git's superpowers is the ability to recover from almost any mistake:
| Situation | Command |
|---|---|
| Undo last commit, keep changes staged | git reset --soft HEAD~1 |
| Undo last commit, unstage changes | git reset HEAD~1 |
| Discard all local changes | git restore . |
| Revert a specific commit (safe for shared branches) | git revert <commit-hash> |
| Find a lost commit | git reflog |
Stashing Work in Progress
git stash lets you set aside uncommitted work and return to a clean state. Key variations:
git stash push -m "WIP: auth changes"— Stash with a descriptive message.git stash list— See all stashed states.git stash pop— Apply the most recent stash and remove it from the list.git stash apply stash@{2}— Apply a specific stash without removing it.
Powerful But Lesser-Known Commands
These commands can save hours when you need them:
git bisect— Binary search through commit history to find which commit introduced a bug.git blame -L 10,20 file.js— See who last changed lines 10–20 of a file and in which commit.git cherry-pick <hash>— Apply a specific commit from another branch onto your current branch.git shortlog -sn— A contributor summary sorted by commit count.
Building Good Habits
The best Git users aren't those who know every flag — they're the ones who commit frequently, write clear messages, and treat version history as documentation. Pair this CLI knowledge with a solid branching strategy (like GitHub Flow or trunk-based development) and you'll work more confidently on any team or project.