Introduction to Git Merge

heloisalouzada

Heloisa Louzada B. Gomes

Posted on September 20, 2024

Introduction to Git Merge

In this post, we will talk about an essential process in project development: the merge. Merge means "to combine," that is, the action of merging two lines of development in a project.

What is the "Head"?

Before explaining merge, it is important to understand the concept of the head. The head is a pointer that always points to the version of the code currently being worked on. When a merge is performed, the head moves to reflect the combined changes between the branches.

Merge Strategies

There are different merge strategies. Let's cover the main ones:

Fast-Forward: The simplest strategy. The system just moves the pointer forward without conflicts. This happens when, after creating a new branch, all commits and changes were made exclusively in it. Thus, when performing the merge, there are no divergences between the main branch (master) and the new branch.

Image description

Ort: This strategy is used when there are changes in both the main branch and the created branch, but in different files, without causing conflicts. Git detects that the files are in different stages but can merge the changes automatically without manual intervention.

Image description

Handling Conflicts
When a conflict occurs during the merge, the changes need to be handled manually. When using VSCode, for example, a new tab opens so you can intuitively choose how to resolve the conflicts and continue with the project.

Image description

Step by step
First, you need to be on the branch where you want to combine the changes. To do this, use the command:

git checkout [branch name]
Enter fullscreen mode Exit fullscreen mode

Now, you can merge the auxiliary branch with the target branch. Use the command:

git merge [branch name]
Enter fullscreen mode Exit fullscreen mode

Pratical Exemple: If you are on a branch called auxiliary_branch and you want to merge it with the master branch, execute:

git checkout master
git merge auxiliar_branch
Enter fullscreen mode Exit fullscreen mode

Now the changes from auxiliary_branch are now integrated into master. Always check that you are on the correct branch before merging to avoid unwanted conflicts.

💖 💪 🙅 🚩
heloisalouzada
Heloisa Louzada B. Gomes

Posted on September 20, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related