Introduction
Git is the industry standard for version control. This comprehensive guide covers everything from initial setup to advanced team workflows, helping you collaborate effectively and maintain clean project history.
Git Fundamentals and Setup
Configure Git: git config --global user.name "Your Name", git config --global user.email "email@example.com". Set default editor: git config --global core.editor "vim". Create SSH keys for secure GitHub access: ssh-keygen -t ed25519 -C "your_email@example.com".
Basic Commands and Workflow
The Git workflow: git init (new repo), git add (stage changes), git commit -m "message" (snapshot), git status (check status), git log (view history). Understand working directory, staging area, and repository commits.
Branching Strategies That Work
Branches enable parallel development. Git Flow: main (production), develop (integration), feature/* (new features), release/* (preparation), hotfix/* (emergency fixes). GitHub Flow: main branch always deployable, feature branches for everything, pull requests for review.
Merging vs Rebasing
Merge preserves history but creates merge commits: git merge feature-branch. Rebase creates linear history: git rebase main. Golden rule: Don't rebase public branches. Use interactive rebase (git rebase -i) to squash, reorder, or edit commits before merging.
Resolving Merge Conflicts
Conflicts occur when same lines change differently. Git marks conflicts with <<<<<<<, =======, >>>>>>>. Manually edit to resolve, then git add and git commit. Use merge tools (vimdiff, meld, VSCode). Prevent conflicts with frequent pulls and small, focused changes.
GitHub Collaboration Features
Pull requests enable code review before merging. Use draft PRs for work in progress. Request reviewers, add assignees, link issues. Protect main branch with required status checks, minimum reviewers, and linear history requirement.
Git Hooks and Automation
Client-side hooks run on local operations: pre-commit (linting), commit-msg (conventional commits), pre-push (tests). Server-side hooks on GitHub: pre-receive, post-receive. Use Husky for Node.js projects to manage Git hooks easily.
GitHub Actions CI/CD
Automate testing and deployment with GitHub Actions. Create .github/workflows/main.yml. Define triggers (push, pull_request), jobs (runs-on, steps), and actions (checkout, setup-node, npm test). Deploy to servers, Docker registries, or cloud platforms automatically.
Advanced Git Techniques
Use git bisect to find bug-introducing commits. Git cherry-pick to apply specific commits. Git reflog to recover lost commits. Git stash to temporarily save changes. Git submodules or subtree for external dependencies. Git worktree for multiple branches simultaneously.
Repository Management Best Practices
Write meaningful commit messages (imperative tense, 50 char subject, blank line, detailed body). Keep commits atomic (one logical change). Use .gitignore effectively. Tag releases with semantic versioning (v1.2.3). Regular garbage collection (git gc) for repository optimization.
Conclusion
Git mastery requires continuous practice. Start with basic commands, then adopt branching strategies, and finally implement CI/CD automation. Good version control habits enable confident experimentation and smooth team collaboration.