In the previous articles, you learned how to:
Create branches
Switch between branches
Delete branches
Work independently on different features
But branches are meant to be temporary workspaces.
Once a feature is complete and tested, you usually want to bring those changes back into the main project.
This process is called merging.
Git provides the git merge command to combine the history of two branches into one.
Whether you're working alone or collaborating with a team, merging is one of the most common Git operations you'll perform.
In this article, you will learn:
What merging is
Why merging is needed
How
git mergeworksFast-forward merge
Three-way merge
Merge commits
Viewing merge history
Common mistakes
Best practices
By the end of this article, you'll understand how Git safely combines work from different branches.
What is Git Merge?
Git Merge is the process of combining the changes from one branch into another.
For example:
main
feature-login
After completing work on feature-login, you can merge it into main.
The result is that the main branch now contains the new feature.
Why Do We Need Merge?
Imagine you are building an Online Shopping Website.
The project currently has a stable version.
Three developers are working simultaneously:
Developer A → Login System
Developer B → Shopping Cart
Developer C → Payment Gateway
Each developer works on a separate branch.
Eventually, all completed work needs to become part of the main application.
Git Merge makes this possible.
Visual Representation
Initially:
main
↓
Commit A
↓
Commit B
Create a branch:
feature-login
Repository:
Commit A
↓
Commit B
│
└────────► feature-login
Making Changes in the Feature Branch
Switch to:
feature-login
Add two commits.
Commit A
↓
Commit B
↓
Commit C
↓
Commit D
Meanwhile:
main
still points to:
Commit B
The new work exists only in the feature branch.
Merging the Feature
First, switch to the branch that should receive the changes.
Usually:
git switch main
Now merge:
git merge feature-login
Git combines the changes from feature-login into main.
Important Rule
Always remember:
You merge into the branch you are currently on.
Example:
git switch main
git merge feature-login
Meaning:
"Take the changes from
feature-loginand add them tomain."
Before and After Merge
Before:
main
↓
Commit B
feature-login
↓
Commit D
After:
main
↓
Commit D
feature-login
↓
Commit D
Both branches now include the completed work.
Fast-Forward Merge
The simplest type of merge is called a Fast-Forward Merge.
Suppose:
Commit A
↓
Commit B
Create:
feature-login
Add:
Commit C
↓
Commit D
Meanwhile, main has not changed.
Git simply moves the main pointer forward.
Before:
main
↓
B
feature-login
↓
D
After:
main
↓
D
No extra merge commit is created because the history is completely linear.
Why Is It Called "Fast-Forward"?
Git does not need to combine two different histories.
It simply moves the branch pointer forward.
Think of it as moving a bookmark to the latest page in a book.
Three-Way Merge
Now consider a different situation.
Suppose both branches have changed.
Initial history:
Commit A
↓
Commit B
After branching:
Main branch:
Commit E
Feature branch:
Commit C
↓
Commit D
The histories have diverged.
Now Git must combine both sets of changes.
Visual Representation
C
│
D
/
A──B
\
E
Git creates a new commit:
C
│
D
/ \
A──B M
\ /
E
Where:
M
is called a Merge Commit.
What is a Merge Commit?
A merge commit is a special commit that has two parent commits.
Unlike a normal commit, it records the combination of two development histories.
This preserves information about how the branches were joined.
Viewing Merge History
Run:
git log --oneline --graph
Example output:
* Merge branch 'feature-login'
|\
| * Add login page
| * Create login form
* | Update README
|/
* Initial Commit
The graph clearly shows where the branches split and merged.
Automatic Merge
Most merges happen automatically.
Git compares:
Added lines
Deleted lines
Modified lines
If the changes do not overlap, Git combines them without asking for user intervention.
When Does Git Need Help?
Suppose:
main
changes:
System.out.println("Welcome");
Feature branch changes the same line to:
System.out.println("Welcome User");
At the same time,
main changes it to:
System.out.println("Welcome Student");
Now Git cannot determine which version should be kept.
This situation is called a merge conflict.
We will study merge conflicts in detail in the next article.
Real-World Example
Suppose your team develops a Hospital Management System.
Branches:
feature-patient
feature-billing
feature-appointments
Each developer completes and tests their feature independently.
Once approved, the team switches to main and merges each feature branch.
The application gradually grows while keeping the production code stable.
Merging Multiple Branches
Suppose you have completed:
feature-login
feature-dashboard
feature-payment
Each branch can be merged separately:
git switch main
git merge feature-login
git merge feature-dashboard
git merge feature-payment
After each successful merge, the main branch gains the new functionality.
Should You Delete the Branch After Merging?
Usually, yes.
Once a feature has been merged successfully and is no longer needed:
git branch -d feature-login
This keeps your repository clean.
Remember:
Deleting the branch does not remove the merged commits, because those commits are now reachable from main.
Common Beginner Mistakes
Mistake 1
Trying to merge while on the wrong branch.
Always verify your current branch with:
git branch
The branch marked with * is the one that will receive the merge.
Mistake 2
Deleting a branch before confirming the merge was successful.
Check:
git log --oneline --graph
or test your application before deleting the feature branch.
Mistake 3
Assuming every merge creates a merge commit.
Fast-forward merges often do not create one.
Mistake 4
Ignoring merge conflicts.
Conflicts must be resolved before the merge can be completed.
Best Practices
Commit your work before starting a merge.
Switch to the correct destination branch before merging.
Test the project after every merge.
Delete merged feature branches when they are no longer needed.
Use descriptive feature branch names.
Review the commit history after important merges.
Useful Merge Commands
| Command | Purpose |
|---|---|
git merge feature-login | Merge a branch into the current branch |
git log --graph | View branch and merge history |
git log --oneline --graph | Compact graphical history |
git branch -d feature-login | Delete a merged branch |
Summary
Git Merge is the mechanism that combines work from different branches into a single development history. Whether Git performs a simple fast-forward merge or creates a merge commit through a three-way merge depends on how the branches have evolved.
Understanding merging is essential because nearly every collaborative Git workflow relies on it. By merging carefully, testing thoroughly, and keeping branches organized, you can integrate new features without disrupting the stability of your project.
Comments
Post a Comment