Everybody has a list of things they don’t like: an appointment at the dentist, a traffic jam, or a canceled flight. When I was preparing my book, I wasn’t surprised that a lot of designers and developers would add merge conflicts to this list without hesitation.
When using Git for version control, there is nothing to fear. Once you understand how merge conflicts work and how to deal with them, I’m sure you’ll be able to cross them off this list.
You Cannot Break Things
The first thing that you should keep in mind is that you can always undo a merge and go back to the state before the conflict occurred. You’re always able to undo and start fresh.
If you’re coming from another version control system (e.g. Subversion) you might be traumatized: conflicts in Subversion have the (rightful) reputation of being incredibly complex and nasty. One reason for this is that Git, simply stated, works completely different in this regard than Subversion. As a consequence, Git is able to take care of most things during a merge – leaving you with comparatively simple scenarios to solve.
Also, a conflict will only ever handicap yourself. It will not bring your complete team to a halt or cripple your central repository. This is because, in Git, conflicts can only occur on a your local machine – and not on the server.
How a Merge Conflict Occurs
In Git, “merging” is the act of integrating another branch into your current working branch. You’re taking changes from another context (that’s what a branch effectively is: a context) and combine them with your current working files. Have a look at this introduction to branching if you’re new to the concept in general.
A great thing about having Git as your version control system is that it makes merging extremely easy: in most cases, Git will figure out how to integrate new changes.
However, there’s a handful of situations where you might have to step in and tell Git what to do. Most commonly, this is when there are changes to the same file on both branches. Even in this case, Git will most likely be able to figure it out on its own. But if two people changed the same lines in that same file, or if one person decided to delete it while the other person decided to modify it, Git simply cannot know what is correct. Git will then mark the file as having a conflict – which you’ll have to solve before you can continue your work.
How to Solve a Merge Conflict
When faced with a merge conflict, the first step is to understand what happened. Did one of your colleagues edit the same file on the same lines as you? Did they delete a file that you modified? Did you both add a file with the same name?
Git will tell you that you have “unmerged paths” (which is just another way of telling you that you have one or more conflicts) via “git status”:
Conflict Markup
Let’s take an in-depth look on how to solve the most common case: when two changes affected the same file on the same lines.
Now is the time to have a look at the contents of the conflicted file. Literally open it in your code editor. Git is nice enough to mark the problematic area in the file by enclosing it in “<<<<<<< HEAD” and “>>>>>>> [other/branch/name]“.
The contents after the first marker originate from your current working branch. After the angle brackets, Git tells us where (from which branch) the changes came from. A line with “=======” separates the two conflicting changes.
Cleaning Up
Our job is now to clean up these lines: when we’re done, the file should look exactly as we want it to look. It can be necessary to consult the teammate who wrote the conflicting changes to decide which code is finally correct. Maybe it’s yours, maybe it’s theirs, or maybe a mixture between the two.
Opening the raw file in your editor and cleaning it up there is perfectly valid, but not very comfortable. Using a dedicated merge tool can make this job a great deal easier. You can configure your tool of choice using the “git config” command. Consult your tool’s documentation for detailed instructions. Note that just because you have git installed doesn’t mean you have a dedicated merge tool installed, these are separate, optional software tools.
Then, in case of a conflict, you can later invoke it by simply typing “git mergetool”.
For this example, I’ve used Kaleidoscope on Mac:
The left and right panes stand for the conflicting changes; a far more elegant visualization than <<<<<<<” and “>>>>>>>“.
You can now simply toggle which change shall be taken. The middle pane shows the resulting code; in good tools, you can even edit this further.
Now, after cleaning up the file with the final code, all that’s left is to save it. To give Git a hint that you’re done with this file, you should quit the merge tool to continue. Behind the scenes, this told Git to execute a “git add” command on the (now formerly) conflicted file. This marks the conflict as solved. Should you decide not to use a merge tool and instead clean up the file in your editor, you’ll have to mark the file as resolved by hand (by executing “git add <filename>”).
Finally, after resolving all conflicts, a merge conflict situation needs to be concluded by a regular commit.
How to Undo a Merge
As already said, you can return to the state before you started the merge at any time. This should give you the confidence that you can’t break anything. On the command line, a simple “git merge –abort” will do this for you.
In case you’ve made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it: just roll back to the commit before the merge happened with “git reset –hard <commit-hash>” and start over again.
Tools Can Make Things Easier
I’ve already mentioned it briefly: being equipped with good tools can make your life quite a bit easier. A proper merge tool will help you better understand a conflict scenario and make the right decision.
Have Confidence
With all the safety nets in place that Git provides, there’s really no need to be afraid of merge conflicts in any way. Once understood how they work and what your options are, you should be ready to merge like there’s no tomorrow. Always keep in mind: you can’t break anything!
Note from the Editor
Tobias modestly left out Tower as an excellent tool to help with merge conflicts. In fact one of the reasons I personally use Tower is because I like how it helps with merge conflicts.
It’s very clear when you have a conflict:
And right-clicking on the conflicted file gives you some nice options:
So my process is usually:
- Is it very obvious to me whether my changes to this file or the other branches changes to this file should “win”? If so, select the relevant “Resolve <file> Using Mine” or “Resolve <file> Using Theirs”
- If I need a closer look, pop it open in a code editor and check it out. Then maybe still I can use one of those options.
- If it’s more complicated, manually fix it up and then choose “Mark <file> as Manually Resolved” from Tower.
- Sometimes use a Merge Tool if there is a lot of complex fixes, which Tower also supports opening conflicts in via the “Open <file> in Merge Tool” option.
I love Tower! Use it every day in my workflow. Would highly recommend it!
How ironic and timely: I just recently made a GitHub account. Thanks for the article!
Ironically, the list behind A proper merge tool has a notable Linux omission. As we all know, Git’s creator, Linus Torvalds is famous for another project that escapes my mind at the moment.
For the Linux users out there, checkout Meld and Gitg
Thanks I was just gonna ask about one for Linux users
Hi everybody! Here’s the author typing. Let me know if you have any questions – happy to answer if I can!
How do I report a bug in Git?
Can I access my repository over git:// or ssh:// instead of https:// ?
Yes: Git supports all of those protocols. Keep in mind that “git://” is very efficient but has no authentication mechanism (therefore good for read-only access). “ssh://” is mostly supported with SSH Public Key authentication.
A great summary.
I’ve only been using Git in anger for the last few months, and conflicts were my nightmare. But I have learned the hard way how to resolve them. Shame this article wasn’t written sooner.
And my merge / diff tool of choice is Beyond Compare.
Or you can still use SourceTree which has a usable GUI.
Interesting read. I will, however, disagree with something: You can absolutely break things.
It’s important to realize that although git will prevent you from creating a conflict in a remote repository, this doesn’t mean that you can’t break your application. It is, of course, very possible to resolve a conflict in such a way that git won’t complain about, but which will break your application.
This is true of any version control method, for sure, but I think the whole “you can’t break anything” mantra here is a little misleading.
Of course Git won’t prevent you from writing “wrong” code – that’s absolutely true. However, it provides a safety net in these cases: you can easily revert bad changes.
No matter what mistake you made, it’s rather easy to undo & recover from in Git.
For Windows users, I personally like WinMerge. Go to File > Open Conflict File… and it will open up the merge conflict using the same interface it uses to compare two separate files.
You forgot to explain how to resolve conflict for whole file… not just inside it.
For example i have conflict in 63 files -> and i know i want to keep version from remote branch for 23 of them and local version for the rest.
Example – you want to resolve conflict by keeping local file:
git checkout --ours conflict.file
or
you want to resolve conflict by keeping their version of file:
git checkout --theirs conflict.file
Yeah, You’re Right.
I just posted an article explaining how to smoothly integrate Git with P4Merge. I like P4Merge because it brings me a unified performance be it on Windows, Linux or Mac.
Give it a try? http://goo.gl/YKGvnX
I just started using Meld on Linux. It’s a really nice one for Linux users with a very straight forward documentation
thanks for correction and its work great