Git In-depth Merging and ReReRe Solution
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.
[00:00:35] 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.
[00:01:43] 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.
[00:02:16] 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.
[00:02:41] 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.
[00:03:05] 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.
[00:03:48] 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.
[00:04:20] 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.
[00:04:47] 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.
[00:05:17] 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.
[00:07:10] 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.
[00:07:37] 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.
[00:08:49] 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.
[00:09:29] 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.
[00:09:58] 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.