Skip to main content

What is git merge?

git merge is a command that combines changes from one branch into another, keeping the history of both branches.

In simple terms: "Take everything that was done in the other branch, and add it to the current one."


Why git merge is needed

In Git you usually work with branches:

  • main / master, the main branch
  • feature/*, branches for new features
  • bugfix/*, branches for fixes

You work in a separate branch, and when everything is ready, you merge it back into the main one. That is exactly what git merge is for.


How git merge works

Example:

  1. You are on the main branch
  2. You want to bring in changes from the feature-login branch
bash
git checkout main git merge feature-login

Result:

  • All commits from feature-login appear in main
  • The branch history is preserved

What happens "under the hood"

Git:

  1. Finds the common ancestor commit of the two branches
  2. Compares the changes
  3. Combines them
  4. Creates a merge commit (if needed)

Types of merge

1. Fast-forward merge

If main had no new commits, Git simply moves the pointer:

A---B---C (main) \ D---E (feature)

After the merge:

A---B---C---D---E (main)

No extra commits Clean history


2. Merge commit (a regular merge)

If both branches evolved in parallel:

A---B---C---F (main) \ D---E (feature)

Git creates a separate merge commit:

A---B---C---F---M (main) \ / D---E---

You can see that there was a separate branch The history is maximally honest


Conflicts during a merge

If the changes touch the same lines, Git does not know which to pick.

Then:

  • Git reports a conflict
  • You manually pick the correct option
  • After that you run git commit

Conflicts are normal, this is emphasized at interviews.


Advantages of git merge

Simple to use Safe Does not rewrite history Good for team work


Disadvantages

The history can become "noisy" (many merge commits)


Short version for an interview

git merge is a command for combining branches that preserves history and creates a merge commit when needed. It does not rewrite commits and is considered safe for shared work.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.