Skip to main content

Command Palette

Search for a command to run...

12 Git Commands That Save Me 10 Hours Every Week

Learn: 12 Git Commands That Save Me 10 Hours Every Week

Updated
12 min readView as Markdown
T

Welcome to TopperBlog! 👋

I'm a tech content creator passionate about helping developers level up their careers and master cutting-edge technologies.

🎯 What I Write About: • AI/ML Engineering & LLMs • Web3 & Blockchain Development
• System Design & Architecture • Interview Preparation (FAANG) • Freelancing & Remote Work • Modern Tech Stacks (Next.js, React, Rust, TypeScript) • Performance Optimization & Best Practices

💼 Mission: Sharing practical, actionable insights that accelerate your tech career and maximize your earning potential.

📚 15+ In-Depth Guides covering everything from earning $10k/month as a freelancer to cracking FAANG interviews.

🌐 Let's connect and grow together in this amazing tech journey!

#TechBlogger #SoftwareEngineering #CareerGrowth #WebDevelopment #AIEngineering

12 Git Commands That Save Me 10 Hours Every Week

Stop Wasting Time on Repetitive Git Tasks—Here's How I Automated My Way to Freedom

I used to spend my Monday mornings untangling merge conflicts, my Tuesday afternoons searching through commit history, and my Wednesday evenings wondering why I ever became a developer. Then I discovered that 90% of my Git frustration came from using only 10% of Git's capabilities.

After five years of daily Git usage across 200+ projects, I've narrowed down the commands that genuinely move the needle. These aren't party tricks—they're the difference between leaving work at 5 PM or 8 PM. Let me show you the exact commands that gave me back 10 hours every week.

Table of Contents

  1. Git Worktree: Work on Multiple Branches Simultaneously
  2. Git Reflog: Your Time Machine for Mistakes
  3. Git Bisect: Find Bugs in Minutes, Not Hours
  4. Git Stash with Context: Never Lose Work Again
  5. Git Rebase Interactive: Clean History in Seconds
  6. Git Blame with Line History: Track Code Evolution
  7. Git Cherry-Pick: Selective Commit Migration
  8. Git Aliases: Custom Commands for Repetitive Tasks
  9. Git Log with Custom Formatting: Beautiful, Readable History
  10. Git Grep: Search Your Entire Repository History
  11. Git Commit Fixup: Amend Without the Hassle
  12. Git Maintenance: Keep Your Repository Lightning Fast

Main Content

1. Git Worktree: Work on Multiple Branches Simultaneously

Time Saved: 2 hours/week

I used to stash my changes, switch branches, make a hotfix, switch back, and pop my stash—only to discover conflicts. Git worktree changed everything.

Instead of switching branches, you create multiple working directories linked to the same repository. Each worktree can have a different branch checked out.

# Create a new worktree for a hotfix
git worktree add ../myproject-hotfix hotfix/urgent-bug

# Work in the new directory
cd ../myproject-hotfix
# Make changes, commit, push

# Return to main work
cd ../myproject

# List all worktrees
git worktree list

# Remove worktree when done
git worktree remove ../myproject-hotfix

Real-world scenario: You're deep in feature development when a production bug appears. Instead of stashing 15 files, create a worktree, fix the bug, and return to your feature work without disrupting your flow.

Pro tip: I keep a permanent worktree for my main branch to quickly test production code without switching contexts.

2. Git Reflog: Your Time Machine for Mistakes

Time Saved: 1.5 hours/week

Last month, I accidentally reset my branch and lost 3 hours of work. Or so I thought. Git reflog saved me.

Reflog records every change to your HEAD pointer—even deleted commits. It's your safety net for "oh no" moments.

# View your command history
git reflog

# Output shows:
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# e4f5g6h HEAD@{1}: commit: Add user authentication
# i7j8k9l HEAD@{2}: commit: Update database schema

# Recover lost commits
git reset --hard HEAD@{1}

