DEV Community

郑沛沛
郑沛沛

Posted on

Git Commands That Will Make You Look Like a Senior Developer

Most developers know add, commit, push. Here are the Git commands and workflows that separate juniors from seniors.

Interactive Rebase: Clean Up Your History

# Squash last 3 commits into one
git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

Your editor opens:

pick abc1234 Add user model
pick def5678 Fix typo in user model
pick ghi9012 Add validation to user model
Enter fullscreen mode Exit fullscreen mode

Change to:

pick abc1234 Add user model
squash def5678 Fix typo in user model
squash ghi9012 Add validation to user model
Enter fullscreen mode Exit fullscreen mode

Result: one clean commit instead of three messy ones.

Stash: Save Work Without Committing

# Save current changes
git stash

# Save with a description
git stash push -m "WIP: user authentication"

# List stashes
git stash list

# Apply most recent stash
git stash pop

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

# Stash only specific files
git stash push -m "partial work" src/auth.py src/models.py
Enter fullscreen mode Exit fullscreen mode

Bisect: Find the Bug-Introducing Commit

git bisect start
git bisect bad          # current commit is broken
git bisect good v1.0.0  # this tag was working

# Git checks out a middle commit. Test it, then:
git bisect good  # if this commit works
git bisect bad   # if this commit is broken

# Git narrows down to the exact commit that introduced the bug
# When done:
git bisect reset
Enter fullscreen mode Exit fullscreen mode

Automate it:

git bisect start HEAD v1.0.0
git bisect run python -m pytest tests/test_auth.py
Enter fullscreen mode Exit fullscreen mode

Cherry-Pick: Grab Specific Commits

# Apply a specific commit from another branch
git cherry-pick abc1234

# Cherry-pick without committing (stage changes only)
git cherry-pick --no-commit abc1234

# Cherry-pick a range
git cherry-pick abc1234..def5678
Enter fullscreen mode Exit fullscreen mode

Reflog: Your Safety Net

# See everything you've done (even "deleted" commits)
git reflog

# Recover a "lost" commit after a bad reset
git reset --hard HEAD@{3}

# Recover a deleted branch
git checkout -b recovered-branch HEAD@{5}
Enter fullscreen mode Exit fullscreen mode

Worktrees: Multiple Branches Simultaneously

# Work on a hotfix without switching branches
git worktree add ../hotfix-branch hotfix/critical-bug

# Now you have two directories, each on a different branch
cd ../hotfix-branch
# fix the bug, commit, push
cd ../main-repo

# Clean up
git worktree remove ../hotfix-branch
Enter fullscreen mode Exit fullscreen mode

Better Diffs and Logs

# Word-level diff (great for prose/docs)
git diff --word-diff

# Show changes in a specific function
git log -p -L :function_name:file.py

# Pretty log with graph
git log --oneline --graph --all --decorate

# Find who changed a line
git blame -L 10,20 src/auth.py

# Search commit messages
git log --grep="fix auth"

# Search code changes
git log -S "def authenticate" --oneline
Enter fullscreen mode Exit fullscreen mode

Aliases for Speed

# Add to ~/.gitconfig
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st "status -sb"
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.undo "reset --soft HEAD~1"
git config --global alias.amend "commit --amend --no-edit"
Enter fullscreen mode Exit fullscreen mode

Commit Message Convention

# Format: type(scope): description
git commit -m "feat(auth): add JWT refresh token support"
git commit -m "fix(api): handle null response from payment gateway"
git commit -m "docs(readme): update installation instructions"
git commit -m "refactor(db): extract query builder into separate module"

# Types: feat, fix, docs, style, refactor, test, chore
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  1. Interactive rebase keeps your history clean and reviewable
  2. Bisect finds bug-introducing commits automatically
  3. Reflog is your safety net — almost nothing is truly lost in Git
  4. Worktrees let you work on multiple branches without stashing
  5. Good commit messages make debugging and reviewing much easier

6. Aliases save hundreds of keystrokes per day

🚀 Level up your AI workflow! Check out my AI Developer Mega Prompt Pack — 80 battle-tested prompts for developers. $9.99

Top comments (0)