Chuck's Academy

Git and GitHub

Branch Merging (Merging)

In this module, we will explore the process of branch merging in Git. Merging is a critical action to combine the work of different branches into a single branch, maintaining a clear and consistent project history.

What is branch merging?

Branch merging is the process of taking changes from one branch and combining them into another branch. This is essential when different developers work on multiple features or bug fixes simultaneously and it is necessary to consolidate all changes into the main branch of the project.

Types of Merge

Fast-forward merge

Occurs when the target branch has no additional commits since the source branch was created. In this case, the target branch simply advances to the latest commit of the source branch.

bash

Non-fast-forward merge

A new merge commit is created that unites the two commit histories when the target branch has additional commits after the source branch was created.

bash

Practical example of merging

Create and merge a branch

  1. Create a new branch:

    bash
  2. Make changes and commit on the new branch:

    bash
  3. Switch to the main branch:

    bash
  4. Merge the feature branch into the main branch:

    bash

    If it is a non-fast-forward merge:

    bash
  5. Delete the merged branch if not needed:

    bash

Conflict Resolution

Sometimes, changes in different branches can conflict. Git will attempt to automatically merge the changes, but if it can't, it will ask you to resolve the conflicts manually.

How to view conflicts

If there are conflicts, Git will indicate and modify the conflicting files to include markers that highlight the differences.

bash

Ask me anything