In most cases, Git can resolve differences between branches and merge them. This is because developers are usually making changes to different lines or files. There are some cases where Git cannot merge a repository. These situations are called merge conflicts.
In this guide, we’re going to talk about what merging is, what merge conflicts are, and how you can resolve a git merge conflict. Let’s begin!
What is a Merge Conflict?
A merge conflict is where Git cannot merge two branches without your help.
Merges are a cornerstone feature of version control systems like Git. They allow developers to maintain multiple separate lines of development in a repository. This means one developer can work on fixing bugs on one branch without having to change the main copy of a project.
Merge conflicts arise in two scenarios:
- Two branches have changed the same line of code.
- One branch has removed a file which has been changed on another branch.
When a merge conflict arises, an error will be presented in Git. This tells you that Git was unable to successfully merge branches.
How to Solve a Removed File Conflict
Git branches are independent from each other. This means that a developer can remove a file on one branch without that change affecting the other branch.
Merge conflicts can occur if a change is made to a file on one branch, and that file is removed on another branch. This is because Git does not know whether to delete the file or change it.
Let’s say that we have two branches: dev and master. On the dev branch, we have removed the README.md file. On the master branch, we have added new text.
We can merge these branches using the git merge command:
git merge dev
This will merge the “dev” branch onto our current branch, which is master. Our code has returned a merge conflict:
Auto-merging README.md CONFLICT (content): Merge conflict in README.md Automatic merge failed; fix conflicts and then commit the result.
To solve this conflict, we need to make a decision: do we want to keep the file, or change it? If you want to keep the file, use the git add command and add it back to the repository:
git add README.md
Otherwise, use the git rm command to remove it from your repository:
git rm README.md
The rm command
will delete the file from your local repository. Before you use the rm command, double check to make sure you want to remove the file.
Once you have chosen what to do with the conflicted files, you can create a commit with your changes and merge the branch:
git add README.md git commit -m "feat: Add README.md to repository" git merge dev
The git add command stages our changes. The git commit command creates a new commit. The git merge command merges our dev and master branches. Our branches can now merge because we have resolved the merge conflict.
How to Solve a File Change Conflict
Developers sometimes make changes to the same files on two different branches. It is impossible for Git to distinguish which change to keep when two branches are being merged.
In this case, a merge conflict will arise. Let’s suppose we have a repository with two branches: master and dev. We have made a change to the README.md file on both branches. This has caused a merge conflict:
Auto-merging README.md CONFLICT (content): Merge conflict in README.md Automatic merge failed; fix conflicts and then commit the result.
The first step is to identify the root cause of a merge conflict. You can do this by using the git status command:
On branch master You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: README.md
We can see from this message that the file README.md
was modified on both branches. Now, we have to resolve our merge conflict. Let’s open up our README.md file using a text editor:
This is a README.md file. This is in a repository called merge-conflicts. <<<<<<< HEAD This file is on the main branch. ======= This file is on the dev branch. >>>>>>> dev
Git has edited the file to tell us that a merge conflict exists and added conflict markers. Git uses less than, greater than, and equals sign characters to express the merge conflict.
Less than signs (<) represent the changes made to the current branch. Greater than signs (>) tell you the change made to the branch which you tried to merge to your current branch. The equals signs (=) separate out the differences.
In this case:
- “This file is on the main branch.” is on our master branch (or HEAD).
- “This file is on the dev branch.” is on our dev branch.
To resolve this merge tool conflict, all you need to do is remove the text that you do not want. Let’s say that we want our code to say: “This file is on a branch.” We could make this amendment like so:
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
This is a README.md file. This is in a repository called merge-conflicts. This file is on a branch.
Once we have made these changes, we are ready to make a final merge. Let’s stage our changes, commit them to our repository, and merge our dev
and master
branches:
git add . git commit -m "feat: Resolve merge conflict." git merge dev
Now that we have resolved our merge conflict, our code successfully merges.
Conclusion
Merge conflicts arise when you make changes to the same line of text over two branches or where you change a file on one branch and remove that file on another branch.
In this guide, we’ve discussed how to resolve merge conflicts in both cases. Now you’re ready to start tackling merge conflicts like a Git expert.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.