Branches and History

6 min

Table of contents


1 — Why branches?

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:

  • Isolation: experimental work never breaks the stable version.
  • Collaboration: each person works on their own branch, without getting in the way.
  • Organization: one branch = one feature or one fix.
Typical branchRole
mainStable version, ready to ship
feature/...New feature in progress
fix/...Bug fix

↑ Back to top


2 — Create and switch branches
bash
# 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 commandOlder equivalentEffect
git switch namegit checkout nameSwitch to a branch
git switch -c namegit checkout -b nameCreate and switch
git branchList branches
git branch -d nameDelete a merged branch

git switch is the modern command dedicated to branches. git checkout still works but does too many things at once — prefer switch for branches.

🔧 Mini-exercise — Write the command that creates the feature-login branch and switches to it in a single operation.

✅ See a solution
bash
git switch -c feature-login

↑ Back to top


3 — Merge a branch

To merge is to integrate the changes of one branch into another — typically, bring your finished feature back into main.

bash
# 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 typeWhen?Result
Fast-forwardmain has not moved since the branchmain simply advances
Merge commitmain has also evolvedA merge commit reunites the two histories
bash
# After a successful merge, you can delete the branch
git branch -d feature-connexion

Simple 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.

✅ See a solution
bash
git switch main
git merge feature-login

↑ Back to top


4 — Handle a merge conflict

A 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-connexion

To resolve:

  1. Open the file, choose the right content (or combine both).
  2. Delete the <<<<<<<, =======, >>>>>>> markers.
  3. 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.

✅ See a solution

<<<<<<<, ======= and >>>>>>>. Once the right content is chosen, delete them, then run git add and git commit.

↑ Back to top


5 — Consult the history
bash
# 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
CommandAnswers the question
git logWhat is the history of the project?
git log --oneline --graph --allHow did the branches fork?
git show <hash>What exactly did this commit change?
git blame fileWho 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.

✅ See a solution
bash
git log --oneline --graph --all

↑ Back to top


6 — Quiz — Branches and history

Question 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

See the solution

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

See the solution

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

See the solution

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

See the solution

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 .

See the solution

Answer: b)--graph --all draws the structure of branches and merges; --oneline keeps it compact.

↑ Back to top


7 — Practice — A feature on its own branch

Instructions

On an existing repository, create a branch, make a commit on it, then merge it into main and consult the graph.


Correction — Expected command sequence

bash
# 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 --all

Expected 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.

↑ Back to top


8 — Summary

Key takeaways

  1. A branch = an isolated line of development; main stays stable.
  2. git switch -c name creates and switches to a branch.
  3. Merge: stand on the destination (main), then git merge source.
  4. A conflict occurs on the same lines modified on both sides; you resolve it by hand.
  5. git log --oneline --graph --all visualizes the history and the branches.

What's next

Lesson 08 — Remote repository: share your work online and collaborate via a Git server (GitHub).

↑ Back to top


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