Merging Branches
Understanding Branch Merging
In Git, merging is the process of integrating changes from one branch into another. The most common use case is merging feature branches into the main branch (e.g., main or master). This ensures that the latest features and bug fixes are incorporated into the main codebase.
Basic Merge Workflow
First, view the two most recent commits.
Remember, you are currently on the feature/new-feature branch.
As you can see, the master branch is one commit behind.
Before merging, you must switch to the branch where the changes will be integrated.
Switch to the master branch and display the two most recent commits on it:
The master branch is indeed one commit behind the feature branch.
To merge them, use the git merge command and specify the branch to merge into the current branch.
For example, to merge the feature/new-feature branch into master, run:
The message displayed after running the command contains the following: Fast-forward, which specifies that the fast-forward merge was performed.
You'll explore the two types of merges later in this chapter. Before that, view the two most recent commits:
You are still on the master branch, but it is now up to date with the feature branch, with both branches pointing to the same commit.
The HEAD still points to the master branch because you were on this branch when performing the merge.
Merge Types
There are two types of merge operations in Git:
- Fast-forward merge;
- Three-way merge.
Fast-forward merge
The merge you performed is a fast-forward merge, as indicated in the message. This type occurs when all commits from the currently checked-out branch are also present in the branch being merged, meaning their commit histories do not diverge.
Refer to the following illustration for clarification:
With this type of merge the pointer of the checked out branch is simply updated to the latest commit.
Three-way merge
However, if the history of the branches has diverged meaning that after a certain common commit new commits were made separately on each branch, then a three-way merge is performed.
The term "three-way" refers to the three versions involved in the merge:
- the version of the most recent common ancestor (commit);
- the version of the latest commit in the current branch (HEAD);
- the version of the latest commit in the branch being merged.
Take a look at the following illustration of this type of merge:
Here is how a three-way merge works:
- Git identifies the changes made on each of the branches after the most recent common ancestor;
- If each branch has changes in different files or different parts of the same file, Git will automatically merge the changes by combining them in the resulting commit;
- Otherwise a merge conflict will occur.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.57
Merging Branches
Swipe to show menu
Understanding Branch Merging
In Git, merging is the process of integrating changes from one branch into another. The most common use case is merging feature branches into the main branch (e.g., main or master). This ensures that the latest features and bug fixes are incorporated into the main codebase.
Basic Merge Workflow
First, view the two most recent commits.
Remember, you are currently on the feature/new-feature branch.
As you can see, the master branch is one commit behind.
Before merging, you must switch to the branch where the changes will be integrated.
Switch to the master branch and display the two most recent commits on it:
The master branch is indeed one commit behind the feature branch.
To merge them, use the git merge command and specify the branch to merge into the current branch.
For example, to merge the feature/new-feature branch into master, run:
The message displayed after running the command contains the following: Fast-forward, which specifies that the fast-forward merge was performed.
You'll explore the two types of merges later in this chapter. Before that, view the two most recent commits:
You are still on the master branch, but it is now up to date with the feature branch, with both branches pointing to the same commit.
The HEAD still points to the master branch because you were on this branch when performing the merge.
Merge Types
There are two types of merge operations in Git:
- Fast-forward merge;
- Three-way merge.
Fast-forward merge
The merge you performed is a fast-forward merge, as indicated in the message. This type occurs when all commits from the currently checked-out branch are also present in the branch being merged, meaning their commit histories do not diverge.
Refer to the following illustration for clarification:
With this type of merge the pointer of the checked out branch is simply updated to the latest commit.
Three-way merge
However, if the history of the branches has diverged meaning that after a certain common commit new commits were made separately on each branch, then a three-way merge is performed.
The term "three-way" refers to the three versions involved in the merge:
- the version of the most recent common ancestor (commit);
- the version of the latest commit in the current branch (HEAD);
- the version of the latest commit in the branch being merged.
Take a look at the following illustration of this type of merge:
Here is how a three-way merge works:
- Git identifies the changes made on each of the branches after the most recent common ancestor;
- If each branch has changes in different files or different parts of the same file, Git will automatically merge the changes by combining them in the resulting commit;
- Otherwise a merge conflict will occur.
Thanks for your feedback!