0
0
Gitdevops~7 mins

Three-way merge in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When multiple people change the same file in different ways, Git uses a three-way merge to combine those changes. It compares the common starting point and the two changed versions to create a final merged file.
When two developers edit the same file on different branches and you want to combine their work.
When you pull changes from a remote repository that conflict with your local changes.
When you want to merge a feature branch back into the main branch after independent work.
When resolving conflicts after rebasing your branch onto another branch.
When Git cannot automatically merge changes and asks you to fix conflicts manually.
Commands
Create and switch to a new branch called feature-branch to work on changes separately.
Terminal
git checkout -b feature-branch
Expected OutputExpected
Switched to a new branch 'feature-branch'
Commit your changes in the feature branch with a message describing the update.
Terminal
git commit -am "Change in feature branch"
Expected OutputExpected
[feature-branch abc1234] Change in feature branch 1 file changed, 1 insertion(+), 1 deletion(-)
-a - Automatically stage tracked files before committing
-m - Add commit message inline
Switch back to the main branch to prepare for merging the feature branch.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Merge the feature branch into main. Git uses a three-way merge comparing the common ancestor and both branches.
Terminal
git merge feature-branch
Expected OutputExpected
Updating 123abcd..abc1234 Fast-forward file.txt | 2 ++ 1 file changed, 2 insertions(+)
If there are conflicts, Git will pause the merge and ask you to fix them manually.
Terminal
git merge feature-branch
Expected OutputExpected
Auto-merging file.txt CONFLICT (content): Merge conflict in file.txt Automatic merge failed; fix conflicts and then commit the result.
Check which files have conflicts that need to be resolved before completing the merge.
Terminal
git status
Expected OutputExpected
On branch main You have unmerged paths. (fix conflicts and run "git commit") Unmerged paths: (use "git add <file>..." to mark resolution) both modified: file.txt no changes added to commit (use "git add" and/or "git commit -a")
After fixing conflicts in the file, stage the resolved file to mark it as ready.
Terminal
git add file.txt
Expected OutputExpected
No output (command runs silently)
Complete the merge by committing the resolved changes with a message.
Terminal
git commit -m "Resolve merge conflict in file.txt"
Expected OutputExpected
[main abc5678] Resolve merge conflict in file.txt
-m - Add commit message inline
Key Concept

If you remember nothing else from this pattern, remember: a three-way merge uses the common ancestor and both changed versions to combine edits safely.

Common Mistakes
Ignoring merge conflicts and trying to commit without resolving them.
Git will not allow committing until conflicts are fixed, causing confusion and blocking progress.
Open conflicted files, fix the conflicts, stage the files with git add, then commit.
Merging without updating the main branch first.
You might merge outdated code, causing unnecessary conflicts or overwriting newer changes.
Always pull or fetch and update your main branch before merging feature branches.
Using git merge without understanding the changes in both branches.
You may accidentally overwrite important changes or create conflicts that are hard to resolve.
Review changes with git diff or git log before merging to understand what will be combined.
Summary
Create a feature branch to work on changes separately.
Use git merge to combine branches; Git uses three-way merge to compare common ancestor and both versions.
If conflicts occur, fix them manually, stage resolved files, and commit to complete the merge.