How to merge branches in Git

· Category: Git

Short answer

Switch to the target branch and run git merge <source-branch> to integrate changes.

Steps

  1. Switch to the target branch:
git checkout main
  1. Merge the feature branch:
git merge feature-login
  1. If conflicts occur, resolve them, then:
git add .
git commit

Tips

  • Fast-forward merges keep history linear if no diverging commits exist.
  • Use --no-ff to force a merge commit for better history visualization.
  • Always review the branch diff before merging.

Common issues

  • Merge conflicts happen when the same lines are modified in both branches.
  • Abort a problematic merge with git merge --abort.
  • Never merge broken or untested code into main.