Merging

Fast-Forward Merge

When the branch you're merging has commits that are directly ahead of the current branch.

git checkout main
git merge feature-branch

Three-Way Merge

When branches have diverged and Git needs to create a merge commit.

git checkout main
git merge feature-branch
# This creates a merge commit

Merge Conflicts

When the same lines in the same files have been changed in both branches.

Resolving Conflicts

  1. Git will mark the conflicted files
  2. Edit the files to resolve conflicts
  3. Stage the resolved files
  4. Commit the merge
# After conflict occurs
git status

# Edit conflicted files
# Remove conflict markers (<<<<<<<, =======, >>>>>>>)

# Stage resolved files
git add resolved-file.txt

# Commit the merge
git commit

Aborting a Merge

git merge --abort

Merge Strategies

Recursive (default for branches)

git merge -s recursive feature-branch

Ours (keep current branch changes)

git merge -s ours feature-branch

Theirs (keep other branch changes)

git merge -X theirs feature-branch

Squash Merging

Combine all commits from a branch into a single commit.

git merge --squash feature-branch
git commit -m "Squashed commit message"

Rebase vs Merge

# Rebase instead of merge
git checkout feature-branch
git rebase main
Loading