Skip to main content

Git Practical Exercises


Table of contents


  1. Exercise 1: Complete workflow
  2. Exercise 2: Branch management
  3. Exercise 3: Conflict resolution
  4. Exercise 4: Interactive rebase
  5. Exercise 5: Realistic scenario
  6. Solutions


1 - Exercise 1: Complete workflow



Goal

Create a project, push it to GitHub, and make several commits.

Instructions

  1. Create a new folder git-exercice-1
  2. Initialize Git
  3. Create the following files:
    • index.html with a basic HTML structure
    • style.css with a few styles
    • README.md with a project description
  4. Make 3 separate commits (one for each file)
  5. Create a repository on GitHub
  6. Push your code

Verification

# Vous devez voir 3 commits
git log --oneline
# abc1234 docs: add README
# def5678 style: add CSS
# ghi9012 feat: add HTML structure
💡 Hints
mkdir git-exercice-1
cd git-exercice-1
git init
# Créer les fichiers...
git add index.html
git commit -m "feat: add HTML structure"
# etc.

🔝 Back to table of contents



2 - Exercise 2: Branch management



Goal

Work with several branches and merge them.

Instructions

  1. Start from the project in exercise 1
  2. Create a branch feature/contact-page
  3. On this branch, create contact.html
  4. Make a commit
  5. Return to main
  6. Create a branch feature/about-page
  7. On this branch, create about.html
  8. Make a commit
  9. Return to main
  10. Merge both branches into main
  11. Delete the feature branches

Verification

git log --oneline --graph --all
# Doit montrer les merges ou un historique linéaire

Bonus questions

  • What is the difference if you use --no-ff for the merge?
  • How do you do a squash merge?
💡 Hints
git checkout -b feature/contact-page
# Créer contact.html
git add contact.html
git commit -m "feat: add contact page"
git checkout main
# Répéter pour about
git merge feature/contact-page
git merge feature/about-page
git branch -d feature/contact-page
git branch -d feature/about-page

🔝 Back to table of contents



3 - Exercise 3: Conflict resolution



Goal

Create and resolve a merge conflict.

Setup

mkdir git-exercice-3
cd git-exercice-3
git init

# Fichier initial
echo "Ligne 1" > file.txt
echo "Ligne 2" >> file.txt
echo "Ligne 3" >> file.txt
git add file.txt
git commit -m "Initial commit"

Instructions

  1. Create a branch feature/modification-a
  2. Modify line 2 of file.txt: "Ligne 2 - Version A"
  3. Commit
  4. Return to main
  5. Create a branch feature/modification-b
  6. Modify line 2 of file.txt: "Ligne 2 - Version B"
  7. Commit
  8. Return to main
  9. Merge feature/modification-a
  10. Merge feature/modification-b (conflict!)
  11. Resolve the conflict by keeping both versions

Expected result

file.txt after resolution:

Ligne 1
Ligne 2 - Version A et Version B
Ligne 3
💡 Hints

To resolve the conflict:

  1. Open the file
  2. You will see <<<<<<, =======, >>>>>>>
  3. Edit it to keep what you want
  4. git add file.txt
  5. git commit

🔝 Back to table of contents



4 - Exercise 4: Interactive rebase



Goal

Clean up the history with interactive rebase.

Setup

mkdir git-exercice-4
cd git-exercice-4
git init

echo "v1" > file.txt && git add . && git commit -m "Initial"
echo "v2" >> file.txt && git commit -am "wip"
echo "v3" >> file.txt && git commit -am "more wip"
echo "v4" >> file.txt && git commit -am "almost done"
echo "v5" >> file.txt && git commit -am "fix typo"
echo "v6" >> file.txt && git commit -am "final"

Instructions

Use git rebase -i HEAD~5 to:

  1. Squash "wip", "more wip", "almost done" into a single commit "feat: implement feature"
  2. Squash "fix typo" with "final" into "feat: finalize feature"

Expected result

git log --oneline
# abc1234 feat: finalize feature
# def5678 feat: implement feature
# ghi9012 Initial
💡 Hints

In the interactive rebase editor:

pick ghi9012 wip
squash xxx more wip
squash xxx almost done
pick xxx fix typo
squash xxx final

Then edit the messages.

🔝 Back to table of contents



5 - Exercise 5: Realistic scenario



Context

You are working on a website with a team. Simulate the complete workflow.

Instructions

Part A: Setup

  1. Create a repository website-team
  2. Create a project structure:
website-team/
├── index.html
├── css/
│ └── style.css
├── js/
│ └── app.js
└── README.md
  1. Commit and push to GitHub

Part B: Feature development

  1. Create a branch feature/navbar
  2. Add a navbar in index.html
  3. Add the corresponding styles
  4. Make 2-3 commits
  5. Push the branch

Part C: Hotfix

Meanwhile, an urgent bug:

  1. From main, create hotfix/critical-bug
  2. "Fix" the bug (modify a file)
  3. Merge into main
  4. Push

Part D: Update and finalization

  1. Return to feature/navbar
  2. Rebase onto the new main
  3. Resolve any conflicts
  4. Squash your commits into a single one
  5. Merge into main with --no-ff
  6. Create a tag v1.0.0
  7. Push everything

Final verification

git log --oneline --graph --all

Should show:

  • The merged hotfix
  • The feature merged with a merge commit
  • The v1.0.0 tag

🔝 Back to table of contents



6 - Solutions



Exercise 1 Solution

mkdir git-exercice-1
cd git-exercice-1
git init

cat > index.html << 'EOF'
<!DOCTYPE html>
<html>
<head><title>Mon Site</title></head>
<body><h1>Hello</h1></body>
</html>
EOF
git add index.html
git commit -m "feat: add HTML structure"

cat > style.css << 'EOF'
body { font-family: Arial; }
h1 { color: blue; }
EOF
git add style.css
git commit -m "style: add CSS"

echo "# Mon Projet" > README.md
git add README.md
git commit -m "docs: add README"

# Sur GitHub, créez le repo puis :
git remote add origin [email protected]:user/git-exercice-1.git
git push -u origin main

Exercise 3 Solution

# Après le conflit
# file.txt contient :
<<<<<<< HEAD
Ligne 2 - Version A
=======
Ligne 2 - Version B
>>>>>>> feature/modification-b

# Éditer pour :
Ligne 2 - Version A et Version B

git add file.txt
git commit -m "merge: combine modifications A and B"

Exercise 4 Solution

In the interactive rebase, set up:

pick abc Initial
pick def wip
squash xxx more wip
squash xxx almost done
reword xxx fix typo
squash xxx final

Key takeaways

ConceptKey command
Basic workflowadd → commit → push
Branchescheckout -b, merge, branch -d
ConflictsEdit, add, commit
Cleanuprebase -i, squash
Versioningtag -a v1.0.0

🎉 Congratulations!

You have completed the Git course! You now know:

  • ✅ The basic commands
  • ✅ The Git workflow
  • ✅ Branches and merging
  • ✅ Rebase and squash
  • ✅ Conflict resolution
  • ✅ Synchronization with GitHub
  • ✅ Tags and versions

Next steps

  1. Practice daily
  2. Contribute to open source projects
  3. Explore advanced features (hooks, submodules, etc.)

🔝 Back to table of contents