In this example, we have a repository with a main branch main containing a base file fichier1.txt and a first initial commit. From main, we create two branches, branche1 and branche2, each intended to test rebase and merge respectively. Then two extra working branches are created, branche-rebase from branche1 and branche-merge from branche2, to add modifications there.
The project starts with a repository containing a single commit on main, corresponding to the creation of the file fichier1.txt:
(main)
A # Initial commit with fichier1.txtIn commit A, we added the file fichier1.txt with initial content.
Then two new branches are created from main:
branche1: intended to host branche-rebase to test git rebase.branche2: intended to host branche-merge to test git merge.In each working branch (branche-rebase and branche-merge), we make three distinct commits. Here are the commits in detail for each branch:
(main)
A
(branche-rebase)
\
D---E---F # Commits added on branche-rebase
(branche-merge)
\
G---H---I # Commits added on branche-mergeCommits on branche-rebase:
fichier-2-rebase.txt with the initial content "Modification 1 dans branche-rebase".fichier-2-rebase.txt.fichier-2-rebase.txt.Commits on branche-merge:
fichier-2-merge.txt with the initial content "Modification 1 dans branche-merge".fichier-2-merge.txt.fichier-2-merge.txt.These commits add identical content, but in different files (fichier-2-rebase.txt and fichier-2-merge.txt), to allow a clear comparison between merge and rebase.
branche-merge into branche2 with git mergeTo integrate the modifications of branche-merge into branche2, we use the git merge command on branche2. This operation creates a merge commit that keeps a complete history and includes the working-branch commits without reordering them.
The history after the merge:
(branche2)
A-------J # J is the merge commit
\ |
\ |
G---H---I # branche-merge keeps its distinct historybranche2 with the modifications of branche-merge (G, H, I) without moving them or changing their order.git merge, the history shows a "junction point" that visually indicates where branche-merge was merged into branche2. This keeps a complete and distinct history for each branch, which is often useful in collaborative projects.branche-rebase into branche1 with git rebaseTo integrate the modifications of branche-rebase into branche1, we use the git rebase command. Unlike merge, rebase repositions the commits (D, E, F) of branche-rebase as if they had been created after the last commit of branche1 (A).
The history after the rebase:
(branche1)
A---D'---E'---F' # Commits of branche-rebase reapplied at the end of branche1branche-rebase applied linearly after A on branche1.git rebase, the commits of branche-rebase are reorganized to form a linear history without a "junction point", as if they had been added directly after the initial commit A. This simplifies the history by making it cleaner, but it can hide the path of the original working branches.The differences observed in the Git logs make it easier to understand the specific uses of merge and rebase.
git merge:
git rebase:
| Command | History | Ideal use case |
|---|---|---|
git merge | Complete history with a junction point | Collaborative projects with several branches |
git rebase | Linear history without junction points | Cleaning up the history before a shared merge |
In summary:
git merge -> Complete history with a merge commit
git rebase -> Linear history without a merge commitThis guide lets you understand and visualize the differences between git merge and git rebase, and choose the method best suited to your project context.