0
0
GitHow-ToBeginner · 3 min read

How to Use Git Merge: Simple Guide to Combine Branches

Use git merge <branch-name> to combine changes from another branch into your current branch. This command integrates the histories of both branches, creating a new commit if needed.
📐

Syntax

The basic syntax of git merge is simple and includes the branch you want to merge into your current branch.

  • git merge <branch-name>: Merges the specified branch into the current branch.
  • <branch-name>: The name of the branch whose changes you want to bring into your current branch.
bash
git merge <branch-name>
💻

Example

This example shows how to merge a branch named feature into the current branch main. It demonstrates the merge process and the output you will see.

bash
git checkout main
git merge feature
Output
Updating 1a2b3c4..5d6e7f8 Fast-forward file.txt | 2 ++ 1 file changed, 2 insertions(+)
⚠️

Common Pitfalls

Common mistakes when using git merge include:

  • Trying to merge without switching to the target branch first.
  • Not resolving conflicts when they appear, which stops the merge process.
  • Using git merge on unrelated branches without understanding the impact.

Always ensure you are on the branch you want to update before merging.

bash
git merge feature
# Wrong: running this on the feature branch instead of main

# Correct way:
git checkout main
git merge feature
📊

Quick Reference

CommandDescription
git merge Merge specified branch into current branch
git merge --no-ff Create a merge commit even if fast-forward is possible
git merge --abortStop the merge and revert to the pre-merge state if conflicts occur
git statusCheck for conflicts and current branch status after merge

Key Takeaways

Always switch to the branch you want to update before running git merge.
Use git merge to combine changes from another branch.
Resolve conflicts promptly to complete the merge process.
Use git merge --abort to cancel a merge if needed.
Check your branch status with git status after merging.