What Is a Git Merge Fast Forward?

Hugo EscafitHugo Escafit
4 min read

A Git fast forward is an extremely useful and efficient mechanism for harmonizing your project's main branch with changes introduced in a given feature branch. Git makes ample use of fast-forwarding merges behind the scenes, speeding up your development workflow in the process.

Fast-forward merges can help keep your commit history clean and readable without erasing important information. In fact, many developers intentionally maintain their projects' repositories with this in mind, favoring fast-forward merges for their convenience and readability. However, it helps to know when a Git merge fast forward can and should be used to make the most of it.

What happens when you merge

Git does a number of things to ensure your project's history is maintained whenever commits from separate branches are merged back in. In the event that there are conflicts of any kind between the new code being merged in and the existing code in the main branch, Git will request that someone intervene to resolve them. Assuming there are no conflicts in need of human intervention, Git will find the most efficient way to put your code together on its own. However, depending on your project's organizational needs, it may make sense to take control of this process on a deeper level. To make a more informed decision, it helps to know how Git's two different types of merges work and when they’re best used.

How Git handles three-way merges

A three-way merge with Git makes it possible for project branches to be rejoined with the main history even when both of these have been altered. This is achieved by pulling three separate versions of your code together—the current main branch, your commits to be merged, and a common ancestor of the two. With all three of these in memory, Git then determines whether their differences can safely coexist or need to be resolved by a member of your team first.

Three-way merges are a great way for you to keep track of important feature additions and development milestones in your project as they leave a visible merge commit in place when they’re used. However, they are not always so useful, especially if your project has a vast assortment of small commits happening at any given time that you’re not interested in recording.

For instance, bug fixes and other minor alterations to your project's code can quickly clutter up its history with an ever-expanding list of merge commits. Most of these commits don’t need to be tracked in this way and can instead be merged using Git's fast-forward merge algorithm.

How a Git merge fast forward happens

When Git detects that your commit is about to be merged into your project's main branch without the main branch having been modified since your feature branch was first made, it chooses to use a fast-forward merge instead of a three-way merge. Fast-forward merges literally move your main branch's tip forward to the end of your feature branch. This keeps all commits created in your feature branch sequential while integrating it neatly back into your main branch.

How to fast-forward merge with Git

You can fast-forward merge Git without explicitly telling it to do so by ensuring no new changes have been performed on your main branch since your feature branch was first generated. Of course, this is not always a possibility with fast-moving projects being worked on by large, distributed teams. Instead, you can take advantage of rebasing operations to ensure your next commit can be completed with a clean fast forward.

If you really don't want anything else than a fast-forward merge, you can pass the --ff-only option to git merge. Git will abort the merge if a fast-foward is impossible.

Rebasing to perform a fast-forward merge on Git

Rebasing can be used to create a merge fast forward on Git thanks to its ability to make both the master branch and your feature branch's history (besides the new feature branch changes) identical. By rebasing, you are effectively copying all of the changes that have taken place on th3e main branch while you were busy working on your feature branch. Once these are the same and you’re sure your code still works, you can merge with the master branch without triggering a three-way merge operation. Git will move the tip of the master branch up to the tip of your feature branch with a Git fast forward instead of searching for a common ancestor.

If you're using GitHub and want to rebase your pull request without doing anything, you can leverage Mergify rebasecommand:

You can use Mergify to implement any type of Git history. It can be used to rebase before merging and assure you always do fast-forward merge.

Make merging simpler with Mergify

Mergify can help your team tackle the complexities of merging so they can spend more time coding.

From queueing up merge operations to assigning reviewers, labeling, and more, Mergify helps make sense of your team's workflow by automating the redundant bits.

Automation with Mergify reduces the potential for errors to eat away at your team's productivity and profitability. Learn more about Mergify and how we can help with a quick demo today.

0
Subscribe to my newsletter

Read articles from Hugo Escafit directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hugo Escafit
Hugo Escafit

Developer Relations, Mergify 🚀