Git Merge –no-ff Option

29/12/2020
Git

Understanding Git Merge no-ff Option

The easy merge capability of git is one of its strengths. During a merge, git uses fast-forward merge when it notices that the HEAD of the current branch is an ancestor of the commit you are trying to merge. In a fast-forward merge, there’s no new commit. Git just moves the pointer. If this behavior isn’t desirable, you can use the no-ff flag to create a new commit for the merge.

How Merge Looks With and Without Fast-Forward

After a fast-forward, your git history will look like this:

C0 —> C1  —> C2—> C3

For the same number of commits, here is a merge history without fast-forward:

In the first case, there is no indication that there was any branching. In the second case, the history is showing a C4 commit to indicate where the merge occurred.

Walking Through an Example

You will create a git repository, create a branch and then try the merges with and without fast-forward.

Section 1: Setup

First, you can create the git repository with the following steps:

$ mkdir my_project
$ cd my_project
$ git init
$ touch a.txt
$ git add -A
$ git commit -m "C0: Adding a.txt"

Now let’s create a branch called features and commit a few changes:

$ git branch features
$ git checkout features
$ touch b.txt
$ git add -A
$ git commit -m "C1: Adding b.txt"
$ touch c.txt
$ git add -A
$ git commit -m "C2: Adding c.txt"
$ touch d.txt
$ git add -A
$ git commit -m "C3: Adding d.txt"

Section2: Merge With Fast-Forwarding

Let’s go back to master branch and merge features branch into it:

$ git checkout master
$ git merge features

Output:

Updating 08076fb..9ee88eb
Fast-forward
b.txt | 0
c.txt | 0
d.txt | 0
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
create mode 100644 c.txt
create mode 100644 d.txt

If you check the history, you’ll see:

$ git log –oneline
9ee88eb C3: Adding d.txt
c72b92c C2: Adding c.txt
2e4039e C1: Adding b.txt
08076fb C0: Adding a.txt

So, all of the commits from features branch is in the master branch now. If you continue making changes to master, there is no way to know when the features branch was merged into it.

Section 3: Without Fast-Forwarding

Repeat Section 1 for a new folder.

Then, try a merge without fast-forwarding:

$ git checkout master
$ git merge –no-ff feature

It will open up the following in your git’s default text editor:

Merge branch ‘features’
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with ‘#’ will be ignored, and an empty message aborts
# the commit.

Modify the comments. In this case, you can just add “C4: ” before “Merge branch ‘features’”. The output should look like this:

Merge made by the ‘recursive’ strategy.
b.txt | 0
c.txt | 0
d.txt | 0
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b.txt
create mode 100644 c.txt
create mode 100644 d.txt

Now if you check the history, it should look like the following:

$ git log –oneline
e071527 C4: Merge branch ‘features’
bb79c25 C3: Adding d.txt
692bd8c C2: Adding c.txt
a0df62a C1: Adding b.txt
7575971 C0: Adding a.txt

You can see that even though you have the exact same changes, this version of merge has the extra C4 commit which signifies the merging of features branch into master.

Conclusion

The git merge no-ff flag helps create a more readable history. It allows you to put tags that clearly shows where the merges occurred. It can save you time and effort during debugging.

Further Study:

References:
ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Git Bisect Tutorial

Commenting your commits is an essential part of maintaining traceable code. It helps you track problems. However, finding...
28/12/2020

How to Use git blame

git blame is a very good tracking command for Git. git blame shows the author information of each line of the project’s...
29/12/2020

GitHub vs GitLab

GitHub vs GitLab: A Look at Git Remote Repository Managers Today Git dominates the version control market with GitHub being...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Huớng dẫn dùng proxy cho ios, iphone 2023
23/09/2023

Cách gắn set proxy cho điện thoại android, oppo, giả lập android, Ldplayer Bằng Proxydroid
20/09/2023

Mua Proxy Socks5 VN Chơi Game Gia Lập Tăng Cường Trải Nghiệm Chơi Game
22/06/2023

Mua Proxy Mỹ, Us Nuôi Tài Khoản Etsy, eBay Tìm Hiểu Về Mua Proxy Mỹ tại Onet.com.vn
22/06/2023

Mua Proxy Game – Giải pháp tuyệt vời cho việc chơi game trên mạng mà không bị giới hạn về vị trí địa lý
03/06/2023