How to Merge Two Branches in Git Without Issues

Summarize this article with:
Two developers. Same file. Different changes. Now what?
Learning how to merge two branches in Git is the skill that separates chaotic codebases from clean version control workflows.
Every feature branch, every bugfix, every parallel development effort eventually needs to come together. The git merge command handles this integration.
This guide walks you through the complete merge process in 5 steps. You will learn how to switch branches, pull remote changes, execute the merge command, resolve conflicts, and verify the results.
Works with Git 2.x on Windows, macOS, and Linux. Takes about 2-5 minutes for a standard merge operation.
How to Merge Two Branches in Git

Merging two branches in Git is the process of combining code changes from one branch into another using the git merge command.
Developers need this when integrating feature branches, applying hotfixes, or consolidating work from multiple team members into the main branch.
This guide covers 5 steps requiring 2-5 minutes and basic familiarity with Git command line operations.
Prerequisites
Before you start, make sure you have:
- Git version 2.x or later installed on your system
- Access to a Git repository with at least two branches
- Basic knowledge of terminal or command prompt navigation
- Write permissions to the target branch
Time estimate: 2-5 minutes for a clean merge, longer if conflicts exist.
Step One: How Do You Switch to the Target Branch?
Switch to the branch that will receive the changes using git checkout or git switch. The target branch is typically main or master. Your terminal prompt updates to show the current branch name after switching.
Action
- Open terminal: Navigate to your repository’s root directory
- Run command:
git checkout mainorgit switch main - Verify: Check the branch name in your prompt or run
git branch
Purpose
Git merges changes into your current branch. If you skip this step, you might accidentally merge into the wrong branch and mess up your commit history.
Need to create a new branch first? Do that before proceeding.
Step Two: How Do You Update Your Local Branch with Remote Changes?
Pull the latest changes from the remote repository to sync your local branch. This prevents merge conflicts caused by outdated code. The command fetches and integrates remote commits into your working directory.
Action
- Run pull command:
git pull origin main - Check output: Look for “Already up to date” or list of updated files
- Resolve any conflicts: Fix issues before proceeding to the merge
Purpose
Your teammates might have pushed commits while you worked on your feature branch.
Pulling first ensures you have the complete codebase before merging. Skipping this step often creates unnecessary merge conflicts.
Step Three: How Do You Execute the Merge Command?
Run git merge [source-branch] to integrate changes from your feature branch into the target branch. Git combines the commit histories and creates a merge commit. The terminal displays which files were modified during the merge operation.
Action
- Run merge command:
git merge feature-branch(replace with your branch name) - Review output: Check for “Fast-forward” or “Merge made by” messages
- Verify files: Review the list of changed files in the output
Purpose
The merge command does the actual work of combining branch histories.
Git uses a three-way merge algorithm when branches have diverged, or a fast-forward merge when the target branch has no new commits since branching.
Understanding what git merge does helps you troubleshoot issues.
Step Four: How Do You Handle Merge Conflicts?
Merge conflicts occur when Git cannot automatically combine changes from both branches. The terminal displays “CONFLICT” messages with affected file names. You must manually edit these files, choose which changes to keep, then stage the resolved files.
Action
- Identify conflicts: Run
git statusto see files marked as “both modified” - Open conflicted files: Look for
<<<<<<<,=======, and>>>>>>>markers - Edit manually: Remove markers, keep the correct code
- Stage resolved files:
git add [filename]
Purpose
Conflicts happen when two branches modify the same lines in a file.
Git marks these sections so you can decide which version to keep. For complex conflicts, check our guide on how to resolve merge conflicts in Git.
Step Five: How Do You Complete the Merge Commit?
After resolving conflicts or completing a clean merge, finalize the operation with a merge commit. Git opens your default editor for the commit message. Save and close to complete the branch integration.
Action
- Run commit:
git commit(Git auto-generates merge message) - Edit message: Modify the default “Merge branch ‘feature’ into main” if needed
- Save and exit: Press
:wqin Vim or Ctrl+S in other editors
Purpose
The merge commit records the branch integration in your git log history.
Understanding what git commit does helps you write meaningful messages that explain why branches were merged.
Verification
Confirm the merge succeeded before pushing to the remote repository.
Commands to Verify
git log --oneline --graph: Shows merge commit and branch structuregit branch --merged: Lists branches already merged into current branchgit status: Confirms clean working directory with no pending changes
Expected Output
The log displays a merge commit with two parent commits. Your feature branch appears in the merged branches list.
Ready to clean up? Learn how to delete a branch in Git after merging.
Troubleshooting
Issue: “Already up to date” Message
Cause: Target branch already contains all commits from source branch.
Solution: Verify you switched to the correct branch. Run git log --oneline on both branches to compare commit histories.
Issue: Merge Conflicts Prevent Completion
Cause: Both branches modified identical lines in the same files.
Solution: Open each conflicted file, remove conflict markers, keep desired code, run git add . then git commit.
Issue: Accidental Merge (Need to Undo)
In-progress merge: git merge --abort cancels the operation.
Completed merge: git reset --hard HEAD~1 removes the merge commit. Use git reset carefully since it rewrites history.
Alternative Methods
Fast-Forward Merge vs. Three-Way Merge
Fast-forward: Moves branch pointer when no divergent commits exist. Clean, linear history. Command: git merge --ff-only feature-branch
Three-way merge: Creates merge commit when branches diverged. Preserves complete branch history. Command: git merge --no-ff feature-branch
Merge vs. Rebase
| Factor | Git Merge | Git Rebase |
| History Style | Non-destructive; preserves all branch context. | Linear; rewrites history for a clean “story.” |
| Commit Logs | Can become “messy” with many merge commits. | Extremely clean; no unnecessary merge noise. |
| Conflict Handling | Resolve all conflicts once at the end. | Resolve conflicts commit-by-commit as they replay. |
| Best For | Public/Shared branches and team collaboration. | Local/Private branches and solo feature work. |
| Audit Trail | Excellent; shows exactly when integration happened. | Limited; original timestamps/hashes are changed. |
Learn more about git rebase for alternative branch integration strategies.
Related Processes
- How to switch branches in Git
- How to rename a branch in Git
- How to revert a commit in Git
- How to clone a Git repository
- What is a Git branch
Working with GitHub? Check how to merge branches in GitHub for pull request workflows.
FAQ on How To Merge Two Branches In Git
What is the difference between git merge and git rebase?
Git merge preserves complete branch history by creating a merge commit with two parent commits. Git rebase rewrites history by moving commits onto the target branch tip. Use merge for shared branches, rebase for local feature branches before pushing.
Can I merge two branches without switching branches first?
No. Git requires you to checkout the target branch before merging. The merge command integrates changes into your current branch only. Always switch to the receiving branch (usually main) before running git merge.
What happens if I merge the wrong branch?
Use git merge --abort if the merge is in progress. For completed merges, run git reset --hard HEAD~1 to undo the merge commit. Check git status afterward to confirm a clean working directory.
How do I know if my merge was successful?
Run git log --oneline --graph to see the merge commit in your history. The output shows branch convergence points. Use git branch --merged to list all branches already integrated into your current branch.
What causes merge conflicts in Git?
Conflicts occur when two branches modify the same lines in a file. Git cannot automatically decide which version to keep. You must manually edit conflicted files, remove conflict markers, then stage and commit the resolved changes.
Should I delete the branch after merging?
Yes, for feature branches. Deleting merged branches keeps your repository clean and reduces clutter. Run git branch -d branch-name for local deletion. The -d flag only works on fully merged branches as a safety measure.
What is a fast-forward merge?
A fast-forward merge occurs when the target branch has no new commits since you created the feature branch. Git simply moves the branch pointer forward. No merge commit is created. Use --no-ff to force a merge commit anyway.
How do I merge a remote branch into my local branch?
First run git fetch origin to download remote branch data. Then execute git merge origin/branch-name to integrate those changes. Alternatively, git pull combines fetch and merge into one command.
Can I merge multiple branches at once?
Yes. Git supports octopus merges for combining multiple branches simultaneously. Run git merge branch1 branch2 branch3 to merge all at once. This works best when branches have no conflicts with each other.
How does merging fit into a CI/CD workflow?
Merging triggers continuous integration pipelines that run automated tests. Failed tests block the merge in most Git workflows. This ensures only validated code reaches the main branch.
Conclusion
You now know how to merge two branches in Git using a reliable 5-step process. Switch to target branch, pull updates, run the merge command, handle conflicts, complete the commit.
Branch integration becomes routine once you understand the underlying mechanics. Fast-forward merges keep history clean. Three-way merges preserve the complete commit history of collaborative work.
Merge conflicts are not failures. They are signals that your source control management system needs human judgment.
Practice on a test repository first. Break things. Fix them. That is how you build confidence with Git commands.
Your next step? Set up a proper Git Flow branching strategy for your team and integrate merges into your build pipeline.
- Agile vs Lean Software Development: Which is Better? - March 13, 2026
- Feature-Driven Development vs Agile: Key Differences - March 12, 2026
- Agile vs DevOps: How They Work Together - March 11, 2026







