DEV Community

The AI producer
The AI producer

Posted on

15 Git Commands That Save Me Hours Every Week (And Why Most Developers Don't Know Them)

Most developers use 10 Git commands and Google the rest. That wastes 15+ minutes every day searching for the right syntax.

After 8 years of daily Git usage, here are the 15 commands that actually save me hours every week — organized by the problem they solve.

Daily Workflow (5 commands)

1. git switch -c <branch> — Create and switch in one step

git switch -c feature/payment-refactor
Enter fullscreen mode Exit fullscreen mode

Replaces git checkout -b. Cleaner, explicit intent. Added in Git 2.23.

2. git restore --staged <file> — Unstage without losing changes

git restore --staged package-lock.json
Enter fullscreen mode Exit fullscreen mode

Replaces git reset HEAD <file>. Your working directory stays intact.

3. git switch - — Jump back to previous branch

git switch feature/auth
# ... work ...
git switch -  # back to main
Enter fullscreen mode Exit fullscreen mode

Instant context switching. No need to remember branch names.

4. git commit -v — See diff in your editor while committing

git commit -v
Enter fullscreen mode Exit fullscreen mode

Shows the full diff in your commit message buffer. Catches stray console.log before they ship.

5. git log --oneline --graph --all -20 — Visual history at a glance

git log --oneline --graph --all -20
Enter fullscreen mode Exit fullscreen mode

Alias this. You'll use it 20x/day.

Fixing Mistakes (5 commands)

6. git commit --amend --no-edit — Fix the last commit silently

git add forgotten-file.js
git commit --amend --no-edit
Enter fullscreen mode Exit fullscreen mode

Adds staged changes to previous commit without opening editor.

7. git reset --soft HEAD~1 — Undo commit, keep changes staged

git reset --soft HEAD~1
# changes still staged, ready to re-commit properly
Enter fullscreen mode Exit fullscreen mode

Perfect for splitting one messy commit into two clean ones.

8. git restore <file> — Discard working directory changes

git restore broken-experiment.js
Enter fullscreen mode Exit fullscreen mode

Replaces git checkout -- <file>. Safer — won't switch branches by accident.

9. git reflog — The time machine

git reflog
# find the hash you want
git reset --hard <hash>
Enter fullscreen mode Exit fullscreen mode

Recovers "lost" commits, reset mistakes, rebase gone wrong. Never truly loses data.

10. git bisect — Find the bug-introducing commit automatically

git bisect start
git bisect bad HEAD
git bisect good v2.0.0
# Git checks out middle commit — test, mark good/bad, repeat
git bisect reset
Enter fullscreen mode Exit fullscreen mode

Binary search through history. Finds the exact commit that broke something in log₂(n) steps.

Branch Management (5 commands)

11. git branch --merged / --no-merged — Clean up merged branches

git branch --merged | grep -v main | xargs git branch -d
git branch --no-merged  # shows work-in-progress
Enter fullscreen mode Exit fullscreen mode

Safe cleanup. --merged only deletes fully merged branches.

12. git worktree add ../hotfix main — Parallel work without stashing

git worktree add ../hotfix main
cd ../hotfix
# fix critical bug on main
cd ../main
git worktree remove ../hotfix
Enter fullscreen mode Exit fullscreen mode

Multiple working directories from one repo. No stash/unstash dance.

13. git push --force-with-lease — Safe force push

git push --force-with-lease origin feature/auth
Enter fullscreen mode Exit fullscreen mode

Fails if someone else pushed to the branch. Prevents overwriting teammates' work.

14. git fetch --prune — Clean up dead remote refs

git fetch --prune
Enter fullscreen mode Exit fullscreen mode

Removes local tracking branches for deleted remote branches. Run weekly.

15. git maintenance start — Background repo optimization

git maintenance start
Enter fullscreen mode Exit fullscreen mode

Runs gc, commit-graph, prefetch in background. Keeps large repos fast automatically.

More Developer Resources

Top comments (0)