JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Merge branches in Git without fast-forward

Author:JIYIK Last Updated:2025/03/31 Views:

This tutorial shows how to merge branches in Git without using fast-forward.

We work in a collaborative development environment using Git. We create many branches for different purposes, and eventually, we merge these branches into the main branch to provide a release.

We merge branches in Git with or without fast-forward.

We will now illustrate this with an example.


Using the command with and without --no-ffthe (no fast-forward) option in Gitgit merge

In a collaborative development environment, we often create multiple branches in Git to serve different purposes. Then we eventually merge these branches into the main branch to provide a release version.

Sometimes, our mainline branch HEADhas commits that are ancestors of the commits of the branch we are trying to merge in. In this case, the merge is done with a fast-forward.

A fast-forward merge can happen when there is a linear path from the current branch tip to the target branch. Suppose we have branch feature1, and we merge it into our mainline branch main.

Now, suppose that commit mainof branch -i HEADis feature1an ancestor of commit of branch -i, the one we want to merge.

In this case, instead of merging the branches, all Git needs to do to integrate the history is to move (i.e.) fast-forward the current branch tip to the target branch tip.

So, in our example, by fast-forwarding, mainthe current position of the branch HEADwill move up to feature1the branch tip.

mainPlease see the following figure for the situation of branches and before merging feature1.

              E---F---G feature1
             /
A---B---C---D main

Therefore, when we git mergemerge two branches using the command, a fast-forward will occur.

To perform the merge, we execute the command as follows.

$ git merge feature1

After executing the command, mainthe branch will be fast-forwarded.

See the fast forward illustration below.

              E---F---G feature1, main
             /
A---B---C---D

Therefore, mainthe current of branch HEADis now fast-forwarded. No merge commit is created in this case.

Sometimes, we might want to do an actual merge instead of a fast-forward. Suppose we want to maintain our branch topology.

In this case, we can use the command with --no-ffoption git merge.

The command option --no-ffcauses a merge commit to be created in all cases, even if the merge could be resolved as a fast-forward.

So in our case, to do a merge without fast-forwarding, we need to do the following.

$ git merge --no-ff feature1

So now git mergethe command feaure1merges branch into mainbranch and produces a merge commit (even though it is a fast-forward merge).

This is useful for recording all merges that have occurred in a repository.

Therefore, we explain in detail how to merge branches in Git without using fast-forward.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Git diff shows diff details of uncommitted changes

Publish Date:2025/03/31 Views:105 Category:Git

This article outlines how we can get diff details of uncommitted work in Git. We use the git diff command to show the differences between various Git references, such as commits, the index, and the working tree. We can use this command to d

Staging area in Git

Publish Date:2025/03/31 Views:151 Category:Git

In this article, we will learn about the staging area in Git . Git is a version control system that maintains a history of changes made to a project directory. Git uses commits to track changes. Git has three internal management systems, on

Add all files in a folder to commit in Git

Publish Date:2025/03/31 Views:158 Category:Git

This article will discuss the necessary steps to add all of your files into one folder for submission. If you have a folder with a dozen files, adding the files one by one can be tedious. Fortunately, Git allows us to add all the contents o

Meaning of Fetch_Head in Git

Publish Date:2025/03/31 Views:64 Category:Git

This article defines Fetch_HEAD in Git . This reference is an integral part of the git pull command and is important when merging changes from a remote repository into a local repository or branch. If you're not sure what Fetch_Head means,

Get all branches in Git

Publish Date:2025/03/31 Views:62 Category:Git

This article discusses how to fetch all branches from a remote repository. The git fetch command is a useful utility when you want to download changes from a remote repository without having to update your local branches. Sometimes, you may

Clone a Git repository with a specific revision

Publish Date:2025/03/31 Views:82 Category:Git

This article discussed various methods that we can use to clone a Git repository with a specific revision or changeset. This comes in handy when you have a repository with large files and you only need a specific version of the code. Instea

Squash commits pushed in Git

Publish Date:2025/03/31 Views:86 Category:Git

This article outlines the process of squashing commits that we have pushed to a remote repository. We squash the commits into one to reduce clutter in the repository. To squash the commits, we run git rebase in interactive mode . Squash com

Git squash all commits

Publish Date:2025/03/31 Views:65 Category:Git

In every developer’s life, the word squash is often used while working with the Git distributed control system . This feature in Git is a handy option that developers often use to achieve a neat workflow in their development team. In this

Close the Git commit editor on Windows

Publish Date:2025/03/31 Views:62 Category:Git

In this article, we will discuss how to exit the Git commit editor. This can be a little tricky, especially if you are new to Git bash . Let's see how to exit the editor on Windows. Close the Git commit editor on Windows We will look at a t

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial