adrianvlupu
adrianvlupu

Reputation: 4618

What is fast-forwarding?

Is it OK to assume that fast-forward means all commits are replayed on the target branch and the HEAD is set to the last commit on that branch?

Upvotes: 343

Views: 398438

Answers (3)

Abimaran Kugathasan
Abimaran Kugathasan

Reputation: 32458

When you try to merge one commit with a commit that can be reached by following the first commit’s history, Git simplifies things by moving the pointer forward, because there isn't any divergent work to merge together—this is called a fast-forward.

For more: 3.2 Git Branching - Basic Branching and Merging

In another way,

If master has not diverged, instead of creating a new commit, Git will just point the master to the latest commit of the feature branch. This is a fast forward.

There won't be any merge commit in fast-forwarding merge.

Upvotes: 317

Prajval Singh
Prajval Singh

Reputation: 1111

Imagine there is a branch

Master : A---> B --->

There are two commits on it, A and B There is another feature branch, from B, it has the following commit History

Feature : C--->D---->E

So the present it looks like this

`A--->B`
     `C--->D--->E `

So when you want to merge it, you can do it by fast forward merge by moving the head from B to E and incorporating the feature branch in the main branch and it looks like this

A--->B--->C--->D--->E with the header at E.

This example by Atlassian is so good: Atlassian example

Atlassian Example

But as Mentioned in the example, this is only possible when the branches have not diverged, in which case the traditional merge or --no-ff works.

Another Good Example

Upvotes: 21

Kaz
Kaz

Reputation: 58500

In Git, to "fast forward" means to update the HEAD pointer in such a way that its new value is a direct descendant of the prior value. In other words, the prior value is a parent, or grandparent, or grandgrandparent, ...

Fast forwarding is not possible when the new HEAD is in a diverged state relative to the stream you want to integrate. For instance, you are on master and have local commits, and git fetch has brought new upstream commits into origin/master. The branch now diverges from its upstream and cannot be fast forwarded: your master HEAD commit is not an ancestor of origin/master HEAD. To simply reset master to the value of origin/master would discard your local commits. The situation requires a rebase or merge.

If your local master has no changes, then it can be fast-forwarded: simply updated to point to the same commit as the latestorigin/master. Usually, no special steps are needed to do fast-forwarding; it is done by merge or rebase in the situation when there are no local commits.

Is it ok to assume that fast-forward means all commits are replayed on the target branch and the HEAD is set to the last commit on that branch?

No, that is called rebasing, of which fast-forwarding is a special case when there are no commits to be replayed (and the target branch has new commits, and the history of the target branch has not been rewritten, so that all the commits on the target branch have the current one as their ancestor.)

Upvotes: 112

Related Questions