| # | Section |
|---|---|
| 1 | Why branches? |
| 2 | Create and switch branches |
| 3 | Merge a branch |
| 4 | Handle a merge conflict |
| 5 | Consult the history |
| 6 | Quiz — Branches and history |
| 7 | Practice — A feature on its own branch |
| 8 | Summary |
A branch is an independent line of development. It lets you work on a new feature without touching the main version (main), which stays stable.
Analogy: a shared document. Rather than editing the official version directly (risk of breaking everything), you work on a working copy (the branch). Once you are satisfied, you merge your changes into the original.
Why it is essential:
| Typical branch | Role |
|---|---|
main | Stable version, ready to ship |
feature/... | New feature in progress |
fix/... | Bug fix |
# List branches (the current one is marked with a *)
git branch
# Create a branch
git branch feature-connexion
# Switch branch
git switch feature-connexion
# Create AND switch in a single command (recommended)
git switch -c feature-connexion| Modern command | Older equivalent | Effect |
|---|---|---|
git switch name | git checkout name | Switch to a branch |
git switch -c name | git checkout -b name | Create and switch |
git branch | — | List branches |
git branch -d name | — | Delete a merged branch |
git switchis the modern command dedicated to branches.git checkoutstill works but does too many things at once — preferswitchfor branches.
🔧 Mini-exercise — Write the command that creates the feature-login branch and switches to it in a single operation.
git switch -c feature-loginTo merge is to integrate the changes of one branch into another — typically, bring your finished feature back into main.
# 1. Switch to the branch that will RECEIVE the changes
git switch main
# 2. Merge the feature branch into it
git merge feature-connexion| Merge type | When? | Result |
|---|---|---|
| Fast-forward | main has not moved since the branch | main simply advances |
| Merge commit | main has also evolved | A merge commit reunites the two histories |
# After a successful merge, you can delete the branch
git branch -d feature-connexionSimple rule: you stand on the destination branch (
main), then you merge the source branch into it. “I am on main, I pull feature in.”
🔧 Mini-exercise — You are on feature-login and the work is done. Write the two commands to merge this branch into main.
git switch main
git merge feature-loginA conflict occurs when two branches have modified the same line of the same file. Git cannot choose for you: it asks you to decide.
Git inserts markers into the conflicting file:
<<<<<<< HEAD
text from the current branch (main)
=======
text from the merged branch (feature)
>>>>>>> feature-connexionTo resolve:
<<<<<<<, =======, >>>>>>> markers.git add file then git commit to finalize the merge.A conflict is not a serious error: Git is protecting you by refusing to guess. Stay calm, read both versions, keep the right one.
🔧 Mini-exercise — Name the three markers Git inserts into a conflicting file and that you must delete after resolution.
<<<<<<<, ======= and >>>>>>>. Once the right content is chosen, delete them, then run git add and git commit.
# Full history
git log
# Compact version (one line per commit)
git log --oneline
# With the graph of branches and merges
git log --oneline --graph --all
# See the changes of a commit
git show <hash>
# See who modified each line of a file
git blame fichier.txt| Command | Answers the question |
|---|---|
git log | What is the history of the project? |
git log --oneline --graph --all | How did the branches fork? |
git show <hash> | What exactly did this commit change? |
git blame file | Who wrote this line, and when? |
Git history is a time machine. Used well (with good commit messages), it lets you understand why the code is in its current state.
🔧 Mini-exercise — Write the command that displays the history in a compact way, with the graph of branches and all branches.
git log --oneline --graph --allQuestion 1: What is a branch for?
a) Deleting the history
b) Developing in isolation without touching the main version
c) Connecting to GitHub
d) Compiling the code
Answer: b) — A branch is an independent line of development: it isolates work in progress from the stable main branch.
Question 2: Which command creates a branch AND switches to it?
a) git branch name
b) git switch -c name
c) git merge name
d) git log name
Answer: b) — git switch -c name creates the branch and positions you on it in a single command (modern equivalent of git checkout -b).
Question 3: To merge feature into main, I must first…
a) Switch to feature then run git merge main
b) Switch to main then run git merge feature
c) Delete main
d) Run git init
Answer: b) — You position yourself on the destination branch (main), then you merge the source branch (feature) into it.
Question 4: When does a merge conflict occur?
a) When two branches modify the same line of the same file
b) On every merge, systematically
c) When you create a branch
d) When you install Git
Answer: a) — Git cannot choose between two modifications of the same line: it reports a conflict to resolve by hand.
Question 5: Which command shows the history with the branch graph?
a) git status
b) git log --oneline --graph --all
c) git branch -d
d) git add .
Answer: b) — --graph --all draws the structure of branches and merges; --oneline keeps it compact.
On an existing repository, create a branch, make a commit on it, then merge it into main and consult the graph.
# 1. Start from an up-to-date main
git switch main
# 2. Create and switch to a feature branch
git switch -c feature-titre
# 3. Work and commit on the branch
echo "## Nouvelle section" >> README.md
git add README.md
git commit -m "Add a new section to the README"
# 4. Return to main and merge
git switch main
git merge feature-titre
# 5. Clean up the merged branch
git branch -d feature-titre
# 6. Visualize the history
git log --oneline --graph --allExpected result: the branch commit appears in the history of main, and git branch no longer lists feature-titre.
This cycle — create a branch, work, merge, delete — is a developer's daily workflow. You will repeat it hundreds of times.
main stays stable.git switch -c name creates and switches to a branch.main), then git merge source.git log --oneline --graph --all visualizes the history and the branches.Lesson 08 — Remote repository: share your work online and collaborate via a Git server (GitHub).
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