0
0
Gitdevops~7 mins

Resolving merge conflicts in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When two people change the same part of a file in different ways, Git cannot decide which change to keep. This is called a merge conflict. You need to fix these conflicts manually to combine the changes.
When you try to merge a feature branch into the main branch and both have changed the same lines in a file.
When you pull changes from a remote repository and your local changes conflict with the incoming ones.
When you rebase your branch onto another branch and conflicts appear in the process.
When multiple team members edit the same file and push their changes at different times.
When you want to combine changes from two branches but Git cannot automatically merge them.
Commands
This command tries to merge the 'feature-branch' into your current branch. If there are conflicts, Git will stop and tell you which files need fixing.
Terminal
git merge feature-branch
Expected OutputExpected
Auto-merging example.txt CONFLICT (content): Merge conflict in example.txt Automatic merge failed; fix conflicts and then commit the result.
Shows which files have conflicts and need to be fixed before you can complete 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: example.txt no changes added to commit (use "git add" and/or "git commit -a")
Open the conflicted file in a text editor to manually fix the conflict by choosing which changes to keep or combining them.
Terminal
nano example.txt
Expected OutputExpected
No output (command runs silently)
Mark the conflict as resolved by adding the fixed file to the staging area.
Terminal
git add example.txt
Expected OutputExpected
No output (command runs silently)
Complete the merge by committing the resolved changes with a message explaining the fix.
Terminal
git commit -m "Resolve merge conflict in example.txt"
Expected OutputExpected
[main 1a2b3c4] Resolve merge conflict in example.txt
-m - Add a commit message inline
Key Concept

If you remember nothing else from this pattern, remember: you must manually fix conflicting files and then add and commit them to complete the merge.

Common Mistakes
Trying to commit before resolving conflicts and adding files.
Git will not allow the commit because conflicts are still present and unresolved.
Open conflicted files, fix conflicts, then use 'git add' before committing.
Ignoring conflict markers in files and committing them as is.
The conflict markers will remain in the code, causing errors or broken code.
Remove conflict markers and ensure the file content is correct before adding and committing.
Using 'git merge --abort' without trying to fix conflicts when you want to keep changes.
This cancels the merge and discards the merge attempt, losing the chance to combine changes.
Manually resolve conflicts instead of aborting if you want to keep both changes.
Summary
Run 'git merge' to combine branches; Git will stop if conflicts occur.
Use 'git status' to see which files have conflicts.
Manually edit conflicted files to fix conflicts, then 'git add' them.
Finish by committing the resolved files to complete the merge.