This page is not available in every language. Missing translations are disabled in the language selector.

Quick reference

Git Tips & Tricks Cheatsheet

A quick reference guide for essential Git productivity tips: revision refs, submodule sync, diff stat analysis, cherry-picking, and custom aliases.

· Published

A curated compilation of everyday Git shortcuts, inspection flags, submodules commands, and workflow tricks.

Ancestry References

HEAD^       # 1 commit before HEAD
HEAD^^      # 2 commits before HEAD
HEAD~5      # 5 commits before HEAD

Branch Management

# Create and switch to new branch
git checkout -b $branchname
git push origin $branchname --set-upstream

# Checkout remote branch locally
git fetch origin
git checkout --track origin/$branchname

# Prune stale tracking references
git remote prune origin

Submodules

# Clone a repository including all submodules recursively
git clone --recursive <repository-url>

# Initialize and update submodules in existing repo
git submodule update --init --recursive

# Pull latest changes across all submodules
git submodule foreach git pull origin master

Diff & Inspection Tricks

Diffs with Statistics

git diff --stat
git diff --dirstat

Show Changed File Names Only

git diff --name-only
git diff --name-status

Checking Log with Custom Date Formats

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

Cherry-Picking

git cherry-pick <commit-hash>        # Apply commit onto active branch
git cherry-pick -n <commit-hash>     # Stage changes without creating a new commit
git cherry-pick --continue           # Continue after resolving merge conflicts
git cherry-pick --abort              # Abort cherry-pick and return to previous state

Useful Everyday Aliases

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'

References

Related topics