Practical Work No. 1: Difference between git merge and git rebase

3 min

Learning objectives:

  • Understand the fundamental differences between git merge and git rebase.
  • Observe the effects of both commands on the Git history.
  • Document the operations with screenshots and clear explanations.

Lab objectives

At the end of this lab, you will be able to:

  1. Use git merge and git rebase to integrate branch changes.
  2. Analyze the differences between the two commands in the history structure.
  3. Identify the ideal use cases for merge and rebase.

Instructions and detailed steps

Preparation

  1. Create a working folder for this lab:

    • In your terminal, type the following commands to prepare your environment:
      bash
      mkdir git-evaluation-1
      cd git-evaluation-1
      git init
  2. Screenshot: Take a screenshot showing the repository initialization (git init).


Step 1: Initialize the repository and create the main branch

  1. Create the initial file Create a file named fichier1.txt (original file) and add a base commit on the main branch main:

    bash
    echo "Contenu initial dans fichier1" > fichier1.txt
    git add fichier1.txt
    git commit -m "Initialize the project with fichier1.txt"
  2. Rename the main branch to main (if needed):

    bash
    git branch -M main
  3. Screenshot: Show the file addition and the first commit.


Step 2: Create the branches for rebase and merge

  1. Create the branche1 and branche2 branches from main:

    • Create branche1:
      bash
      git checkout -b branche1
    • Go back to main and create branche2:
      bash
      git checkout main
      git checkout -b branche2
  2. Create the working branches:

    • Create branche-rebase from branche1:
      bash
      git checkout branche1
      git checkout -b branche-rebase
    • Create branche-merge from branche2:
      bash
      git checkout branche2
      git checkout -b branche-merge
  3. Screenshot: Show the branch creation with the git branch command.


Step 3: Add identical commits on branche-rebase and branche-merge

Note: We will use different files (fichier-2-rebase.txt and fichier-2-merge.txt) and perform the same modifications so we can compare the effects of merge and rebase.

  1. Work on branche-rebase:

    • First commit:

      bash
      git checkout branche-rebase
      echo "Modification 1 dans branche-rebase" > fichier-2-rebase.txt
      git add fichier-2-rebase.txt
      git commit -m "Add fichier-2-rebase.txt with modification 1"
    • Second commit:

      bash
      echo "Modification 2 dans branche-rebase" >> fichier-2-rebase.txt
      git add fichier-2-rebase.txt
      git commit -m "Add modification 2 in fichier-2-rebase.txt"
    • Third commit:

      bash
      echo "Modification 3 dans branche-rebase" >> fichier-2-rebase.txt
      git add fichier-2-rebase.txt
      git commit -m "Add modification 3 in fichier-2-rebase.txt"
  2. Work on branche-merge:

    • First commit:

      bash
      git checkout branche-merge
      echo "Modification 1 dans branche-merge" > fichier-2-merge.txt
      git add fichier-2-merge.txt
      git commit -m "Add fichier-2-merge.txt with modification 1"
    • Second commit:

      bash
      echo "Modification 2 dans branche-merge" >> fichier-2-merge.txt
      git add fichier-2-merge.txt
      git commit -m "Add modification 2 in fichier-2-merge.txt"
    • Third commit:

      bash
      echo "Modification 3 dans branche-merge" >> fichier-2-merge.txt
      git add fichier-2-merge.txt
      git commit -m "Add modification 3 in fichier-2-merge.txt"
  3. Screenshot: Show the commits made in each branch with git log.


Step 4: Integrate the branches with merge and rebase

  1. Rebase of branche-rebase into branche1:

    • Switch to branche1:

      bash
      git checkout branche1
    • Rebase branche-rebase into branche1:

      bash
      git rebase branche-rebase
    • Screenshot: Use git log --oneline --graph --all to show the linear history created by the rebase.

  2. Merge of branche-merge into branche2:

    • Switch to branche2:

      bash
      git checkout branche2
    • Merge branche-merge into branche2:

      bash
      git merge branche-merge -m "Merge branche-merge into branche2"
    • Screenshot: Use git log --oneline --graph --all to show the history with the merge commit.


Step 5: Final comparison and reflection questions

Reflection questions

  1. Compare the histories:

    • What do you notice in the difference between the history of branche1 (with rebase) and that of branche2 (with merge)?
    • Take a screenshot of each history and explain the differences observed.
  2. Arguments for each method:

    • In which cases would it be preferable to use rebase to integrate changes?
    • In which cases is merge more advantageous?
  3. Application scenarios:

    • Imagine a team project. Explain a concrete case where merge could cause conflicts in the history, and how rebase could solve this problem.
    • Conversely, describe a case where excessive use of rebase could make tracking difficult in a collaborative project.

Summary and submission

To submit this lab:

  1. Compile your answers and screenshots in a document (PDF or Word).
  2. Answer the reflection questions at the bottom of the document.
  3. Submit your file on the course platform.