Git Merge is a core Git operation used to combine changes from different branches into one. It is commonly used when a feature branch is complete and needs to be integrated into the main branch.
Merging integrates the commit history of one branch into another, preserving development context and collaboration history.
// Switch to the main branch before merging
git checkout main
// Merge a feature branch into main
git merge feature-xyz
// Git conflict markers inside a file
<<<<<<< HEAD
Changes from current branch
=======
Changes from feature branch
>>>>>>> feature-xyz
After resolving conflicts and committing, Git records a merge commit with two parent commits, preserving both histories.
Think of Git merge like combining two timelines into one, ensuring every important change is included without losing history.
// Use the 'ours' merge strategy
git merge -s ours feature-xyz
// Force a merge commit even when fast-forward is possible
git merge --no-ff feature-xyz
--no-ff to preserve historygit log