Merging Branches
git merge, fast-forward merge, three-way merge, merge commit, --no-ff flag
Merging Branches
Merging integrates changes from one branch into another. It is the standard way to bring a completed feature back into the main branch. Git performs merges automatically when possible, and asks for your help when it cannot.
Fast-Forward Merge
If the target branch has not diverged from the source branch โ meaning no new commits were made on the target after branching โ Git performs a fast-forward merge. It simply moves the target branch pointer forward to the source branch tip. No merge commit is created.
git switch main
git merge feature/user-loginThree-Way Merge
When both branches have new commits since they diverged, Git uses a three-way merge. It finds the common ancestor commit, compares both branches against it, and creates a new merge commit that combines the changes. This merge commit has two parents.
Forcing a Merge Commit
Even when a fast-forward is possible, teams often prefer to always create a merge commit to preserve branch history:
git merge --no-ff feature/user-login -m "Merge feature/user-login into main"The --no-ff flag ensures a merge commit is always created. This makes the branch topology visible in git log --graph.
Viewing the Merge
After merging, run git log --oneline --graph to see the branching and merging structure visually in your terminal.
