ConfigureIntermediateTutorials

How to Merge a Branch in Git and Resolve Conflicts

Learn how to merge one branch into another in Git with git merge, understand fast-forward and three-way merges, resolve merge conflicts, and clean up afterward.

Emanuel De Almeida July 13, 2026 13 min read
Difficulty
Intermediate
Time
About 10 minutes
Steps
7

Merging in Git takes the commits from one branch and brings them into another. The direction matters: you stand on the branch you want to merge into, usually main, and merge the other branch in. So the pattern is switch to main, then run git merge with the feature branch's name.

Git handles two situations. If main hasn't changed since you branched, Git does a fast-forward: it simply moves the main pointer up to your branch, with no extra commit. If both branches have new commits, Git does a three-way merge, combining the changes and creating a merge commit with two parents. Most of the time you don't choose; Git picks the right one.

Sometimes both branches changed the same lines, and Git can't decide. That's a merge conflict. It's normal, not a failure. Git pauses and marks the spots so you can pick what's correct, then you finish the merge. This guide walks through a clean merge, conflict resolution, verifying the result, pushing, and cleaning up the merged branch. It also shows how to back out with git merge --abort if you change your mind.

Before you start

What you will learn

  • How to merge one branch into another with git merge, how fast-forward and three-way merges differ, how to resolve conflicts, and how to undo a merge if needed.
  • Merging is how the work you did on a branch gets folded back into the main line. Doing it cleanly, and handling conflicts calmly, keeps your project history healthy.

Requirements

  • A local Git repository with at least two branches, one of which has commits you want to merge in.
  • Git installed on your machine. git switch and git restore need Git 2.23 or newer; the git checkout equivalents work on older versions.

Good to know

  • A clean merge takes seconds. Resolving conflicts is what takes time, and depends on how much the branches overlap.
  • Commands use the standard Git CLI. git switch needs Git 2.23 or newer; the git checkout equivalents are shown alongside.
  • This tutorial changes the branch you merge into. It's recoverable: use git merge --abort during a conflict, and you can undo a completed merge before sharing it.

Quick answer

To merge a branch, first switch to the branch you want to merge into, usually main, with git switch main. Then run git merge followed by the other branch name, for example git merge feature-login. Git either fast-forwards or creates a merge commit. If there's a conflict, edit the marked files, run git add on them, then git commit to finish.

Code
git switch main   →   git merge <branch-name>

Step-by-step tutorial

7 steps
1

Switch to the branch you're merging into

Stand on the branch that will receive the changes.

Merging brings another branch's commits into the branch you're currently on. So first switch to the receiving branch, usually main, with git switch main. On Git older than 2.23, use git checkout main.

Getting the direction right matters: you merge the feature branch into main, not the other way around, so you need to be on main before you merge.

Bash
git switch main
# On older Git:
# git checkout main
Expected resultgit status shows you're on main (or whichever branch you want to merge into).

If you merge while on the wrong branch, the changes land in the wrong place. Check git status first.

2

Update the receiving branch and check it's clean

Make sure main is current and you have no uncommitted changes.

If you're working with a remote, update the receiving branch with git pull so you merge into the latest version. Then run git status to confirm your working tree is clean.

Uncommitted changes can block a merge or get tangled up in it. Commit them, or set them aside with git stash, before you continue.

Bash
git pull
git status
Expected resultmain is up to date with the remote, and git status reports a clean working tree with nothing to commit.

Skip git pull if this is a purely local repository with no remote.

3

Run the merge

Bring the other branch's commits into the current branch.

Run git merge followed by the branch you want to bring in, for example git merge feature-login. Git figures out the merge type for you.

If main hasn't moved since you branched, Git does a fast-forward and just advances the pointer, with no new commit. If both branches have new commits, Git does a three-way merge and creates a merge commit; it may open an editor for the merge message, where you can save and close to accept the default.

Bash
git merge feature-login
# Force a merge commit even if fast-forward is possible:
# git merge --no-ff feature-login
Expected resultGit reports the merge, either 'Fast-forward' or a summary of the merge commit and changed files.

Use --no-ff when you want the branch to show as a distinct merge in history, even on a fast-forward.

4

Resolve conflicts if Git reports them

Fix files Git couldn't merge automatically, then finish the merge.

If both branches changed the same lines, Git stops and reports a conflict, and git status lists the files under 'Unmerged paths'. Open each one. Git marks the clashing sections between markers: the current branch's version, a divider, and the incoming version.

Edit the file to keep what's correct, and remove the conflict markers. Then stage the resolved files with git add, and finish with git commit (or git merge --continue). To abandon the merge entirely and return to before you started, run git merge --abort.

Bash
git status
# edit the conflicted files, then:
git add <resolved-file>
git commit
# Or bail out:
# git merge --abort
Expected resultAfter staging and committing, git status is clean and the merge is complete with the conflicts resolved.

The conflict markers look like <<<<<<<, =======, and >>>>>>>. Delete all three once you've chosen the correct content.

5

Verify the merge

Confirm the history and files look right.

