Skip to main content
Branching & Merging

git merge

Combine changes from one branch into your current branch

Practical Example

git checkout main
git merge feature-login
# If conflicts: resolve them, then:
git add .
git commit

Command Overview & How It Works

Takes the commits from a source branch and integrates them into your current branch. When the target branch has not diverged, Git performs a fast-forward merge — it simply moves the branch pointer forward. When branches have diverged, Git creates a merge commit that has two parents, preserving the full history of both branches. If conflicts occur (changes overlap in the same lines), Git pauses with the conflicting files marked. Use git merge --abort to cancel a conflicted merge and return to the pre-merge state. Use git merge --no-ff to force a merge commit even when fast-forward is possible — useful for preserving feature branch context. Use git merge --squash to combine all source branch commits into a single commit on the target branch. Always merge on the receiving branch (e.g., checkout main, then merge feature-login).

Practical Tips

  • Always merge on the receiving branch: check out main first, then merge the feature branch into it.
  • Use --no-ff to preserve the feature branch in history, or --squash to fold it into a single commit.
  • If a merge conflicts and you get stuck, git merge --abort returns you to the exact pre-merge state.

Related Commands in Branching & Merging