Advanced Git Techniques
Git is more than just basic commits and pushes. Let's explore advanced techniques that will make you a Git power user.
Branching Strategies
Git Flow
A robust branching model for larger projects:
- main: Production-ready code
- develop: Integration branch for features
- feature/*: Individual feature branches
- release/*: Prepare new releases
- hotfix/*: Quick production fixes
GitHub Flow
A simpler workflow for continuous deployment:
- Create a feature branch from main
- Make commits and push regularly
- Open a pull request
- Review and discuss changes
- Merge to main and deploy
Useful Git Commands
# Interactive rebase to clean up commits
git rebase -i HEAD~3
# Stash changes temporarily
git stash push -m "Work in progress"
git stash pop
# Cherry-pick specific commits
git cherry-pick abc123
# Find when a bug was introduced
git bisect start
git bisect bad HEAD
git bisect good v1.0
# View file history
git log --follow -- filename.js
Commit Message Best Practices
Write clear, descriptive commit messages:
# Good commit message format
type(scope): short description
Longer explanation if needed
# Examples
feat(auth): add password reset functionality
fix(api): handle null response in user endpoint
docs(readme): update installation instructions
Merge vs Rebase
- Merge: Preserves commit history, creates merge commits
- Rebase: Creates linear history, rewrites commits
- Rule of thumb: Rebase private branches, merge public ones
Git Hooks
Automate workflows with Git hooks:
- pre-commit: Run tests before commits
- pre-push: Validate before pushing
- post-receive: Deploy after receiving pushes
Mastering these Git techniques will significantly improve your development workflow and team collaboration.