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
- Switch to the target branch:
git checkout main
- Merge the feature branch:
git merge feature-login
- If conflicts occur, resolve them, then:
git add .
git commit
Tips
- Fast-forward merges keep history linear if no diverging commits exist.
- Use
--no-ffto 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.