Merging Branches
Merging combines changes from one branch into another. Git tries to merge automatically, but sometimes conflicts occur.
Example
# Merge feature branch into main
git switch main
git merge feature-login
# Fast-forward merge (linear history)
# Happens when main hasn't changed since branching
# Three-way merge (merge commit)
# Happens when both branches have new commits
# Creates a merge commit combining both Resolving Merge Conflicts
Conflicts happen when the same lines were changed in both branches. Git marks the conflicts in the file and you must resolve them manually.
Example
# Conflict markers in a file:
<<<<<<< HEAD
const title = "Welcome Home";
=======
const title = "Welcome Back";
>>>>>>> feature-branch
# To resolve:
# 1. Open the file and choose which version to keep
# 2. Remove the conflict markers
# 3. Stage and commit
const title = "Welcome Home"; # Your resolution
git add filename.js
git commit -m "Resolve merge conflict in filename.js" Aborting a Merge
If a merge gets too complicated, you can abort and go back to the state before the merge.
Example
# Abort the merge
git merge --abort
# This restores your branch to its pre-merge state
# No changes are lost from either branch 