Merge
The process of combining changes from one branch into another, integrating parallel lines of development into a unified codebase.
Detailed Explanation
Merging is how Git combines branches. When you merge a branch, Git creates a new commit that incorporates changes from both branches. There are several merge strategies: fast-forward (moves the pointer forward when there are no conflicts), recursive (creates a merge commit combining both branches), and squash (combines all changes into a single commit).
Merge conflicts occur when both branches modify the same lines. Git cannot automatically resolve these and requires manual intervention. Modern IDEs provide visual merge conflict resolution tools that make this process much easier.
Why It Matters
Merging is how parallel development comes together. Understanding merge strategies and conflict resolution is essential for team-based development.
Real-World Example
After code review approval, a developer merges their feature branch into main. Git automatically combines the changes, and if there are no conflicts, creates a merge commit. If conflicts exist, the developer resolves them before completing the merge.
When to Use
When a branch is ready to be integrated into the target branch. Typically after code review, testing, and approval through a pull request.
Advantages
- Combines parallel development work
- Preserves complete history of both branches
- Automatic merge for non-conflicting changes
- Multiple merge strategies for different needs
- Enables collaborative development workflows
Disadvantages
- Merge conflicts require manual resolution
- Merge commits can clutter history
- Large merges are hard to review
- Reverting a merge commit is more complex
- Nested merge conflicts can be confusing
Related Terms
Frequently Asked Questions
What is a merge conflict?
A merge conflict occurs when both branches have changed the same lines in a file. Git cannot determine which change to keep, so it marks the conflict for manual resolution. You must choose or combine the changes.
What is the difference between merge and rebase?
Merge preserves the complete branch history with a merge commit. Rebase replays commits on top of the target branch, creating a linear history. Merge is safer for shared branches; rebase is cleaner for local history.
How do I resolve a merge conflict?
Open the conflicted file, look for conflict markers (<<<<<<, ======, >>>>>>), choose which changes to keep, remove the markers, and commit the resolution. VS Code and other editors provide visual merge tools.
What is a fast-forward merge?
A fast-forward merge simply moves the branch pointer forward when the target branch has no new commits since the feature branch was created. It creates no merge commit and keeps history linear.
Should I always merge to main?
Follow your team's workflow. Common approaches: merge pull requests to main (GitHub Flow), merge to develop then release (GitFlow), or rebase and fast-forward (trunk-based). Choose one and be consistent.