Branching & Merging: Parallel Universes
In Git, a branch is simply a movable pointer to one of your commits. The default branch name in Git is main.
Merge Types: Fast-Forward vs. Recursive
- Fast-Forward: If the current branch has no new commits, Git just moves the pointer forward. No merge commit is created.
- Recursive (3-way merge): If the branches have diverged, Git creates a new "Merge Commit" that has two parents.
Resolving Conflicts
Conflicts happen when two people change the same line of the same file. Git cannot decide which version is correct.
Step-by-Step Resolution:
- Identify conflicting files with
git status. - Open the file and look for markers:
<<<<<<< HEADand>>>>>>> branch-name. - Choose the correct code and remove markers.
git addandgit committo finalize.
The Professional Perspective (Staff Tier)
In a high-velocity engineering team, Git is more than a command-line tool; it is a Communication Protocol. Your commits tell a story. If that story is "fixed bug," "update," "test," you are failing your team. A professional engineer writes commits that provide context, intent, and rationale.
1. Data Integrity and Safety
Git is designed as a directed acyclic graph (DAG) of objects. Understanding that every commit is a unique snapshot of the entire project—not just a diff—is the "Aha!" moment of version control. This means when you are 'moving' between branches, you are simply moving a pointer across the graph.
2. High-Availability Collaboration
When working in a distributed team of 50+ developers, your primary risk is Merge Hell. We mitigate this by enforcing a "Small, Atomic Commits" policy. If a feature takes 5 days to build, it should be broken into 10 smaller PRs. This reduces the blast radius of any single conflict and allows for continuous integration.
3. Production Incident Prevention
If a bad commit reaches production at 3:00 AM, the speed of your recovery is determined by your mastery of Git history. Knowing the difference between git revert (safe for public history) and git reset (dangerous for public history) is mandatory. In this lesson, we prioritize the patterns that ensure your system remains stable while you debug.
4. Verbal Interview Script
Interviewer: "How do you handle a massive merge conflict in a critical service?"
You: "I don't just dive into the code. First, I identify the 'Base' commit where the divergence started using git merge-base. I then communicate with the authors of the conflicting changes to understand the intent. Instead of one massive resolution, I often use git rebase -i to clean up my own history first, reducing the complexity of the final merge. I treat the conflict as an integration problem, not a syntax problem."
5. Summary Checklist for Teams
- Are commits atomic? (One change per commit)
- Is the commit message descriptive? (What and Why)
- Have you synchronized with the remote branch (
git fetch) before starting work? - Is there a CI/CD check for every Pull Request?