Git In-depth

Merging and ReReRe Solution

Git In-depth

Check out a free preview of the full Git In-depth course

The "Merging and ReReRe Solution" 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 walks through the solution to Merging and ReReRe Exercise.


Transcript from the "Merging and ReReRe Solution" Lesson

>> Nina Zakharenko: Let's go through exercise4 about Merging and ReReRe. At this point you should have the advanced git exercises repository cloned locally. Now let's check out the exercise4 branch.
>> Nina Zakharenko: The first thing we wanna demonstrate is a fast forward commit.
>> Nina Zakharenko: If we take a look at our log, we'll see that we're on the initial commit.

We have exercise3, our feature branch and we have a new commit in that one. So if we try to merge it into exercise4, let's try to do that with a regular merge.
>> Nina Zakharenko: And we'll see here that Git did a fast-forward commit. If we look at our history again, we'll see that,

>> Nina Zakharenko: We'll see that the branch pointer has been updated.
>> Nina Zakharenko: But there's nothing saying that this commit was associated with a different exercise. In this case exercise3. And we're gonna want a little bit more information. Remember that fast forward commits happen when the branches, the base branch, the one, that you branch from, doesn't have any additional commits.

It hasn't changed since you branched from it. And that history is linear. Let's try a non-fast forward commit to show the difference.
>> Nina Zakharenko: I'm going to reset to the previous head, and we're gonna go through git reset in detail in a few in a few hours. I'm sorry, in a few sections, not in a few hours.

I'm not trapping you guys here forever, I promise. What we did here is reset to the commit before our merge. Now let's try to open, I'm sorry, let's try to merge the commit with the no fast forward flag. So before we git merge exercise3, and now we're gonna try it with the no fast forward flag.

This is gonna open an editor in your terminal. If you're having problems with your editor, go back to the beginning. You'll see a section for prerequisites. It's very important to set up your editor. It will make working with Git a lot easier. This is the editor that I use, Emacs.

And it opened commit saying, this is the merge of branch exercise3 into exercise4. And we'll see that the merge was made by the recursive strategy instead of the fast forward commit. I'm sorry, instead of the fast forward merge. And now when I look at my log again, I'll see this new merge commit here.

And I can make that even clearer by using the --graph flag for log. We'll see here this was the commit made on a different branch, and here it is being merged back.
>> Speaker 2: What's the extent of the auto fast forwarding? Like, if you have a base and you branch it, make a commit and branch that, and make another commit, so you're kind of say a branch away from base.

And nothing's been done. So if you were to merge that into base, would it automatically fast forward kinda across?
>> Nina Zakharenko: If you were to merge the very last branch.
>> Speaker 2: Right.
>> Nina Zakharenko: You'll have to verify this, I've never done that. But it should just work. Yeah, Git should know about the history from that very first branch.

So this is an example of a no fast forward commit. Now let's set up for a conflict. We're gonna check out another branch. Whoops, we're on exercise4. We're going to Git check out a new branch called mundo. For those who don't know, mundo is world in Spanish. So we're gonna try a different version of our Hello world.

So we're going to edit our text file and I'll just use echo for this example here.
>> Nina Zakharenko: So now if we look at our hello.txt, we'll see it says Hello Mundo! If we do our git status, we'll see that hello was modified. And let's add it and do a commit.

>> Nina Zakharenko: So back on our exercise4 branch. The one we were working on before. And quick shortcut. For those of you who switch branches lots, if you wanna switch to the previous branch that you were just on, you can do that with the dash as a shortcut. Since exercise4 was the branch I was just on, I'm gonna check it out.

>> Nina Zakharenko: And I'm gonna edit my hello.txt again. So now it's gonna say, Hello World. And I'm gonna commit that.
>> Nina Zakharenko: Actually, I want it to say, Hola World.
>> Nina Zakharenko: And I'm gonna commit that
>> Nina Zakharenko: Great, so let's check out how git rerere can help us with merge conflicts.

We're going to turn it on. In this case it's git config rerere.enabled true. If I want to turn it on for all projects, remember that I can pass in the global flag here. I'm gonna try mundo and I'm gonna get a merge conflict. But let's notice this line.

It says recorded preimage for hello.txt. We can do git rerere diff to see what's going on. And that shows us our merge conflict. In one version we have Hello Mundo, in the other version, we have Hello World. And this shows us what the current state is of the resolution

>> Nina Zakharenko: Let's resolve that conflict.
>> Nina Zakharenko: I wanna change it so that it says, Hola Mundo.
>> Nina Zakharenko: Great, so now if I look at my text file, it has the right thing in it. If I do git rerere diff
>> Nina Zakharenko: It'll show me,
>> Nina Zakharenko: What the recorded resolution would be right here at the bottom.

Now let's git add that,
>> Nina Zakharenko: And commit it.
>> Nina Zakharenko: Cool, and now we'll see this message right here, Recorded resolution for 'hello.txt'. Let's back up and merge again and we'll see what ReReRe does for us. We're going to git reset back to the previous commit. And we're gonna try to merge in window again.

We'll see that there is a conflict. But here Git's telling us it used ReReRe to resolve, using a previous resolution. If we look at the git status, we'll see that hello.txt is modified. But if we look at the file, we'll see that it used our previously recorded resolution.

All we need to do is stage hello.txt and commit it again. So ReReRe is powerful. It doesn't do anything for you, you still get to review the purposed, the recorded resolution before you commit it but it will save you tons and tons of time, one of my absolute favorite tools.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now