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:
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:
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:
develophas not moved sincefeature-abranched 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:
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:
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:
Edit the file so it contains the final intended content, then mark it resolved:
Git opens the merge commit message editor, or you can provide the message directly:
Useful Merge Options
If you always want a merge commit even when a fast-forward is possible:
If you want to inspect the result before creating a merge commit:
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:
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:
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-branchto 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 addandgit commit. - Commit or stash local changes before merging to keep the process clean.

