Dealing with merge conflicts is stressful, but it doesn’t have to be!
One of my first tasks in my new role has been dealing with merge conflicts and getting our code branches cleaned up. While I’ve been using git for a few years now, I’ve honestly never had to deal with anything like a merge conflict until now.
My initial feeling was pure terror. “What do I do? How do I go about handling this? What is the usual process for handling this? What all could be broken?” All of these thoughts raced through my head.
But the reality is that git makes it really hard for you to screw up. There’s really nothing to worry about, as long as you don’t do anything stupid.
I’m sure there are other ways of doing it, but I wanted to share the process that I’ve come to use when I’m dealing with handling conflicts.
1. Update the branch to be merged
Step one is making sure that you have your local copies up to date. Do a pull to update before you start.
2. Checkout working branch
Step two is to swap back over to the branch that you’ve been doing your work in that’s now fallen out of date.
3. Merge to your working branch
Step three is to merge in the main branch to the branch you’ve been working on. This can be done by issuing a
git merge <branch> command.
When run, you’ll get a message that informs you that the merge couldn’t be completed because of conflicts. Running a
git status will confirm this and show you the files with merge issues.
4. Edit conflicts in editor
At this point I usually switch from the command line over to my IDE to make the changes. IntelliJ seems to have some tools to help deal with merge issues, but I didn’t have much luck with them. The first time I tried using them, it overwrote all of my changes and I had to go back and fix everything a second time.
Conflicts can be identified by the ««< ====== »»> symbols that are found in the file.
5. Add updated file
After making the needed changes to the file to fix the conflict, I will add the updated file via the command line. This helps me to keep track of what I’ve fixed and what I have left.
6. Repeat steps 4 and 5 until all files are updated
Just keep repeating this process until all of the conflicting files have been fixed and the changes have been staged.
7. Commit the changes
Commit the changes to your branch. This commit will complete the merge into your local branch.
8. Open a pull request back to the main branch
Now that you’ve done all of the hard work getting your local branch synced up with the main branch, you’re safe to go ahead and merge it back in.
As I said, I’m sure there’s other ways to do this, and I’m sure that some are easier that what I outlined above. But for me, it works, and I feel pretty good about it.