Conteúdo do Curso
Git Essentials
Git Essentials
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
Let’s first take a look at the two latest commits. Remember, we are currently on the feature/new-feature
branch.
As you can see, our master
branch is one commit behind, however, before merging changes, we have to switch to the branch where we want to incorporate the changes. Let’s switch to the master
branch and display the two latest commits on this branch:
The master
branch is indeed one commit behind the feature branch, so let's merge them. To initiate the merge process, we should use the git merge
command and specify the branch we want to merge into the current branch. For example, to merge a feature branch named feature/new-feature
into the master
branch, we should run the following command:
Note
The message displayed after running the command contains the following:
Fast-forward
, which specifies that the fast-forward merge was performed.
We’ll discuss the two types of merges later in this chapter, however before we do that, let’s take a look at the two latest commits:
We are still on the master
branch, however, this branch is now up-to-date with our feature branch with both branches pointing to the same commit.
Note
The HEAD still points to the
master
branch since we were on this branch when performing the merge.
Merge Types
As a matter of fact, there are two types of merge operation in Git:
- fast-forward merge;
- three-way merge.
Fast-forward merge
The one we have performed is an example of a fast-forward merge as mentioned in the message. This type of merge is performed if all the commits of a branch we are currently at (checked out branch) are also part of the merging branch meaning that the commit history of these branches does not diverge. Let’s take a look at the following illustration to clarify this:
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.
Let’s take a look at the following illustration of such a 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.
We'll discuss merge conflicts in the following chapter.
Obrigado pelo seu feedback!