How to Merge Branches in Git

Updated on 03 September, 2025
Learn Git branch merging using fast-forward and three-way strategies, resolve conflicts, and manage collaborative development effectively.
How to Merge Branches in Git header image

In Git, merging is the process of combining changes from one branch to another branch. You typically use it when you have completed work on one branch and want to integrate those changes into a different branch. Git compares the commit histories of both branches and determines how to bring them together. If the branches haven't diverged, Git performs a fast forward merge by moving the pointers forward. If both branches have new commits, it uses a three-way-merge to create a new commit that unifies them. Merging helps you to keep a clean, traceable history while supporting parallel development.

Git offers multiple merge strategies depending on how branches evolve, the most common being fast-forward and three-way-merges, which you'll explore next.

  • Fast-Forward merging - Git performs a fast-forward merge when the branch you're merging into has no new commits because the other branch was created. Git doesn't need to create a new merge commit. This results in a straight history. Fast-forward merges are clean, but they don't preserve because a separate branch exists.

  • Three-Way merging - A Three-way merge happens when both branches have progressed independently. Git identifies the common ancestor of the two branches and compares it with the latest commits on each branch. It then creates a new merge commit that brings both changes together. This strategy is useful for preserving context and showing that two lines of development were combined. It's important in collaborative environments where multiple contributors work in parallel.

The Short Answer Version

If you already understand the basics of Git merging and just need the steps, this section is for you. These commands walk you through a standard merge in a direct way. Follow them to integrate one branch into another.

# Switch to the branch you want to merge into
$ git checkout target-branch

# Pull the latest changes from the remote
$ git pull origin target-branch

# Merge the source branch into the current branch
$ git merge source-branch

# Commit the merge if it's not auto-committed
$ git commit -m "Merge changes from source-branch"

# Push the merged branch to the remote repository
$ git push origin target-branch

Merge Branches In Git

Merging branches is a core part of collaborative development in Git. You often start with a new branch, develop your changes in isolation, and then integrate changes back into another branch after they are tested and complete. Follow below steps to merge branches.

  1. Create a new branch.

    console
    $ git checkout -b feature-branch
    

    This command creates a new branch called feature-branch and switches to it. You’ll use this branch to implement and test your changes.

  2. Pull the latest changes.

    console
    $ git checkout target-branch
    $ git pull origin target-branch
    

    You should always pull the latest changes before merging. This gives you a clean starting point and reduces the chances of unexpected conflicts.

  3. Merge the branch.

    console
    $ git merge feature-branch
    

    Depending on the commit history, Git will perform either a fast-forward merge or a three-way merge. If there are no conflicting changes, Git merges automatically. If there is a conflict, Git pauses the merge and prompts you to resolve it manually.

    • Commit the merged code. If the merge results in conflicts or requires additional changes, Git won't commit automatically. After you have resolved everything, create a clear and descriptive commit.

      console
      $ git commit -m “merge feature-branch into target-branch”
      
  4. Push the merged branch.

    console
    $ git push origin target-branch
    

    This completes the merge, and changes are live in the target-branch.

Resolve Merge Conflicts

Merge conflicts happen when Git can’t automatically reconcile differences between two branches. This typically occurs when changes overlap in the same part of a file. But, you don’t need to worry as Git marks conflict areas so you can resolve them manually.

  1. Identify the conflict files.

    console
    $ git status
    

    Git highlights which files conflict. Look for the files marked as both modified. You need to resolve these manually.

  2. Open the conflict files in your code editor. Git uses special markers <<<<<<<, =======, >>>>>>> to show you both versions of code.

  3. Resolve the conflicts by deciding which changes to keep, remove all conflict markers and edit the file so that it reflects the final version you want.

  4. Commit and push the changes

    console
    $ git add .
    $ git commit -m "Resolve merge conflicts"
    $ git push origin target-branch
    

    You have resolved the conflicts, committed the result, and pushed your final merged code.

Conclusion

You’ve learned how to merge branches in Git using fast-forward and three-way strategies, handle merge conflicts, and finalize the merge with proper testing and pushes. These are essential skills for any developer working in a collaborative environment. If you are new to Git branching or want a refresher, check out Vultr's guide on How to switch branches in Git to strengthen your workflow.

Tags:

Comments

No comments yet.