Fast Forward Merge in Git Explained with Examples
When working on new features, you often create separate branches in Git. At some point, you’ll want to bring your feature branch back into main. In this blog, we’ll learn branch merging, focusing on the fast forward merge, and also see how to list merged and unmerged branches.
Setting Up the Project
We start with a fresh project:
mkdir project3
cd project3
git init
Add a file and make the first commit:
echo "Hello World" > hello.txt
git add .
git commit -m "Initial commit"
Visualization shows one commit at this stage.
Creating a Feature Branch
Now we need to add a chat feature. For this, we create a new branch:
git switch -C feature/chat
We are now on the feature/chat branch. Add and commit files:
echo "Chat feature - step 1" > feature_chat.txt
git add feature_chat.txt
git commit -m "Add chat feature step 1"
echo "Chat feature - step 2" >> feature_chat.txt
git add feature_chat.txt
git commit -m "Add chat feature step 2"
At this point, feature/chat has multiple commits ahead of main.
Switching Back to Main
Check main:
git switch main
cat hello.txt
# Hello World
The main branch does not have the chat feature commits yet.
What is Fast Forward Merge?
A fast forward merge happens when the target branch (e.g., main) has not diverged. Git simply moves the main branch pointer forward to the latest commit of feature/chat.
Diagram Before Merge
Here, main points to the first commit, while feature/chat is ahead.
Performing Fast Forward Merge
On the main branch:
git merge feature/chat
Now no new commit is created. Git just advances the main pointer.
Diagram After Merge
Both main and feature/chat now point to the same commit. That’s a fast forward merge.
Listing Branches
See all branches:
git branch
# main
# feature/chat
List branches that are merged into main:
git branch --merged
List branches that are not merged:
git branch --no-merged
If you create a test branch without merging:
git branch test
Then after making a commit in test, you’ll see it under git branch --no-merged.
Summary
- Fast forward merge simply moves the pointer of the target branch forward.
- It only works if the target branch has not diverged.
- Use
git branch --mergedto see merged branches. - Use
git branch --no-mergedto see unmerged branches.
Keep Learning 🚀
👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.
👉 My YouTube Channel – More videos, more fun, and lots of learning!
👉 📺 Watch this topic in video form
Stay curious, keep coding, and let’s make learning fun together! 🎉