How to Merge a Branch in Git (with git merge)
Short answer
git merge integrates the history of one branch into another by finding the common ancestor of both branches and combining the divergent changes into the current branch. When the target branch has not diverged (all its commits are ancestors of the source), Git performs a fast-forward merge — simply advancing the branch pointer, resulting in a perfectly linear history. When histories have diverged, Git creates a three-way merge commit with two parents, preserving the full development timeline of both branches. If the same lines of a file were modified differently in both branches, Git pauses the merge and marks the conflicts for you to resolve manually; after editing and staging the resolved files, run git commit to complete the merge. Use git merge --no-ff to always produce a merge commit even when a fast-forward is possible, which keeps a clear record in the log of when each feature branch was integrated.
Separating different topics into different branches is a crucial practice for any serious developer. By not mixing up code from one feature / bugfix / experiment with another, you avoid a lot of problems - and don't have to worry about breaking things in your development branches.
But at some point your code will (hopefully) reach a state where you'll want to integrate it with the rest of the project. This is where the "git merge" command comes in.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Free. We email you a ZIP with our cheat sheets.
A Simple Example
Let's assume a very simple example scenario:

Our goal is to integrate the changes from "contact-form" back into "master".
Preparing to Merge
Before merging your code into one of your project's long-running branches (like a "development" or "master" branch), make sure that your local repository is up to date. Both your local feature / bugfix / <topic> branch and the receiving branch should be updated with the latest changes from your remote server.
Start with a "git fetch", followed by a "git branch -va":
git fetch
...
git branch -va
master 87eab46 [behind 1] Fix #332
* contact-form b320ab3 Ensure safe login
The [behind 1] remark tells us that "master" has received new changes on the remote. We must update "master" before we can integrate our own changes.
If properly configured, a plain "git pull" should suffice (after making "master" our active branch):
git checkout master
$ git pull
The last thing to check before actually starting the merge process is our current HEAD branch: we need to make sure that we've checked out the branch that should receive the changes.
But since we just updated "master" and already performed a "git checkout master", we're good to go!
Starting the Merge
With such perfect preparation, the actual merge process itself is easy as pie:
git merge contact-form
Looking at our project's commit history, we'll notice that a new commit was created: a so-called "merge commit" that represents the actual "melting knot" that combines the two branches.

Our integration was successful and, if our feature work on "contact-form" is finished, we could safely delete that branch.
No Command Line Needed
Merging in Tower
In case you are using the Tower Git client, merging is as easy as drag and drop — and if you keep the ALT key pressed, you can instead perform a "rebase":
Merge Options
--no-ff
Creates a merge commit even when a fast-forward would be possible. Useful to preserve a clear record that a feature branch was integrated.
git merge --no-ff feature/login
--squash
Combines all commits from the merged branch into a single commit instead of preserving them individually. Stages the result for you to commit manually — useful when you want a clean, single entry in the history.
git merge --squash feature/login
git commit -m "Add login feature"
--abort
Aborts an in-progress merge and restores the project to its pre-merge state. Use this when you've started a merge but want to back out before resolving conflicts.
git merge --abort
Dealing with Conflicts
Git will do what it can to make merging as easy as in our example. And in many cases, a merge will indeed be a walk in the park.
In some cases, however, the integration will not go as smoothly: if the branches contain incompatible changes, you will have to face (and solve) a "merge conflict". If you want to learn more about how to handle such a situation, have a look at Dealing with Merge Conflicts – Command Line edition in our free online book.
If you want to back out of a merge that has already started, use git merge --abort to restore the project to its pre-merge state.
No Command Line Needed
Resolving Conflicts in Tower
In case you are using the Tower Git client, Tower's unique "Conflict Wizard" helps you solve merge conflicts in an easy, visual way:

Learn More
- Check out the chapter Merging Changes – Desktop GUI edition in our free online book