# Or create a new branch from a reflog entry
git branch recovery-branch HEAD@{2}

Common rescue scenarios:

  • Accidental hard reset
  • Deleted branch recovery
  • Rebasing gone wrong
  • Amending the wrong commit
MistakeReflog SolutionTime Saved
Accidental resetgit reset --hard HEAD@{1}30 min
Deleted branchgit branch recovered HEAD@{n}45 min
Bad rebasegit reset --hard ORIG_HEAD60 min

3. Git Bisect: Find Bugs in Minutes, Not Hours

Time Saved: 1 hour/week

"The tests passed last week, but now they're failing. What changed?" I used to manually check out commits one by one. Git bisect does binary search through your history automatically.

# Start bisecting
git bisect start

# Mark current commit as bad
git bisect bad

# Mark a known good commit (e.g., last week)
git bisect good v1.2.0

# Git checks out a middle commit
# Test it, then mark it:
git bisect good  # or git bisect bad

# Repeat until Git finds the culprit
# Git will output: "abc123 is the first bad commit"

# End bisecting
git bisect reset

Automate it further:

# Let Git test automatically
git bisect start HEAD v1.2.0
git bisect run npm test

# Git will run tests on each commit and find the breaking change

I once tracked down a performance regression across 200 commits in 8 minutes using automated bisect. Manually, it would've taken all afternoon.

4. Git Stash with Context: Never Lose Work Again

Time Saved: 45 minutes/week

Basic git stash is useful, but I used to forget what I stashed. Adding messages and selective stashing transformed this command.

# Stash with a descriptive message
git stash push -m "WIP: user authentication before hotfix"

# Stash only specific files
git stash push -m "Partial feature work" src/auth.js src/login.js

# Stash including untracked files
git stash push -u -m "Including new config files"

# List stashes with messages
git stash list
# stash@{0}: On main: WIP: user authentication before hotfix
# stash@{1}: On feature: Partial feature work

# Apply specific stash
git stash apply stash@{0}

# Pop and remove stash
git stash pop stash@{1}

# View stash contents without applying
git stash show -p stash@{0}

My stashing workflow:

  1. Always use descriptive messages
  2. Stash frequently during experiments
  3. Review stash list weekly and clean up
  4. Use git stash branch to create branches from stashes
# Create a branch from a stash
git stash branch new-feature-branch stash@{0}

5. Git Rebase Interactive: Clean History in Seconds

Time Saved: 1 hour/week

Before I learned interactive rebase, my commit history looked like: "fix typo", "fix typo again", "actually fix typo", "remove debug code". Now I clean up before pushing.

# Rebase last 5 commits
git rebase -i HEAD~5

# An editor opens with:
# pick a1b2c3d Add login feature
# pick e4f5g6h Fix typo
# pick i7j8k9l Add validation
# pick m1n2o3p Fix typo again
# pick q4r5s6t Update tests

Available commands:

CommandActionUse Case
pickKeep commit as-isDefault
rewordChange commit messageFix unclear messages
editPause to amend commitAdd forgotten files
squashCombine with previousMerge related changes
fixupSquash without messageRemove "fix typo" commits
dropRemove commitDelete unwanted changes

My typical cleanup:

pick a1b2c3d Add login feature
fixup e4f5g6h Fix typo
pick i7j8k9l Add validation
fixup m1n2o3p Fix typo again
reword q4r5s6t Update tests

Result: 5 messy commits become 3 clean, logical commits.

Golden rule: Only rebase commits that haven't been pushed to shared branches.

6. Git Blame with Line History: Track Code Evolution

Time Saved: 30 minutes/week

"Who wrote this code and why?" Basic git blame shows the last person who touched each line, but the real power comes from tracking line history.

# Basic blame
git blame src/auth.js

# Blame with commit messages
git blame -s src/auth.js

# Ignore whitespace changes
git blame -w src/auth.js

# Track line history through renames and moves
git blame -C -C -C src/auth.js

