git
local branch
merge
version control
development

merge one local branch into another local branch

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Merging one local Git branch into another uses the same merge mechanics as any other Git merge. The key rule is simple: check out the branch that should receive the changes, then merge the branch that contains the changes you want to bring in.

The Basic Command Sequence

Suppose you want to merge feature-a into develop. Start on the target branch:

bash
git switch develop
git merge feature-a

That tells Git to take the commits reachable from feature-a and integrate them into develop.

If you prefer the older checkout syntax, the equivalent is:

bash
git checkout develop
git merge feature-a

Both versions work. git switch is just clearer about branch movement.

What Git Actually Does

Git compares the histories of the two branches and tries to produce a merged result. There are two common outcomes:

  • Fast-forward merge: develop has not moved since feature-a branched off, so Git simply advances the branch pointer.
  • True merge commit: both branches have unique commits, so Git creates a merge commit tying the histories together.

You can see this with a small example:

bash
git log --oneline --graph --decorate --all

Running that before and after the merge helps explain what changed in the history.

Example Workflow

Create two local branches and merge one into the other:

bash
1git switch -c develop
2echo "base" > app.txt
3git add app.txt
4git commit -m "Add base file"
5
6git switch -c feature-a
7echo "feature change" >> app.txt
8git commit -am "Update app.txt in feature-a"
9
10git switch develop
11git merge feature-a

If no other changes were made on develop, this is likely a fast-forward merge. If both branches edited the same lines differently, Git stops and asks you to resolve conflicts.

Handling Merge Conflicts

If Git reports a conflict, open the affected file and look for markers:

text
1<<<<<<< HEAD
2current branch content
3=======
4incoming branch content
5>>>>>>> feature-a

Edit the file so it contains the final intended content, then mark it resolved:

bash
git add app.txt
git commit

Git opens the merge commit message editor, or you can provide the message directly:

bash
git commit -m "Merge feature-a into develop"

Useful Merge Options

If you always want a merge commit even when a fast-forward is possible:

bash
git merge --no-ff feature-a

If you want to inspect the result before creating a merge commit:

bash
git merge --no-commit --no-ff feature-a

That is helpful when you want one last review of the working tree before finalizing the merge.

Merging Local Branches Does Not Require A Remote

Because both branches are local, you do not need to push or pull just to perform the merge. Git reads the commit graph stored in your repository and merges directly.

Of course, if the branches also exist on a remote and you want collaborators to see the merged result, you would push afterward:

bash
git push origin develop

But the merge itself is a local operation.

Common Pitfalls

The biggest mistake is merging in the wrong direction. If you mean "bring feature changes into develop," you must check out develop first and then merge feature-a.

Another common issue is starting a merge with uncommitted local changes. Git may block the merge or mix unrelated edits into the conflict resolution process. Commit or stash your work first.

Developers also sometimes delete the source branch immediately without checking the merged result. Make sure the merge is correct before cleaning up:

bash
git branch -d feature-a

Finally, do not confuse merge with rebase. Both integrate changes, but they produce different history shapes and different collaboration tradeoffs.

Summary

  • Check out the target branch first.
  • Run git merge source-branch to bring in the other branch's commits.
  • Expect either a fast-forward or a merge commit, depending on history.
  • Resolve conflicts by editing files, then git add and git commit.
  • Commit or stash local changes before merging to keep the process clean.

Course illustration
Course illustration

All Rights Reserved.