Merging back into main

Last updated on 2026-01-26 | Edit this page

Overview

Questions

  • What does it mean to merge branches?
  • What does it mean to merge branches?
  • How do we merge changes locally?

Objectives

  • Explain what merging means in Git.
  • Merge a branch into main using a merge request.
  • Update a local repository after a merge.

We have learned how to create a branch and commit changes to it. At this stage, we have two versions of the repository:

  • main, which has not changed since the branch was created
  • add-cooking-difficulty, which contains new commits

Once the changes in a branch are ready, we want to integrate them back into main. This makes the changes available to collaborators and allows further development.

Creating a merge request


To integrate changes on a remote repository, we open merge request. Think of this as submitting a new recipe to your team for review before it becomes part of the official cookbook.

Callout

The name merge requests is specific to GitLab. If we were using GitHub, then this would be called a pull request. Different forges use different names, but the functionality is the same.

Why use a merge request?

  • It allows collaborators to review and discuss changes,
  • It helps maintain consistency and code quality,
  • It can run automated checks before merging.

We will not cover automated tests in this workshop. However, you may encounter them in other projects. These are scripts that run automatically for each merge request to verify that new changes do not break existing functionality (for example, running pytest for Python code).

First, we push our changes to the remote repository:

BASH

$ git push origin add-cooking-difficulty

OUTPUT

Énumération des objets: 11, fait.
Décompte des objets: 100% (10/10), fait.
Compression par delta en utilisant jusqu'à 8 fils d'exécution
Compression des objets: 100% (4/4), fait.
Écriture des objets: 100% (6/6), 1.23 Kio | 1.23 Mio/s, fait.
Total 6 (delta 0), réutilisés 0 (delta 0), réutilisés du paquet 0 (depuis 0)
remote:
remote: To create a merge request for add-cooking-difficulty, visit:
remote:   https://git.unistra.fr/manon.marchand/recipes/-/merge_requests/new?merge_request%5Bsource_branch%5D=add-cooking-difficulty
remote:
To git.unistra.fr:manon.marchand/recipes.git
 * [new branch]      add-cooking-difficulty -> add-cooking-difficulty

GitLab provides a link that opens a new merge request directly. You can also create one manually:

  1. In the left menu, go to Merge Requests > New Merge Request,
  2. Select:
    • source branch: add-cooking-difficulty,
    • target branch: main
  3. Select Compare branches and continue
  4. Provide a title
  5. In the description, explain:
    • why the change is useful
    • anything that can be useful for your team to understand the changes
  6. Inspect the changes shown at the bottom of the page
  7. When ready, select Create merge request

Reviewing a Merge Request


Once the merge request is created, collaborators can review the changes and leave comments.

Challenge

Challenge

You received comments and suggestions on your merge request. How do you apply the requested changes?

You can continue working locally on the add-cooking-difficulty branch. Edit the files, commit the changes, and push again. The new commits will automatically appear in the existing merge request.

Callout

What if main changes while you are working on a branch?

---
config:
  gitGraph:
    showCommitLabel: false
---
    gitGraph
        accDescr {A Git graph showing edits to the main branch while we were editing the other one.}
        commit
        commit
        branch add-cooking-difficulty
        commit
        commit
        checkout main
        commit
        commit

If there are no conflicts, GitLab won’t complain and the merge can go on.

If conflicts occur, we should rebase our branch so that it starts from the latest commit on main:

BASH

$ git switch main
$ git pull
$ git switch add-cooking-difficulty
$ git rebase main

This reapplies your commits on top of the updated main branch. If conflicts arise, resolve them, add the files to the staging area, and continue the rebase:

BASH

$ git rebase --continue

Repeat this process until the rebase ends. The history now looks like this:

---
config:
  gitGraph:
    showCommitLabel: false
---
    gitGraph
        accDescr {A Git graph showing edits to the main branch while we were editing the other one.}
        commit
        commit
        commit
        commit
        branch add-cooking-difficulty
        commit
        commit

After rebasing, the branch can be merged without conflicts!

Merging and updating locally


Once everyone agrees to merge the branch, we can click the Merge button.

The project history now includes:

  • the commits from add-cooking-difficulty
  • a merge commit created by GitLab

To update your local repository:

BASH

$ git switch main
$ git pull

OUTPUT

remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Dépaquetage des objets: 100% (1/1), 281 octets | 281.00 Kio/s, fait.
Depuis git.unistra.fr:alfredo.languini/recipes
   0a508b4..1384050  main       -> origin/main
Mise à jour 0a508b4..1384050
Fast-forward
 cakes/brownie.md     | 3 +++
 cakes/carrot_cake.md | 3 +++
 guacamole.md         | 2 ++
 3 files changed, 8 insertions(+)

The local repository now reflects the merged changes:

---
config:
  gitGraph:
    showCommitLabel: false
---
    gitGraph
        accDescr {A Git graph showing the merge into main.}
        commit
        commit
        commit
        commit
        branch add-cooking-difficulty
        commit
        commit
        checkout main
        merge add-cooking-difficulty

The contributing cycle can now start again. We can create a new branch with the next change set!

Callout

What if I’m not using a remote repository?

You can still use branches to organize and test your work. The merges happen locally with the git merge command.

Discussion

Challenge

  • Create a new branch to indicate allergens in the recipes
  • Commit your changes
  • Open a merge request
  • Review your work
  • Merge the branch
  • Pull it locally
Key Points
  • Changes are integrated into main using merges.
  • Merges can be performed remotely using merge requests or locally using git merge.