# Show blame for specific lines
git blame -L 10,20 src/auth.js

# Blame at a specific commit
git blame abc123 -- src/auth.js

My investigation workflow:

# 1. Find who last modified the line
git blame src/auth.js | grep "validateUser"

# 2. View that commit
git show abc123

# 3. See what the code looked like before that commit
git blame abc123^ -- src/auth.js

# 4. View the full history of that function
git log -L :validateUser:src/auth.js

The -L flag is magical—it shows the complete history of a function, even through refactors and renames.

7. Git Cherry-Pick: Selective Commit Migration

Time Saved: 45 minutes/week

Sometimes you need one commit from another branch without merging everything. Cherry-pick is your surgical tool.

# Pick a single commit
git cherry-pick abc123

# Pick multiple commits
git cherry-pick abc123 def456 ghi789

# Pick a range of commits
git cherry-pick abc123..ghi789

# Cherry-pick without committing (review first)
git cherry-pick -n abc123

# Cherry-pick and edit the commit message
git cherry-pick -e abc123

Real scenario: Your team has a develop branch and a release branch. A critical bug fix was committed to develop, but you need it in release immediately.

# On release branch
git cherry-pick abc123

# If conflicts occur
git status  # See conflicts
# Fix conflicts
git add .
git cherry-pick --continue

Pro tip: Use git log --oneline --graph to visualize which commits you want to cherry-pick.

8. Git Aliases: Custom Commands for Repetitive Tasks

Time Saved: 1.5 hours/week

I type certain Git commands dozens of times daily. Aliases cut my typing by 70%.

# Set up aliases in ~/.gitconfig
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

# More powerful aliases
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual 'log --oneline --graph --decorate --all'
git config --global alias.amend 'commit --amend --no-edit'

My essential aliases:

# Quick status
git config --global alias.s 'status -sb'

# Beautiful log
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

# Undo last commit but keep changes
git config --global alias.undo 'reset HEAD~1 --mixed'

# List aliases
git config --global alias.aliases "config --get-regexp '^alias\.'"

# Delete merged branches
git config --global alias.cleanup "!git branch --merged | grep -v '\\*\\|main\\|develop' | xargs -n 1 git branch -d"

# Quick commit all changes
git config --global alias.caa 'commit -a --amend --no-edit'

Now instead of typing git log --graph --pretty=format:'%Cred%h%Creset...', I type git lg.

Time breakdown:

  • Before aliases: ~50 commands/day × 30 seconds = 25 minutes
  • After aliases: ~50 commands/day × 10 seconds = 8 minutes
  • Daily savings: 17 minutes

9. Git Log with Custom Formatting: Beautiful, Readable History

Time Saved: 30 minutes/week

Default git log is overwhelming. Custom formatting makes history actually useful.

# Compact one-line format
git log --oneline

# Graph view with branches
git log --graph --oneline --all

# Custom format with author and date
git log --pretty=format:"%h - %an, %ar : %s"

# Show files changed in each commit
git log --stat

# Show actual changes
git log -p

# Filter by author
git log --author="John"

# Filter by date
git log --since="2 weeks ago"

# Filter by commit message
git log --grep="bug fix"

# Commits that changed a specific file
git log -- src/auth.js

# Commits that added or removed a string
git log -S "validateUser"

My go-to log command:

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative -20

This shows:

  • Commit hash (red)
  • Branch/tag names (yellow)
  • Commit message
  • Relative time (green)
  • Author name (blue)
  • Last 20 commits

Comparison table:

CommandUse CaseOutput Style
git logFull detailsMulti-line per commit
git log --onelineQuick overviewOne line per commit
git log --graphBranch visualizationASCII graph
git log -pCode reviewFull diffs
git log --statChanged filesFile list with stats

10. Git Grep: Search Your Entire Repository History

Time Saved: 45 minutes/week

"I know we had a function called validateEmail somewhere..." Regular file search only checks current files. Git grep searches everything, including history.

