Git Practical Exercises
Table of contents
- Exercise 1: Complete workflow
- Exercise 2: Branch management
- Exercise 3: Conflict resolution
- Exercise 4: Interactive rebase
- Exercise 5: Realistic scenario
- Solutions
1 - Exercise 1: Complete workflow
Goal
Create a project, push it to GitHub, and make several commits.
Instructions
- Create a new folder
git-exercice-1 - Initialize Git
- Create the following files:
index.htmlwith a basic HTML structurestyle.csswith a few stylesREADME.mdwith a project description
- Make 3 separate commits (one for each file)
- Create a repository on GitHub
- 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
- Start from the project in exercise 1
- Create a branch
feature/contact-page - On this branch, create
contact.html - Make a commit
- Return to
main - Create a branch
feature/about-page - On this branch, create
about.html - Make a commit
- Return to
main - Merge both branches into
main - 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-fffor 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
- Create a branch
feature/modification-a - Modify line 2 of
file.txt: "Ligne 2 - Version A" - Commit
- Return to
main - Create a branch
feature/modification-b - Modify line 2 of
file.txt: "Ligne 2 - Version B" - Commit
- Return to
main - Merge
feature/modification-a - Merge
feature/modification-b(conflict!) - 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:
- Open the file
- You will see
<<<<<<,=======,>>>>>>> - Edit it to keep what you want
git add file.txtgit 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:
- Squash "wip", "more wip", "almost done" into a single commit "feat: implement feature"
- 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
- Create a repository
website-team - Create a project structure:
website-team/
├── index.html
├── css/
│ └── style.css
├── js/
│ └── app.js
└── README.md
- Commit and push to GitHub
Part B: Feature development
- Create a branch
feature/navbar - Add a navbar in
index.html - Add the corresponding styles
- Make 2-3 commits
- Push the branch
Part C: Hotfix
Meanwhile, an urgent bug:
- From
main, createhotfix/critical-bug - "Fix" the bug (modify a file)
- Merge into
main - Push
Part D: Update and finalization
- Return to
feature/navbar - Rebase onto the new
main - Resolve any conflicts
- Squash your commits into a single one
- Merge into
mainwith--no-ff - Create a tag
v1.0.0 - 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
| Concept | Key command |
|---|---|
| Basic workflow | add → commit → push |
| Branches | checkout -b, merge, branch -d |
| Conflicts | Edit, add, commit |
| Cleanup | rebase -i, squash |
| Versioning | tag -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
- Practice daily
- Contribute to open source projects
- Explore advanced features (hooks, submodules, etc.)