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 branchfeature/*, branches for new featuresbugfix/*, 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:
- You are on the
mainbranch - You want to bring in changes from the
feature-loginbranch
git checkout main
git merge feature-loginResult:
- All commits from
feature-loginappear inmain - The branch history is preserved
What happens "under the hood"
Git:
- Finds the common ancestor commit of the two branches
- Compares the changes
- Combines them
- 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 mergeis 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 readyA concise answer to help you respond confidently on this topic during an interview.