| # | Section |
|---|---|
| 1 | Bringing two branches together: the need |
| 2 | The merge |
| 3 | The rebase |
| 4 | Resolve conflicts |
| 5 | Merge or rebase: which to choose? |
| 6 | Quiz — Merge and rebase |
| 7 | Practice — Resolve a merge conflict |
| 8 | Summary |
You have worked on a feature branch, the code is ready. You now need to reintegrate it into main. Git offers two mechanisms for that: the merge and the rebase.
Both reach the same goal — reunite the work — but with a different history shape. That is the whole point of this lesson.
| Mechanism | Idea in one sentence |
|---|---|
| Merge | You create a commit that joins the two branches, keeping their history as it is. |
| Rebase | You replay the commits of one branch on top of another, as if you had started later. |
Before any integration, you make sure you have the latest version:
git switch main
git pull origin mainThe git merge command reunites one branch into another. There are two cases.
Case 1 — Fast-forward merge: if main has not moved since the branch was created, Git simply advances the pointer. No merge commit is created.
Case 2 — Three-way merge: if main has received new commits in the meantime, Git creates a merge commit that has two parents.
# Switch to the target branch, then merge
git switch main
git merge feature/panier
# Force a merge commit even if a fast-forward is possible
git merge --no-ff feature/panier| Option | Effect |
|---|---|
git merge feature | Merge (fast-forward if possible) |
git merge --no-ff feature | Always create a merge commit (traces the branch) |
git merge --abort | Cancel a merge that is stuck on a conflict |
Merge preserves the real history: you see when and how the branches came together. It is honest, but the history can become bushy with many merge commits.
🔧 Mini-exercise — You want to merge feature/panier into main while forcing the creation of a merge commit, even if a fast-forward would be possible. Write the command.
git merge --no-ff feature/panier — --no-ff keeps an explicit trace of the merged branch.
git rebase moves the commits of your branch to replay them on top of the tip of another branch. Result: a linear history, as if you had started your work after the latest commits of main.
Before the rebase:
After git rebase main (commits B and C are replayed after D):
# On the feature branch, replay on top of main
git switch feature/panier
git rebase main
# Then a clean fast-forward merge into main
git switch main
git merge feature/panier| Advantage of rebase | Precaution |
|---|---|
| Linear and readable history | Rewrites the commits (new SHAs) |
| No parasitic merge commits | Never rebase a branch already pushed and shared |
| Ideal before a pull request | Conflicts to settle commit by commit |
Golden rule of rebase: never rebase commits already published and used by others. You rewrite history, which would break their repositories. Rebase is for your unshared local work.
🔧 Mini-exercise — You are on your feature/panier branch. Write the command to replay your commits on top of the tip of main (linear history).
git rebase main (while you are on feature/panier).
A conflict occurs when Git cannot decide automatically: two branches have modified the same line of the same file. Git stops and asks you to decide.
Git inserts conflict markers into the file:
<<<<<<< HEAD
prix = 10 # main version
=======
prix = 12 # feature version
>>>>>>> feature/panierResolution step by step:
<<<<<<<, =======, >>>>>>> markers.git add.# See the files in conflict
git status
# After manual editing
git add fichier-en-conflit.py
# Finish a merge
git commit
# Finish a rebase
git rebase --continue
# In case of panic: cancel everything
git merge --abort # or: git rebase --abort| Helper | Usage |
|---|---|
git status | List the files in conflict |
git diff | See the conflicting differences |
git mergetool | Launch a graphical resolution tool |
| VS Code | Accept Current / Incoming / Both buttons |
A conflict is not an error: Git is honestly telling you “I do not know which one to keep, it is your decision”. Staying calm and reading the markers is enough in 99 % of cases.
🔧 Mini-exercise — You have edited the file app.py to resolve a conflict that occurred during a rebase. Which two commands finish the operation?
git add app.py
git rebase --continue(For a merge, it would be git add app.py then git commit.)
Both reunite the work, but they produce a different history. The choice is often a team convention.
| Aspect | Merge | Rebase |
|---|---|---|
| History | Faithful, branched | Linear, clean |
| Merge commit | Yes (if not fast-forward) | No |
| Rewrites history | No | Yes (new SHAs) |
| Safe on a shared branch | Yes | No |
Readability of log | Denser | Clearer |
The most common recommended practice:
main before opening a pull request → clean history.main → a clear trace of the integration.# 1. Clean your local branch before the PR
git switch feature/x
git rebase main
# 2. Once the PR is approved, GitHub does the mergeMnemonic: “Rebase in private, merge in public.” You rebase what is yours, you merge what is shared.
Question 1: What does git merge feature from main do when main also has new commits?
a) It deletes the feature branch
b) It creates a merge commit with two parents
c) It erases the history
d) It always refuses to merge
Answer: b) — This is a three-way merge: Git creates a merge commit that reunites the two histories.
Question 2: What is the main effect of git rebase main?
a) Replay the branch commits on top of main for a linear history
b) Clone the repository
c) Delete main
d) Create a remote backup
Answer: a) — Rebase moves and replays your commits on top of main, producing a linear history.
Question 3: Why must you not rebase a branch already pushed and shared?
a) Because GitHub forbids it
b) Because it rewrites the commits and breaks other people's repositories
c) Because rebase is slower
d) Because it erases main
Answer: b) — Rebase creates new SHAs. If others already have those commits, their history becomes inconsistent.
Question 4: What do the <<<<<<<, =======, >>>>>>> markers represent?
a) Code comments
b) A Python syntax error
c) The zones of a conflict to resolve by hand
d) The end of a file
Answer: c) — These are conflict markers: above, the current version (HEAD); below, the incoming version. You choose and you delete them.
Question 5: How do you cleanly cancel a merge blocked by conflicts?
a) git delete
b) git merge --abort
c) git reset --cloud
d) Delete the .git folder
Answer: b) — git merge --abort (or git rebase --abort) returns the repository to its state before the operation.
Two branches modify the same line of a file config.txt. Reproduce the conflict, then resolve it by keeping both pieces of information combined.
main, the file contains port = 8080.feature/ssl branch changes this line to port = 443.main changes the same line to port = 9090.feature/ssl into main, resolve the conflict by keeping port = 443 (HTTPS), then finish.# Preparation: create the conflict
echo "port = 8080" > config.txt
git add config.txt
git commit -m "Initial config"
git switch -c feature/ssl
echo "port = 443" > config.txt
git commit -am "Switch to HTTPS (port 443)"
git switch main
echo "port = 9090" > config.txt
git commit -am "Change port to 9090"
# Merge attempt → conflict
git merge feature/sslGit then displays:
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.The file config.txt contains:
<<<<<<< HEAD
port = 9090
=======
port = 443
>>>>>>> feature/sslYou edit to keep only the right value:
echo "port = 443" > config.txt # you decide in favor of HTTPS
git add config.txt
git commit -m "Merge feature/ssl: keep port 443"Expected result:
| Check | Command | Output |
|---|---|---|
| Conflict resolved | git status | “nothing to commit, working tree clean” |
| Final content | cat config.txt | port = 443 |
| Merge commit | git log --oneline -1 | “Merge feature/ssl: keep port 443” |
Tip: if you make a mistake in the middle of a conflict,
git merge --abortreturns your repository intact. No risk in experimenting.
add, you finish.main.You know how to integrate the code technically. Lesson 03 — Pull requests shows how to have this work reviewed before integrating it, at the heart of GitHub collaboration.
All rights reserved. Any reproduction, distribution, use or adaptation of this course, in whole or in part, is strictly prohibited without the prior written authorization of Dr. Haythem REHOUMA.
Course created by Dr. Haythem REHOUMA — Development and Deployment of Data Solutions