In this post we’ll talk about how to handle conflicts in Git. Once you learn how to do it it’s not that scary, but it definitely helps to learn how to do it on code that doesn’t matter as opposed to production code. Check out all of our Git tutorials here.
Note: This is a kind of post where the video really helps, I encourage you to watch it.
What is a Git conflict?
You get conflicts when Git has files that “conflict” with each other, like when one developer modifies a file, and another developer deletes that same file in a different branch. When you try to merge those branches together Git doesn’t know whether to change the file or delete it, so a conflict occurs.
When this happens Git will take all of the changes and lay them out and basically say “These are all the conflicting parts of the code. What do you want to do?”
From there you get to decide… Does this file get deleted? Do I use this code, that code, both, or something else? Once you’ve made your changes and “resolved” the conflict, you have to make an additional commit saying you’ve resolved the conflicts. Let’s give it a try!
We’re going to create a new folder called conflictGit, initialize a repository, create two files, add some text to the files, and make the commit all in one go. (Note: I’m doing this tutorial in Windows command prompt so your commands might vary slightly)
$ mkdir conflictGit $ cd conflictGit $ git init $ echo "this is a commit from master" > file.txt $ echo "delete and modify conflict" > file2.txt $ git add . $ git commit -m "commit from master"
Ok, we have our code, and we’re ready to make a conflict. Pop open a text editor to see the changes being made to help you visualize. Now let’s move over to a different branch. From that branch we’ll REMOVE file2.txt, and modify file.txt
$ git branch dev $ git checkout dev
I went ahead and deleted my file2.txt manually and modified my file.txt to have this single line of code: file.txt
"this is a hijacked by dev branch commit from master"
Save and commit this branch, then prepare to make your conflict by switching back to master and making some edits.
$ git add . $ git commit -m "deleted file2.txt and modified file.txt" $ git checkout master
From the master branch we still have our file2.txt even though it’s been deleted in the dev branch. Let’s modify the file2.txt to create a conflict.
Then in the file.txt we’ll make another conflict by changing the code there too. Here’s our new file2.txt
"delete and modify conflict" It feels good to be alive!
and here’s our file.txt
"this is a much better commit from master"
Now save and commit these changes on the master branch. You’ve now created a conflict that Git won’t know how to handle…. Let’s add, commit, and merge to see an error like this:
$ git add . $ git commit -m "a better commit from master" $ git merge dev
There’s your conflict. Git doesn’t know what to do because in one branch file2.txt is deleted, but in another branch file2.txt has been modified. Then in file.txt the content is completely different on the same line. So git let’s you decide what to do.
If I try to open my file.txt It looks really weird… check this out:
This just shows both contents. in the HEAD section is the master branch code. underneath the ==== is the dev branch code. From here you could commit the code and it’d include all those carats and equal signs. YOU get to decide how you want this file to be. It just pulls everything in so you can see for yourself what you want.
Note. Good text editors can make this a bit easier. Check out all the stuff VSCode gives you…
I can “accept current change”, incoming changes, BOTH, or compare them… all by just clicking the appropriate link, so that makes it really easy. I’m going to use the Notepad editor from the image above to make my own custom change (Just open the file with an editor and change it to how you want). file.txt has been saved how I want it.
Now let’s keep that file that the dev branch deleted… We’re making a new commit. So if we want that file we have to add it and commit. Let’s give it a try. running $ git status shows us the state of our repository:
Since we want to keep both files let’s add them and make a commit:
$ git add . $ git commit -m "keeping file2.txt and custom file.txt"
If you wanted to DELETE the file2.txt as they did in the dev branch, well… the message tells you how to do that. Use “git add/rm file”.. if you wanted to remove the file you’d run $ git rm file2.txt
If done correctly, your master branch should have the modified file2.txt and whatever you saved for the file.txt. Here’s what my final project looks like:
Now… this is our entire project on the master branch. If you go back to the dev branch it’s still the same as it was before the merge because we made a new commit to handle the conflicts. When you go back to dev be sure to get the new version of the project. if I run $ git checkout dev then take a look at my project folder you’ll see that file2.txt is still gone:
Just run $ git merge master to pull in the changes from master.
$ git merge master
There you have it! It takes a little practice but it’s nothing too hard. Good to be confident when you’re working on a live project that’s for sure. I hope this was helpful!