Check out a free preview of the full Git In-depth course:
The "Merge Conflicts" Lesson is part of the full, Git In-depth course featured in this preview video. Here's what you'd learn in this lesson:

Nina explains that merge conflicts happen when files have diverged too far. After explainigng that git stops the merging until the conflicts resulting from the attempted merge are resolved, Nina reviews "git rerere" command, which reuses recorded resolutions of future conflicted merges. - https://git-scm.com/docs/git-rerere

Get Unlimited Access Now

Transcript from the "Merge Conflicts" Lesson

[00:00:00]
>> Nina Zakharenko: Merge conflicts. These happen when we try to merge but our files have diverged, the changes are not compatible. Git's going to stop until the conflicts are resolved.
>> Nina Zakharenko: During the merge, git is going to try to merge in your changes on the branch with various strategies.
>> Nina Zakharenko: And when that contact can't be automatically merged then you'll get a conflict like this.

[00:00:31] You'll probably see this message lots of times if you work on a branch that has lots of contributors or lots of people making changes to the same files.
>> Nina Zakharenko: Now, git's gonna create a new file. It's gonna contain those conflicts, and you can make edits, have the solution, and then commit, and continue.

[00:00:55] Later in the course, we're going to cover what is gonna happen when your merges go horribly, horribly wrong, and how to fix that. But for now, I wanted to show you a really cool tool. Git Rerere, which stands for Reuse Recorded Resolution is a really, really helpful tool.

[00:01:16] What it does is when it's turned on, git's gonna save how you resolved a conflict. And next conflict, it's just gonna reuse that same resolution. It's really useful for things like a long lived feature branch, like a refactor, or when you're rebasing. Has anyone tried a rebase, and then every time you rebase, just merge conflicts keep coming up over and over again?

[00:01:42] Yeah, big nod. So this is gonna save you. And it saved me recently when I was working on a massive refactor. I work at Venmo and even though it's a hot, cool new tool, the reality is the code base is about eight years old now. So I was working on a big refactor of our email system, and that email system is used all over the code base.

[00:02:09] The refactor took a really long time. It was one of the earliest features, it just was everywhere. And the problem was that new features that were being added were also calling the email code. So as I was working, new conflicts would be introduced every day or every few days.

[00:02:28] And I'd have to resolve those merge conflicts every time I tried to pull down master. Using git rerere, I didn't have to make the same resolutions in the same files over and over again. It was one and done, and it saved me hours of work. So if you find yourself in this situation, git rerere is truly awesome.

[00:02:50] Now, how do we use it? First, we have to turn it on. We have two options for turning it on. We can use git config and then rerere.enabled, and you'll wanna pass in true or one. That will turn on git rerere in just your current project. If you wanna turn it on for all projects, you're gonna want to use the --global flag when calling git config.

[00:03:25] Now, let's see how it works. I've turned on rerere in my git config, and now I try to merge in my feature. I have a conflict, and I've solved the conflict, and then added the file. Now, I've done my commit, resolved the conflict. Git will tell you when you commit that it's recorded resolution for that conflict.

[00:04:07] So next time that same conflict comes up, it'll be matched. So my feature wasn't ready. I undid the merge. When I try to merge again,
>> Nina Zakharenko: We'll see that,
>> Nina Zakharenko: Git has added the recorded resolution back to our staging area. So it won't commit it for you automatically.

[00:04:38] It'll stop and let you take a peek and make sure that everything's correct.
>> Nina Zakharenko: But we'll see that in our message that the resolution was applied automatically.