Git is more than just a version control system – it’s the backbone of modern software development. While most developers know the basics (git add, git commit, git push), mastering Git’s advanced features can dramatically improve your workflow and make you a more effective developer.
This guide covers the essential advanced Git techniques that every developer should know, from handling complex merge conflicts to leveraging powerful commands like git reflog and git worktree.
Resolving Merge Conflicts Like a Pro
Merge conflicts are inevitable in collaborative development. Here’s how to handle them systematically:
Feature Branch to Main Conflicts
When your feature branch conflicts with main, resolve it locally before merging:
# Switch to main and get latest changes
git switch main
git pull origin main
# Switch to your feature branch
git switch feature-a
# Merge main into your feature branch
git merge main
After resolving conflicts in your editor:
# Stage the resolved files
git add .
# Commit the merge resolution
git commit -m "Resolve merge conflicts with main"
# Push the updated feature branch
git push
Pro tip: Always merge the target branch into your feature branch first, rather than the other way around. This keeps the main branch clean and makes the final merge a fast-forward.
Production Branch Conflicts
For critical branches like production, use a dedicated conflict resolution branch:
# Ensure you have the latest changes
git switch main
git pull origin main
git switch prod
git pull origin prod
# Create a dedicated conflict resolution branch
git checkout -b resolve-main-to-prod-conflict
# Merge main into the resolution branch
git merge main
After resolving conflicts:
git add .
git commit -m "Resolve conflicts for main to prod merge"
git push
# Now merge the resolution branch into prod
git switch prod
git merge resolve-main-to-prod-conflict
This approach keeps your production branch stable and provides a clear audit trail of conflict resolutions.
Cherry-Picking: Surgical Commit Selection
Cherry-picking allows you to apply specific commits from one branch to another – perfect for hotfixes or selective feature deployment.
Basic Cherry-Pick Workflow
# Find the commit you want to cherry-pick
git --no-pager log main --oneline --graph
# Switch to target branch
git checkout prod
git pull origin prod
# Create a branch for the cherry-pick
git checkout -b cherry-pick-hotfix
# Apply the specific commit
git cherry-pick abc1234
Advanced Cherry-Pick Techniques
Cherry-pick without committing immediately:
git cherry-pick abc1234 --no-commit
# Make additional changes if needed
git commit -m "Cherry-picked and modified commit abc1234"
See what’s been cherry-picked:
git cherry main prod
This shows commits in main that haven’t been applied to prod (marked with +) and those that have (marked with -).
Git Log: Your Repository’s Time Machine
The git log command is incredibly powerful when you know its options:
Filter by Author and Date
# Commits by specific author
git log --author="Alexander Felipe Øines" --oneline
# Commits from last week
git log --since="1 week ago"
# Commits between specific dates
git log --since="2024-01-01" --until="2024-12-31"
File-Specific History
# History of a specific file
git log --oneline -- README.md
# See what changed in each commit
git log --stat -- src/components/
# Full diff for each commit
git log -p -- package.json
Advanced Log Filtering
# Only merge commits
git log --merges --oneline
# Commits with specific message content
git log --grep="bugfix" --oneline
# Visual branch history
git log --graph --oneline --all
# Last 10 commits with file changes
git log -n 10 --stat
Reset Strategies: Undoing Changes Safely
Understanding Git’s reset options is crucial for managing your commit history:
Soft Reset: Uncommit but Keep Changes
git reset --soft HEAD~1
This moves HEAD back one commit but keeps your changes staged. Perfect for amending commits or splitting large commits.
Hard Reset: Nuclear Option
# Reset to match remote exactly
git fetch origin
git reset --hard origin/main
Warning: This permanently destroys local changes. Use with extreme caution.
Mixed Reset (Default)
git reset HEAD~1
Moves HEAD back and unstages changes, but keeps them in your working directory.
Git Reflog: Your Safety Net
The reflog is Git’s safety net – it tracks every action that moves HEAD, even “lost” commits:
# See all recent actions
git reflog
# Recover a "lost" commit
git checkout abc1234 # from reflog output
git checkout -b recover-lost-work
Clean up old reflog entries:
git reflog expire --expire=now --all
Advanced Git Techniques
Git Blame: Detective Work
Find out who changed what and when:
git blame src/components/NavBar.tsx
This shows line-by-line authorship and commit information.
Git Stash: Temporary Storage
Save work in progress without committing:
# Stash with descriptive message
git stash push -m "WIP: Refactoring authentication logic"
# List all stashes
git stash list
# Apply specific stash
git stash apply stash@{1}
# Apply and remove from stash
git stash pop
Git Worktree: Multiple Branches Simultaneously
Work on multiple branches without constant switching:
# Create new worktree for feature branch
git worktree add ../feature-worktree feature-branch
# List all worktrees
git worktree list
# Remove worktree when done
git worktree remove ../feature-worktree
This is incredibly useful for comparing branches or working on multiple features simultaneously.
Git Clean: Housekeeping
Remove untracked files and directories:
# Remove untracked files
git clean -f
# Remove untracked files and directories
git clean -fd
# Dry run to see what would be deleted
git clean -n
Always run with -n first to preview what will be deleted!
Useful Git Utilities
Repository Information
# Current commit hash
git rev-parse HEAD
# Repository root directory
git rev-parse --show-toplevel
# Current branch name
git rev-parse --abbrev-ref HEAD
Branch Management
# See local branches with tracking info
git branch -vv
# See which branches are merged
git branch --merged
# Delete merged branches (except main/master)
git branch --merged | grep -v "main\|master" | xargs -n 1 git branch -d
Commit Analysis
# Detailed commit information
git show HEAD
# Summary by author
git shortlog -s -n
# See staged changes
git diff --staged
Best Practices and Pro Tips
- Always create feature branches – Never work directly on main/master
- Use descriptive commit messages – Your future self will thank you
- Commit early and often – Small, focused commits are easier to review and revert
- Use
git stashliberally – Don’t lose work when switching contexts - Learn your editor’s Git integration – VSCode, Vim, and others have excellent Git support
- Set up aliases for common commands:
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'
git config --global alias.visual '!gitk'
Conclusion
Mastering Git is a journey, not a destination. These advanced techniques will make you more productive and confident when working with version control. The key is to practice these commands in safe environments and gradually incorporate them into your daily workflow.
Remember: Git is incredibly powerful, but with great power comes great responsibility. Always double-check destructive operations, and when in doubt, create a backup branch first.
The investment in learning these advanced Git techniques pays dividends in productivity, collaboration, and peace of mind. Your code – and your teammates – will thank you for it.
Resources for Further Learning
- Official Git Documentation – git-scm.com
- Pro Git Book – Free online book covering Git in depth
- Git Aliases – Set up shortcuts for your most-used commands
- Interactive Git Tutorial – learngitbranching.js.org
Happy coding, and may your merges always be conflict-free! 🚀
