Merging is the process of combining changes from one branch into another branch.
Merge allows developers to bring completed work from feature branches into the main development line.
It is one of the most important operations in Git workflows.
When working with branches, each branch has its own history.
Example:
main
A --- B --- C
feature
A --- B --- C --- D --- E
After merging:
main
A --- B --- C ------- F
\ /
D --- E
The changes from the feature branch become part of the main branch.
Merge is useful for:
- Combining completed features
- Integrating team members' work
- Adding bug fixes
- Updating the main branch
- Maintaining project history
First, switch to the branch that will receive changes.
Example:
git switch main
Then merge another branch:
git merge branch-name
Example:
git merge feature-login
A fast-forward merge happens when the target branch has no new commits.
Example:
Before:
main
A --- B
feature
A --- B --- C --- D
After merge:
main
A --- B --- C --- D
Git simply moves the branch pointer forward.
A three-way merge happens when both branches have new commits.
Example:
main
A --- B --- C
feature
A --- B --- D
Git creates a new merge commit:
A --- B --- C --- M
\ /
D
The merge commit combines both histories.
Example:
Create a feature branch:
git switch -c feature-login
Make changes and commit:
git add .
git commit -m "Add login feature"
Return to main:
git switch main
Merge:
git merge feature-login
Show commit history:
git log --oneline
Show branch graph:
git log --graph --oneline --all
Example:
- Merge feature-login
|
| * Add login page | * Add login validation | - Update README
A merge conflict happens when Git cannot automatically combine changes.
Common reasons:
- Two branches modify the same line
- A file was deleted in one branch
- Changes are incompatible
Git may show:
<<<<<<< HEAD
Current branch changes
=======
Incoming branch changes
feature-branch
You must manually choose the correct content.
Steps:
-
Open the conflicted file.
-
Find conflict markers.
-
Edit the file and keep the correct version.
-
Add the resolved file:
git add filename
- Complete the merge:
git commit
Check current merge state:
git status
Git will show:
- Conflicted files
- Files waiting for resolution
- Merge progress
If you want to cancel a merge:
git merge --abort
This returns your repository to the state before the merge started.
After a successful merge:
git branch -d branch-name
Example:
git branch -d feature-login
- Pull the latest changes before merging.
- Keep branches small and focused.
- Resolve conflicts carefully.
- Write meaningful merge commit messages.
- Test the project after merging.
- Do not merge broken code into main.
git merge
Combine branches.
git merge --abort
Cancel a merge.
git log --graph
View branch history.
git status
Check merge status.
git branch -d
Delete merged branches.
Continue to:
Rebasing Guide