Introduction
Git is the backbone of modern software collaboration. Adopting best practices ensures code quality, smooth teamwork, and reliable releases.
Git Workflows
- Feature Branch Workflow: Each feature/fix in its own branch.
- Gitflow Workflow: Structured branching for releases, hotfixes, and features.
- Forking Workflow: Fork the repo, work independently, then submit pull requests.
Branching Strategies
- Use clear branch names (e.g.,
feature/login,bugfix/header) - Protect main branches (
main,develop) - Delete merged branches to keep repo clean
Pull Requests
- Open PRs for all changes
- Describe what and why in the PR description
- Tag reviewers and assign appropriately
Code Review
- Review for logic, style, and security
- Be constructive and respectful
- Approve only when all checks pass
Commit Message Guidelines
- Use present tense:
Fix bug,Add feature - Keep messages concise but descriptive
- Reference issues/tickets when relevant
Handling Merge Conflicts
- Pull latest changes before merging
- Resolve conflicts locally, test thoroughly
- Communicate with team if unsure
Release Management
- Tag releases (
v1.0.0) - Maintain changelogs
- Automate deployments when possible
Automation & CI/CD
- Use CI/CD pipelines for testing and deployment
- Automate code formatting and linting
- Integrate with issue tracking
Security & Access Control
- Limit write access to main branches
- Use signed commits for sensitive changes
- Audit repository for secrets and credentials
Rebasing vs. Merging
Rebase rewrites history for a linear commit tree, while merge preserves all branch history. Use git rebase for clean history, git merge for context.
git checkout feature
# Merge
git merge main
# Rebase
git rebase main
Stashing Changes
Temporarily save changes without committing using git stash.
git stash
# List stashes
git stash list
# Apply last stash
git stash apply
Submodules
Include other repositories inside your project.
git submodule add https://github.com/example/lib.git lib
Git Hooks
Automate tasks (linting, tests) on events like commit or push.
.git/hooks/pre-commit
# Add shell script to run tests before commit
Tagging & Versioning
Tag releases for easy reference.
git tag v1.0.0
git push origin v1.0.0
Bisecting for Bug Hunting
Use git bisect to find the commit that introduced a bug.
git bisect start
git bisect bad
git bisect good
Cherry-picking Commits
Apply a specific commit from one branch to another.
git cherry-pick
Managing Large Files (Git LFS)
Track large files efficiently using Git LFS.
git lfs install
git lfs track "*.psd"
Git Aliases
Create shortcuts for common commands.
git config --global alias.co checkout
git co main
Undoing Changes
Reset, revert, or restore changes as needed.
git reset --hard HEAD~1
# Undo last commit
git revert
# Restore file
git restore file.txt
Working with Remotes
Add, remove, and manage remote repositories.
git remote add origin https://github.com/user/repo.git
git remote -v
Git Configuration & Customization
Customize Git behavior and settings.
git config --global user.name "Your Name"
git config --global core.editor "vim"
Best Practices for Open Source Collaboration
- Fork and submit pull requests
- Follow contribution guidelines
- Communicate clearly in issues and PRs
Further Resources
- Pro Git Book — The official, comprehensive guide to Git, covering everything from basics to advanced workflows.
- Atlassian Git Tutorials — Practical tutorials and guides for real-world Git usage, including branching, merging, and collaboration.
- Gitflow Model — Detailed explanation of the Gitflow branching strategy for managing features, releases, and hotfixes.
- Learn Git Branching (Interactive) — Visual, hands-on tool for mastering branching and merging in Git.
- Oh My Git! — Open-source interactive Git learning game.
- Git Cheat Sheet — Quick reference for common Git commands.