• Servers and Tools

Fast Forwards

From the class:  Git Merging

[INAUDIBLE] There's a special kind of merge that can happen called a fast forward. That's where, if I'm working on a feature branch, and I just keep making progress, and nothing else happens in the master branch, when I merge back into the master branch, Get can just move the head pointer up to the last commit of the feature branch. It could just move this pointer because there's no commits that have to actually be merged together.

Let me show you an example of that, because it might be a little confusing. We'll start off by creating a feature branch. I'll say Get Check Out, with the B option to create the branch if it doesn't exist. And I'll call it Feature 2. OK?

So now, we've got a couple of branches here. And I'm working on Feature 2.

And in Feature 2, I'm going to echo out the second line. We'll append it to the 2.txt file. Now, I have one change-- the second line added to the 2.txt file. I'll commit and add that change to the index at the same time.

And now, I will check out the master branch again. Now, I haven't done any work on the master branch. I've only been working on the feature branch.

And actually, let me go back to the feature branch quickly. And if we look at the log, notice that the last commit of the feature branch is this one right here. 1D19. And all we've done is we've added this commit here. So on the master branch, Head, which, remember from the last class, points to the latest commit or to the reference like master, which points to the latest commit, is going to be right here. And then, in our feature branch, we're going to be up here.

So all we need to do to merge in this feature branches just to move the head to point to this. And we're all done. That's called a fast forward.

Let's actually see it in action.

So I'll check out the master branch again, and merge Feature 2. OK. Notice it tells us that it did the merge, but it's called a fast forward. And we didn't actually get a merge commit as a result of doing this, because there was nothing that actually had to be merged. It just had to move the head pointer.

If we look at the logs and add the graph option, you can get a visual representation, and see what happened here is last time, we were at this commit. And then, we created a feature branch. Added this commit. And then, just moved Head to point to this latest commit. So that's a fast forward merge.

Now, sometimes you don't want to fast forward merge, especially if you're bringing work from someone else. You might want to tell Get, just do a regular merge with a merge commit, even if you don't have to do it. And that's fine. You can give us special option to the merge called No Fast Forward. And that will tell it to do a regular commit.

Let's try it out.

I'll check out a new branch. This time, we'll call it Feature 3. And we'll echo out one more line to 2.txt, and again, commit that change.

And now, we'll look at the log again with a graph. And this is our new commit. And if we just did a fast forward, this log would actually be the exact log that we would see on the master branch. Right now, remember, the master branch is pointing just to this commit.

But we're going to tell Get, no, we don't want that. We actually want you to do the full merge commit.

So I'm going to quit this. And we'll check out the master branch again. And I'll Get Merge Feature 3 No Fast Forward.

So it does an actual merge. Creates a commit. We give it the commit message. And then when I write Quit out of this, it actually makes a new commit, which we can see with the log.

Here's the commit we made in the feature branch. Added another line to 2.txt. And here's the commit that was created automatically when we did the merge.

And it was really not necessary, since there were no commits that continued along the branch-- see, you don't see stars along here. So we could have just moved the pointer right up to this one. But sometimes, this is useful when you're merging in work from other people, because when you look at the history, the Get history-- that's these logs here-- you want to see that there was this branch. You don't want to lose track of that. Sometimes it's useful to see, for example, when you're pulling in pull requests, that there was a branch that happened. And this will allow you to see that clearly.