Git performs a fast-forward merge by following these steps:
-
Check for Divergence: Git first checks if the branch being merged (e.g.,
feature-branch) has any commits that are not present in the target branch (e.g.,master). A fast-forward merge is only possible if there are no divergent commits. -
Move the Pointer: If there are no divergent commits, Git simply moves the pointer of the target branch (e.g.,
master) forward to the latest commit of the branch being merged (e.g.,feature-branch). This effectively updates the target branch to include all the commits from the source branch. -
Update the Working Directory: The working directory is updated to reflect the new state of the target branch, which now includes the changes from the merged branch.
Example Command:
To perform a fast-forward merge, you would typically use the following command:
git checkout master
git merge feature-branch
If a fast-forward merge is possible, Git will execute it without creating a new merge commit, resulting in a linear commit history.
Visual Representation:
Before the merge:
A -- B -- C (feature-branch)
\
(master)
After the fast-forward merge:
A -- B -- C (master, feature-branch)
This process keeps the commit history clean and linear, making it easier to follow.
