Git merge integrates the changes from one branch into another by combining their histories, typically producing a new merge commit that has two parent commits.
Key Points: • A fast-forward merge simply moves the branch pointer forward when there's no divergent history, without creating a merge commit. • A three-way merge creates a new commit combining changes when both branches have diverged, preserving the full history of both. • Conflicts occur when the same lines were changed differently on each branch and must be resolved manually. • Merging is generally safe for shared branches because it never rewrites existing commit history. • The merged branch's commits remain visible in the log, which some teams prefer for traceability over rebase's linear history.
Example: Running git merge feature-branch while on main brings all the commits from feature-branch into main, creating a merge commit if the two branches have diverged.
Code Example:
git checkout main
git merge feature-branchInterview Tip: A concise interview answer is:
"Git merge combines changes from one branch into another, creating a merge commit when histories have diverged, or simply fast-forwarding when they haven't. Unlike rebase, it never rewrites existing commits, which makes it safe to use on shared branches."