Day 30: Sharing Some Useful Git Operation Tips from Work
After 29 days of learning Git, this final article shares practical tips and tricks that make daily Git operations more efficient and enjoyable.
Configuration Tips
Global Configuration
# Set default editor
git config --global core.editor "code --wait"
# Enable color UI
git config --global color.ui auto
# Set default branch name
git config --global init.defaultBranch main
# Auto-correct typos
git config --global help.autocorrect 1
# Cache credentials
git config --global credential.helper cache --timeout=3600
Useful Aliases
# Short status
git config --global alias.st status
# Short log
git config --global alias.ll "log --oneline --graph --all --decorate"
# Last commit
git config --global alias.last "log -1 HEAD"
# Undo last commit (keep changes)
git config --global alias.undo "reset HEAD~1 --mixed"
# Amend without editing message
git config --global alias.amend "commit --amend --no-edit"
# List branches sorted by last modified
git config --global alias.br "branch --sort=-committerdate"
# Show changed files in last commit
git config --global alias.dl "diff --name-only HEAD~1"
# Search commit messages
git config --global alias.search "log --all --grep"
# Nuke branch (force delete)
git config --global alias.nuke "branch -D"
Productivity Tips
Quick Navigation
# Jump to repository root
alias gr='cd $(git rev-parse --show-toplevel)'
# Open repo in browser
alias browse='git remote get-url origin | xargs open'
Searching
# Find file in history
git log --all --full-history -- "**/filename.*"
# Search in all commits
git log -S "search term" --source --all
# Find who changed a line
git blame filename
# Prettier blame
git blame -w -C -C -L 10,20 filename
Cleanup
# Delete merged branches
git branch --merged main | grep -v "^*\|main" | xargs git branch -d
# Delete all local branches except main
git branch | grep -v "main" | xargs git branch -D
# Remove untracked files (dry run first!)
git clean -n
git clean -fd
# Prune remote branches
git fetch --prune
Time-Saving Commands
Quick Commits
# Add and commit in one step
git commit -am "Quick fix"
# Add, commit, and push
alias gcp='git commit -am "$1" && git push'
Branch Management
# Create and checkout branch in one step
git checkout -b feature/new-feature
# Switch to previous branch
git checkout -
# Show branches with last commit
git branch -v
# Show remote tracking branches
git branch -vv
Stash Tricks
# Stash with message
git stash push -m "Work in progress on feature X"
# List stashes with details
git stash list --pretty=format:"%C(yellow)%gd%C(reset) - %C(bold)%s%C(reset)"
# Apply and drop in one step
git stash pop
# Apply specific stash
git stash apply stash@{2}
# Stash including untracked files
git stash -u
Advanced Techniques
Cherry-Pick Multiple Commits
# Cherry-pick a range of commits
git cherry-pick A^..B
# Cherry-pick from another branch
git cherry-pick feature-branch~3..feature-branch
Interactive Staging
# Stage parts of a file
git add -p filename
# Interactive add
git add -i
Diff Tips
# Word diff instead of line diff
git diff --word-diff
# Ignore whitespace changes
git diff -w
# Show diff of staged changes
git diff --staged
# Diff between branches
git diff main..feature-branch
# Diff specific file between commits
git diff commit1 commit2 -- filename
Log Formatting
# Pretty log
git log --pretty=format:"%C(yellow)%h%C(reset) - %C(green)(%cr)%C(reset) %s %C(blue)<%an>%C(reset)"
# Log with graph
git log --graph --abbrev-commit --decorate --all
# Show files changed per commit
git log --stat
# Show patches
git log -p
Troubleshooting
Undo Mistakes
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Undo all uncommitted changes
git checkout .
# Unstage file
git reset HEAD filename
# Restore file from HEAD
git checkout HEAD -- filename
Recovery
# Find lost commits
git reflog
# Recover deleted branch
git branch branch-name <commit-hash>
# Find dangling commits
git fsck --lost-found
Debug
# See who introduced a bug (binary search)
git bisect start
git bisect bad # Current version is bad
git bisect good <commit> # Known good commit
# Git checks out middle commit
# Test and mark as good or bad
git bisect good/bad
# Repeat until found
git bisect reset
# Blame with line range
git blame -L 10,20 filename
# Show commit that introduced line
git log -S "function name" --source --all
Workflow Optimizations
Pre-Commit Hooks
Create .git/hooks/pre-commit:
#!/bin/bash
# Run tests before commit
npm test
if [ $? -ne 0 ]; then
echo "Tests failed, commit aborted"
exit 1
fi
Commit Message Template
Create ~/.git-commit-template:
# <type>: <subject>
#
# <body>
#
# <footer>
#
# Types: feat, fix, docs, style, refactor, test, chore
Set as default:
git config --global commit.template ~/.git-commit-template
Integration Tips
Git with VS Code
# Open current repo in VS Code
code .
# Open merge tool
git mergetool --tool=vscode
Git with Vim
# Set Vim as default editor
git config --global core.editor vim
# Vim as diff tool
git config --global diff.tool vimdiff
git config --global difftool.prompt false
Performance Tips
Large Repositories
# Shallow clone (faster)
git clone --depth 1 https://github.com/user/repo.git
# Partial clone (Git 2.22+)
git clone --filter=blob:none https://github.com/user/repo.git
# Sparse checkout (specific folders)
git sparse-checkout init
git sparse-checkout set folder1 folder2
Faster Status
# Use untracked cache
git config core.untrackedCache true
# Use file system monitor
git config core.fsmonitor true
Security Tips
GPG Signing
# Generate GPG key
gpg --gen-key
# List keys
gpg --list-secret-keys --keyid-format LONG
# Configure Git
git config --global user.signingkey <key-id>
git config --global commit.gpgsign true
# Sign commits
git commit -S -m "Signed commit"
Credentials
# Never store passwords in config
# Use credential helpers instead
# SSH for authentication (recommended)
ssh-keygen -t ed25519 -C "email@example.com"
# Use HTTPS with token
# Generate personal access token on GitHub
# Use it as password
Final Wisdom
Best Practices Checklist
- Commit often, push regularly
- Write meaningful commit messages
- Keep commits atomic and focused
- Branch for every feature/fix
- Review your own code before PR
- Pull before push
- Never force push to shared branches
- Use .gitignore properly
- Tag releases
- Document your workflow
When Things Go Wrong
- Don’t panic: Git rarely loses data
- Check reflog: It’s your safety net
- Make a backup: Branch before experimenting
- Ask for help: Git community is helpful
- Learn from mistakes: Each error teaches something
Congratulations!
You’ve completed 30 days of Git mastery! You’ve learned:
- Git fundamentals and architecture
- Branching and merging strategies
- History manipulation (reset, rebase, amend)
- Remote workflows and collaboration
- GitHub workflows and best practices
- Advanced tips and troubleshooting
Keep Learning
- Practice regularly
- Contribute to open source
- Learn from others’ Git history
- Explore advanced features
- Stay updated with Git releases
Final Thoughts
Git is a powerful tool that becomes more valuable the more you use it. Don’t try to remember everything - bookmark this guide and refer back as needed. The most important thing is to keep practicing and not be afraid to experiment (in a safe environment).
Happy coding, and may your merges always be conflict-free!
Reference Links
Thank You!
Thank you for following this 30-day Git journey. Keep practicing, keep learning, and remember - every Git expert was once a beginner!