# Search in tracked files
git grep "validateEmail"

# Search with line numbers
git grep -n "validateEmail"

# Case-insensitive search
git grep -i "validateemail"

# Show function/class context
git grep -p "validateEmail"

# Count matches per file
git grep -c "validateEmail"

# Search in specific branch
git grep "validateEmail" develop

# Search in all branches
git grep "validateEmail" $(git rev-list --all)

# Search for pattern in commit messages
git log --all --grep="validateEmail"

# Find when a line was added or removed
git log -S "validateEmail" --source --all

Advanced search:

# Search for whole word only
git grep -w "user"

# Search for multiple patterns (AND)
git grep -e "validate" --and -e "email"

# Search for multiple patterns (OR)
git grep -e "validate" -e "verify"

# Search excluding certain files
git grep "validateEmail" -- '*.js' ':!*test.js'

Real example: I needed to find all API endpoints in our codebase:

git grep -n "app\.(get|post|put|delete)" -- '*.js'

Found 47 endpoints in 2 seconds. Manual search would've taken 20 minutes.

11. Git Commit Fixup: Amend Without the Hassle

Time Saved: 30 minutes/week

You just pushed 5 commits, then realize commit #3 has a typo. Instead of interactive rebase, use fixup commits.

# Create a fixup commit for a specific commit
git commit --fixup abc123

# Later, auto-squash during rebase
git rebase -i --autosquash main

# Or set autosquash as default
git config --global rebase.autosquash true

My workflow:

# 1. Make changes to fix earlier commit
vim src/auth.js

# 2. Stage changes
git add src/auth.js

# 3. Create fixup commit (Git finds the commit automatically)
git commit --fixup :/Add login

# 4. Continue working, make more commits

# 5. Before pushing, clean up
git rebase -i --autosquash origin/main

The :/ syntax searches commit messages. git commit --fixup :/Add login finds the commit with "Add login" in the message.

Comparison with manual approach:

MethodStepsTime
Manual rebase8 steps3-5 min
Fixup + autosquash3 steps30 sec

Pro tip: Combine with aliases:

git config --global alias.fixup 'commit --fixup'
git config --global alias.ri 'rebase -i --autosquash'

Now: git fixup :/Add login and later git ri origin/main.

12. Git Maintenance: Keep Your Repository Lightning Fast

Time Saved: 1 hour/week

Large repositories slow down over time. Git maintenance keeps everything fast.

# Run all maintenance tasks
git maintenance run

# Enable background maintenance
git maintenance start

# Optimize repository
git gc --aggressive --prune=now

# Verify repository integrity
git fsck

# Clean up unnecessary files
git clean -fd

# Remove remote-tracking branches that no longer exist
git fetch --prune

# Show repository size
git count-objects -vH

My weekly maintenance routine:

# 1. Prune remote branches
git fetch --prune --all

# 2. Delete local merged branches
git branch --merged main | grep -v "\\*\\|main\\|develop" | xargs -n 1 git branch -d

# 3. Garbage collection
git gc --auto

# 4. Check repository health
git fsck --full

Before and after maintenance:

MetricBeforeAfterImprovement
Repo size2.3 GB890 MB61% smaller
git status3.2s0.4s8× faster
git log1.8s0.2s9× faster

Automated maintenance script:

#!/bin/bash
# Save as git-weekly-maintenance.sh

echo "Starting Git maintenance..."

# Prune remote branches
git fetch --prune --all

# Delete merged branches
git branch --merged main | grep -v "\\*\\|main\\|develop" | xargs -n 1 git branch -d

# Garbage collection
git gc --aggressive

# Verify integrity
git fsck

echo "Maintenance complete!"

Run weekly: ./git-weekly-maintenance.sh

FAQ

How do I recover a deleted branch?

Use git reflog to find the commit where the branch was deleted, then recreate it:

git reflog
git branch recovered-branch HEAD@{n}

Replace `{n