Check the result with git log --oneline --graph, which shows the branches joining and any merge commit. Run git status to confirm nothing is left unresolved.

It's worth a quick look at the merged files or a build or test run, to make sure the combined code actually works, not just that Git accepted the merge.

Bash
git log --oneline --graph
git status
Expected resultThe graph shows the feature branch merged in, and git status reports a clean tree.

A green build or passing tests after the merge is the real confirmation that the combination is sound.

6

Push the updated branch

Share the merged result with your team.

Send the updated branch to the remote with git push. Since main usually already tracks its remote, a plain git push is enough.

Once pushed, everyone pulling main gets the merged work. This is the point where the merge becomes shared, so make sure you're happy with it first.

Bash
git push
Expected resultThe remote branch now includes the merged commits, and teammates get them on their next pull.

If you undid a merge locally, be careful pushing: rewriting shared history affects everyone.

7

Clean up the merged branch (optional)

Remove the branch once its work is safely merged.

When the feature branch is fully merged and no longer needed, delete it locally with git branch -d feature-login. The lowercase -d is safe: Git refuses if the branch isn't merged.

To remove it from the remote too, run git push origin --delete feature-login. Keeping merged branches around isn't harmful, but tidying up keeps your branch list readable.

Bash
git branch -d feature-login
# Remove it from the remote as well:
# git push origin --delete feature-login
Expected resultThe merged branch is gone locally, and optionally on the remote, while its commits live on in main.

Use -D (capital) only if you deliberately want to delete an unmerged branch and discard its work.

Confirm the merge went as intended

A successful merge means the other branch's commits are now part of the branch you merged into, and your working tree is clean. The clearest confirmation is git log --oneline --graph, which shows the two lines of work joining, plus a merge commit if it was a three-way merge. git status should report nothing left to resolve.

Beyond Git's own view, check that the combined result actually works. Open the key files, or run your build and tests. A merge that Git accepted can still produce code that doesn't compile if the two sides changed related logic. After you push, the merged commits appear on the remote for everyone.

  • git log shows the branch merged in (with a merge commit for a three-way merge), git status is clean, and after pushing the commits appear on the remote.
  • git status still shows unmerged paths, meaning conflicts aren't resolved yet, or the build fails after the merge, meaning the combined code needs fixing.
  • Git reports 'Fast-forward' and simply moves main up to the feature branch, with no merge commit.
  • Git creates a merge commit with two parents; git log --graph shows the branches joining.

Troubleshooting

CONFLICT (content): Merge conflict in a file

Cause: Both branches changed the same lines, so Git can't merge them automatically.

Open the files listed by git status under Unmerged paths. Edit each conflicted section, keeping the correct content and removing the <<<<<<<, =======, and >>>>>>> markers. Then run git add on the resolved files and git commit (or git merge --continue) to finish. To start over, use git merge --abort.

Your local changes would be overwritten by merge

Cause: You have uncommitted changes that the merge would touch.

Commit your changes, or set them aside with git stash, then run the merge again. After the merge you can restore stashed work with git stash pop and resolve any overlap.

Git says 'Already up to date'

Cause: The branch you're merging is already contained in the current branch, so there's nothing to bring in.

Confirm you're on the right receiving branch with git status, and that the source branch actually has new commits with git log <branch> --oneline. If you switched the branches by mistake, merge the other direction.

You merged into the wrong branch or want to undo the merge

Cause: The merge went to the wrong place, or you changed your mind.

If you're still resolving a conflict, run git merge --abort to return to before the merge. If the merge already committed but hasn't been pushed, you can undo it locally with git reset --hard to the commit before the merge. For a merge that's already shared, use git revert with -m 1 instead of rewriting history.

Frequently asked questions

How do I merge a branch in Git?

Switch to the branch you want to merge into, usually main, then run git merge followed by the other branch name. For example: git switch main, then git merge feature-login.

Which branch do I merge into which?

You stand on the receiving branch and merge the other one in. To fold feature-login into main, switch to main first, then run git merge feature-login. The branch you're on is the one that changes.

What's the difference between a fast-forward and a three-way merge?

A fast-forward happens when the receiving branch hasn't changed since you branched, so Git just moves its pointer forward with no merge commit. A three-way merge happens when both branches have new commits; Git combines them and creates a merge commit with two parents.

How do I resolve a merge conflict?

Open the files Git marks as conflicted, edit the sections between the conflict markers to keep the correct content, and remove the markers. Then run git add on the resolved files and git commit, or git merge --continue, to finish.

How do I cancel or undo a merge?

During a conflict, git merge --abort returns you to before the merge. If the merge already committed but isn't pushed, git reset --hard to the previous commit undoes it locally. For a shared merge, use git revert with -m 1 instead.

What does git merge --no-ff do?

It forces Git to create a merge commit even when a fast-forward would be possible. This keeps the branch visible as a distinct line in the history, which some teams prefer for traceability.

Read next

Reader reviews

Rate this articleBe the first to rate
No written reviews yetRate the article above, or be the first to share your experience.

Related articles