How it works

The merge integrates the history of one branch into another. Git finds the common ancestor and applies changes from both sides. If the changes do not conflict, the merge is automatic. If they conflict, the user resolves manually.

There are types: fast-forward (no merge commit, when the target branch has not diverged), recursive (creates a merge commit), and squash (compresses all commits from the branch into one). The choice depends on the team's workflow.

Best practices include: updating the branch with main before merging, reviewing conflicts carefully and writing clear commit messages.

Practical example

Terminal
git checkout main
git merge feature/login

In case of conflict, Git marks the lines with <<<<<<<, ======= and >>>>>>>. After resolving:

Terminal
git add .
git commit -m "Merge feature/login"

For squash:

Terminal
git merge --squash feature/login
git commit -m "Adiciona login"

What it is for

Integrate parallel work. Teams use merge to join features developed in separate branches. In AI workflows, agents can create branches and the merge consolidates the result.

The merge preserves the complete history, making audit and rollback easier when needed.