Developer Tools

How do I use a Git merge?

Updated 2026-08-14

Quick answer

To use Git merge, first ensure you are on the branch you want to merge into, then execute 'git merge <branch-name>' to combine changes from the specified branch.

Git merge is a command used to integrate changes from one branch into another, allowing for collaboration and feature integration.

Steps

  1. 1

    Checkout the Target Branch

    Use 'git checkout <target-branch>' to switch to the branch you want to merge changes into.

  2. 2

    Merge the Source Branch

    Run 'git merge <source-branch>' to merge changes from the specified source branch into your current branch.

  3. 3

    Resolve Any Conflicts

    If conflicts arise, manually edit the files to resolve them, then stage the changes with 'git add <file>' and complete the merge with 'git commit'.

  4. 4

    Verify the Merge

    Use 'git log' or 'git status' to confirm that the merge was successful and that your branch is up to date.

Understanding Git Merge

Git merge allows you to combine the changes from one branch into another. This is commonly used in collaborative workflows where multiple branches are developed in parallel.

Merge Strategies

Git supports different merge strategies such as 'fast-forward' and 'three-way merge'. The default strategy is 'three-way merge', which creates a new commit if there are changes on both branches.

Resolving Merge Conflicts

If there are conflicting changes, Git will prompt you to resolve them manually. After resolving, you must stage the changes and complete the merge with 'git commit'.

Watch out for

  • Merging can lead to complex conflicts if multiple branches have diverged significantly.
  • Always ensure your working directory is clean before merging to avoid complications.

FAQ

What happens if I merge without committing?

You will receive an error message indicating that there are uncommitted changes. You must commit or stash changes before merging.

Can I undo a merge?

Yes, you can use 'git merge --abort' to revert to the state before the merge if conflicts occur, or 'git reset --hard HEAD' to discard all changes.

How do I merge multiple branches at once?

You cannot merge multiple branches in a single command. You must merge one branch at a time into your target branch.