The git merge command is used to combine the changes from one branch into another branch. It is commonly used after completing a feature or bug fix on a separate branch and then bringing those changes into the main development branch.
git merge.
git merge combines the history and changes of one branch into the currently checked-out branch.
git merge branch-name
For example:
git switch main
git merge feature-login
Merge is useful when development work has been completed on a separate branch and needs to become part of another branch.
The basic syntax is:
git merge branch-name
The specified branch is merged into the branch that is currently checked out.
The current branch is the target branch of the merge.
git switch main
git merge feature-login
Here, feature-login is merged into main.
main
|
A → B
feature-login
|
C → D
If you switch to main and merge
feature-login, Git combines the histories.
git switch main
git merge feature-login
git switch main
git switch -c feature-login
Now you can develop the feature independently.
git add .
git commit -m "Add login feature"
The feature branch now contains a commit that can later be merged into another branch.
Before merging the feature branch into main, switch to main:
git switch main
Verify the current branch:
git branch --show-current
Now merge the feature branch:
git merge feature-login
Git combines the changes from feature-login into
main.
A fast-forward merge can occur when the target branch has not moved forward since the feature branch was created.
A → B → C → D
↑
feature
If main still points to C, Git can move the
main pointer forward to D without creating a merge commit.
If the branches have diverged, Git may create a merge commit.
C → D
/ \
A → B M
\ /
E → F
The merge commit combines the two development histories.
After merging, use:
git log --oneline --graph --all
This can help you visualize branches and merge commits.
Use:
git status
After a successful merge, Git may show that the working tree is clean if there are no additional changes.
git switch fix-navbar
# Fix the problem
git add .
git commit -m "Fix navbar issue"
git switch main
git merge fix-navbar
The completed bug fix is now integrated into the main branch.
A merge conflict can happen when Git cannot automatically combine changes from the branches.
This commonly happens when different branches modify the same parts of the same file.
git merge feature-login
Git may stop and ask you to resolve the conflicting files.
If a merge conflict occurs, use:
git status
Git will identify files that have unresolved conflicts.
A conflicted file can contain markers such as:
<<<<<<< HEAD
Your current branch changes
=======
Incoming branch changes
>>>>>>> feature-login
These markers help you identify the competing changes.
To resolve a conflict:
git add filename
After resolving and staging all conflicts, complete the merge:
git commit
Git may prepare an appropriate merge commit message automatically.
If you do not want to continue a merge that is currently in progress, you can abort it:
git merge --abort
This attempts to return the repository to the state it had before the merge started.
git switch main
git merge feature-login
git merge feature-dashboard
git merge feature-payment
Each branch is merged separately into the current branch.
The --no-ff option can be used to create a merge commit
even when Git could perform a fast-forward merge.
git merge --no-ff feature-login
This can preserve an explicit merge point in the project history.
The --no-commit option can stop before creating the merge
commit when a merge commit would otherwise be created.
git merge --no-commit feature-login
This gives you an opportunity to inspect the result before completing the merge commit.
After merging locally, you can push the updated target branch to a remote repository when appropriate.
git switch main
git merge feature-login
git push
The merge itself happens locally; git push sends the
resulting commits to the remote repository.
| Merge | Rebase |
|---|---|
| Combines branch histories | Replays commits onto another base |
| Can create a merge commit | Normally creates a different linear history |
| Preserves the existing branch relationships | Rewrites commit history for the rebased commits |
The appropriate workflow depends on the project's collaboration rules.
git merge feature-login
git merge --no-ff feature-login
git merge --no-commit feature-login
git merge --abort
git status
git add filename
git commit
git log --oneline --graph --all
# Create feature branch
git switch -c feature-login
# Make changes
git add .
git commit -m "Add login feature"
# Switch to main
git switch main
# Merge feature
git merge feature-login
# Check result
git status
# View history
git log --oneline --graph --all
git init
git add .
git commit -m "Initial project"
git switch -c feature-about
# Create about page
git add .
git commit -m "Add about page"
git switch main
git merge feature-about
git log --oneline --graph --all
The feature-about branch is merged into
main, bringing the About page changes into the main
development line.
The git merge command combines changes from one branch into the current branch.
git switch main
git merge feature-login
Git may perform a fast-forward merge or create a merge commit, depending on the branch history.
If conflicts occur, resolve them, stage the resolved files, and
complete the merge. If you do not want to continue, use
git merge --abort while the merge is in progress.
git switch to move to the target branch first.git merge branch-name to merge a branch.git status to identify unresolved conflicts.git add after resolving conflicted files.git merge --abort to cancel a merge in progress.git merge --no-ff can force a merge commit.git push can share the result with a remote repository.Question: Which branch receives the changes when you run
git merge feature-login?