Skip to content

    Git Tips & Tricks

    Quick Git aliases and commands for daily use.

    Quick recipes

    # Triage with bisect
    git bisect start
    git bisect bad HEAD
    git bisect good <known-good-commit>
    # run your test, then mark good/bad each step
    git bisect reset
    
    # Switch branch or restore paths (Git 2.23+)
    git switch main
    git restore path/to/file
    
    # Generate annotated release tag
    git tag -a v1.2.0 -m "Release v1.2.0"
    git push origin v1.2.0
    
    # Changelog (last 30 days)
    git log --since='30 days ago' --pretty='* %s (%h)'

    Production checklist

    • Use annotated tags for releases; push tags explicitly.
    • Adopt a conventional commit style for clean changelogs.
    • Agree force-push policies; use protected branches in shared repos.

    Pretty Log Format with Graph

    Colorized graph log with one-line commits via a global alias.

    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"

    Usage: Run git lg after configuring the alias.

    Show Changed Files in Commits

    One line per commit with hash and per-file status (A/M/D).

    git log --name-status --oneline

    Example output: Each commit lists files with status indicators (A=added, M=modified, D=deleted)

    Undo Last Commit (Keep Changes)

    Moves HEAD back one commit but keeps changes staged for a new commit.

    Warning: Do not use after pushing that commit to a shared branch. If others may have pulled it, coordinate or use revert instead.
    git reset --soft HEAD~1

    Note: Staged changes stay in the index so you can amend the message or add files.

    Search Commit Messages

    Search commit subjects and bodies across all refs that match the pattern.

    git log --all --grep='search term'

    Example: git log --all --grep='bugfix' lists commits mentioning "bugfix"

    Show Complete File History

    Patches per commit for one path; optional rename tracking.

    git log -p -- path/to/file

    Rename follow: git log -p --follow -- path/to/file