Git Beyond the Basics
Most developers know `git add`, `git commit`, and `git push`. But in a fast-paced team, basic Git isn't enough. A messy git history with hundreds of "fix typo" commits makes code review and debugging a nightmare. Here is how to master Git for professional collaboration.
1. Choose the Right Branching Strategy
- **Git Flow:** Best for traditional release cycles with distinct releases.
- **GitHub Flow:** Simple, feature-branch-based workflow that is perfect for continuous deployment.
- **Trunk-Based Development:** Developers merge small, frequent updates to a single branch ("trunk"), minimizing merge conflicts.
2. Use Interactive Rebase for Clean History
Before opening a pull request, clean up your commits using `git rebase -i`. This lets you: - **Squash:** Combine multiple minor commits into a single logical commit. - **Reword:** Edit commit messages for clarity. - **Fixup:** Merge a commit into its predecessor without changing the message.
Command: `git rebase -i main`
3. The Power of Cherry-Picking
If you need to apply a specific bugfix commit from a development branch to production without merging the entire branch, use `git cherry-pick <commit-hash>`. It cleanly copies that change onto your current branch.
4. Resolving Merge Conflicts Safely
When conflicts occur: 1. Don't panic. 2. Rebase your feature branch against the updated target branch (`git pull --rebase origin main`). 3. Use a visual merge tool (like VS Code or GitKraken) to resolve conflicts file by file. 4. Run your test suite before completing the rebase (`git rebase --continue`).
5. Enforce Quality with Git Hooks
Use pre-commit and pre-push hooks to automatically run linter checks, format code, and execute unit tests before any code leaves your local machine. This guarantees that broken code never reaches the shared repository.
Clean commits reflect a structured mind. By mastering these workflows, you make your code reviews faster and your deployments safer!