Introduction
Git is essential for modern development. Beyond basic commits and pushes, advanced Git skills improve collaboration and simplify complex scenarios. This guide covers professional Git techniques.
Branching Strategies
Git Flow
main
โโโ develop
โ โโโ feature/login
โ โโโ feature/payment
โโโ hotfix/bugfix
Trunk-Based Development
- Short-lived branches (<2 days)
- Direct commits to main
- Feature flags
GitHub Flow
# Workflow
1. Create branch from main
2. Make changes
3. Open pull request
4. Review and merge
5. Deploy
Advanced Commands
Rebase
Interactive Rebase
# Squash last 3 commits
git rebase -i HEAD~3
# Rebase onto main
git checkout feature
git rebase main
Rebase vs Merge
- Rebase: Clean linear history
- Merge: Preserves context
Cherry-Pick
# Apply specific commit
git cherry-pick abc123
# Cherry-pick without commit
git cherry-pick -n abc123
Stashing
# Save work in progress
git stash push -m "work in progress"
# List stashes
git stash list
# Apply stash
git stash pop
# Create branch from stash
git stash branch feature-branch
Undoing Changes
Reset
# Unstage files
git reset HEAD file.txt
# Soft reset (keep changes staged)
git reset --soft HEAD~1
# Mixed reset (unstage, keep files)
git reset HEAD~1
# Hard reset (destroy changes)
git reset --hard HEAD~1
Revert
# Create new commit undoing changes
git revert abc123
# Revert merge commit
git revert -m 1 abc123
Reflog
# View all reference updates
git reflog
# Recover lost commit
git checkout HEAD@{2}
Submodules
Adding Submodule
git submodule add https://github.com/org/repo.git libs/repo
Working with Submodules
# Clone with submodules
git clone --recurse-submodules repo.git
# Update all submodules
git submodule update --recursive
# Pull in submodules
git pull --recurse-submodules
Advanced Techniques
Bisect
# Start bisect
git bisect start
# Mark current commit (bad)
git bisect bad
# Mark known good commit
git bisect good v1.0.0
# Test, then mark
git bisect good # or bad
git bisect reset # when done
Worktrees
# Create worktree for feature
git worktree add ../feature-branch main
# List worktrees
git worktree list
Filter-Branch / BFG
# Remove sensitive data
bfg --delete-files passwords.txt
git reflog expire --expire=now --all
git gc --prune=now --aggressive
Professional Workflows
Pull Request Best Practices
- Create feature branch
- Make focused commits
- Write good PR description
- Request specific reviewers
- Respond to feedback
- Keep PRs small
Commit Messages
# Conventional commits
feat: add user authentication
fix: resolve login redirect issue
docs: update API documentation
refactor: simplify payment logic
test: add user model tests
# Full format
feat: add payment processing
- Add Stripe integration
- Handle webhooks
- Update user balance
Closes #123
Git Hooks
# .git/hooks/pre-commit
#!/bin/sh
npm test
npm run lint
# .git/hooks/commit-msg
#!/bin/sh
COMMIT_MSG=$(cat "$1")
if ! echo "$COMMIT_MSG" | grep -E "^(feat|fix|docs|test|chore)"; then
echo "Commit message must start with feat, fix, docs, test, or chore"
exit 1
fi
Aliases
# ~/.gitconfig
[alias]
co = checkout
br = branch
st = status
lg = log --oneline --graph --decorate
undo = reset --soft HEAD~1
amend = commit --amend --no-edit
graph = log --oneline --all --graph
GitHub/GitLab Features
Protected Branches
- Require PR reviews
- Status checks pass
- No force push
- Require signed commits
Code Owners
# .github/CODEOWNERS
* @senior-engineer
/src/auth/ @auth-team
/tests/ @qa-team
Troubleshooting
Common Issues
- Detached HEAD
- Merge conflicts
- Lost commits
- Large files
Solutions
# Detached HEAD
git checkout -b fix-branch
git branch -f main HEAD
git checkout main
# Abort merge
git merge --abort
# Large file
git lfs install
git lfs track "*.psd"
Conclusion
Git mastery comes with practice. Learn these advanced techniques, and you’ll handle complex scenarios confidently. Remember: git reflog always has your back.
Comments