Chapter 53: Git Exercises
Git Exercises
You already know a lot of theory, commands, concepts, workflows, safety nets, advanced tricks… But knowing is not the same as being able to do it fluently under pressure.
Git is a muscle memory skill — like riding a bicycle or touch-typing. You only truly learn it when you repeat the same motions 50–100 times in slightly different situations until they become automatic.
So today we are going to do structured, progressive, realistic Git exercises — like a real bootcamp class.
I will give you 10 carefully sequenced exercises that cover 95% of daily Git usage + the most common “oh no” moments.
Do them in order. Try each one without looking back at previous explanations first — force your brain to remember. Only check if you get stuck.
All exercises can be done in one single folder — no need to create new repos every time.
Preparation (do this once)
|
0 1 2 3 4 5 6 7 8 9 10 11 |
mkdir git-exercises-practice cd git-exercises-practice git init # Optional: nice log alias if you haven't set it yet git config --global alias.lg "log --oneline --graph --decorate --all" |
Now — let’s start.
Exercise 1 – Basic commit cycle (foundation)
Goal: Muscle memory for everyday add → commit → status → log
- Create file notes.txt with content:
text01234567Things to learn:- Git basics
- git status — see it red (untracked)
- Stage it → git add notes.txt
- git status — see it green
- Commit → git commit -m “Add initial learning notes”
- git lg or git log –oneline — see your first commit
- Add one more line to notes.txt:
text0123456- Branching & merging
- Repeat: status → add → commit → log
Goal achieved when: You can do this flow without thinking — 10 seconds max.
Exercise 2 – Oops – forgot to add a file (amend)
-
Create todo.txt with one line: “Buy milk”
-
Commit: git add todo.txt && git commit -m “Add todo list”
-
Realize you forgot “Call mom” — add it to todo.txt
-
Stage the change → git add todo.txt
-
Amend the previous commit:
Bash01234567git commit --amend -m "Add todo list with two items"# or just git commit --amend --no-edit (keep old message) -
git lg — see only one commit, but updated content
Goal: Understand amend rewrites last commit (new hash, same parent)
Exercise 3 – Create branch, work, merge back (GitHub Flow style)
- Create branch: git switch -c feature/add-login
- Edit notes.txt — add line “- Login system”
- Commit 2–3 times with different small messages
- Switch back: git switch main
- Merge: git merge feature/add-login
- Delete branch: git branch -d feature/add-login
- git lg — see fast-forward merge (clean line)
Goal: Feel safe parallel work + merge
Exercise 4 – Merge conflict (the emotional one)
-
On main: edit line 3 of notes.txt to “- Branching is easy”
-
Commit: git commit -am “Update branching note”
-
Switch to new branch: git switch -c feature/improve-notes
-
Edit the same line differently: “- Master branching & merging”
-
Commit on feature branch
-
Switch to main → merge feature:
Bash0123456git merge feature/improve-notes
→ Conflict!
-
Open notes.txt — see markers:
text012345678910<<<<<<< HEAD- Branching is easy=======- Master branching & merging>>>>>>> feature/improve-notes -
Resolve: keep both or choose one → remove markers
-
git add notes.txt
-
git commit (auto message)
Goal: No longer panic when you see conflict markers — treat them as “Git needs your decision”
Exercise 5 – Reset & reflog recovery (the safety net)
-
Make 3 small commits on main
-
Do dangerous reset:
Bash0123456git reset --hard HEAD~2
→ Last commit disappeared!
-
Panic? → git reflog
-
Find the lost commit hash (second line usually)
-
Recover:
Bash0123456git reset --hard <lost-hash>
Goal: Trust that almost nothing is permanently lost if you act fast
Exercise 6 – Interactive rebase (clean history)
-
Create branch with 5 messy commits (small changes + bad messages)
-
git rebase -i HEAD~5
-
In editor:
- Change some pick to squash / fixup / reword / drop
- Save & exit
-
Edit combined message if asked
-
git lg — clean, nice history
Goal: Feel comfortable rewriting your own private history
Exercise 7 – Rebase vs Merge (visual difference)
- Create branch A → 2 commits
- Update main → 1 commit
- On branch A: git rebase main → linear
- Create branch B → 2 commits
- On branch B: git merge main → merge bubble
- git lg –all — compare graphs
Goal: Understand why teams prefer rebase for personal branches + merge for shared
Exercise 8 – Stash emergency
- Make messy uncommitted changes on main
- Boss says: “quick fix on main!”
- git stash push -m “WIP dark mode”
- Fix something → commit → push
- git stash pop
Goal: Feel safe context switching
Exercise 9 – Cherry-pick one good commit
- Create branch with 3 commits — middle one is perfect
- Switch to main
- git cherry-pick <middle-hash>
Goal: Transplant only one change
Exercise 10 – .gitignore + .gitattributes practice
- Create .env, node_modules/, image.png, script.sh
- Create .gitignore → ignore .env, node_modules/
- Create .gitattributes → *.sh text eol=lf*.png binary
- git status — see what is ignored vs tracked
Goal: Automatic quality/safety
Final Advice (from your teacher)
Do these 10 exercises at least twice — once today, once tomorrow. Time yourself — aim for under 2 minutes per exercise.
After that:
- Clone any open-source repo → make tiny fix → send PR
- Break things on purpose → recover with reflog/reset
- Set up Husky pre-commit hook
- Use GitHub Desktop / GitKraken for visual help
You now have everything — from git init to advanced recovery, signing, LFS, hooks, CI/CD.
You’re ready to work in any team, contribute to open-source, pass any Git interview, and teach others.
Any final questions? Want me to give you a “Git final exam” with 10 random scenarios to solve?
Or shall we celebrate — you did it! 🎉
You’ve earned it. Very proud of you! 🚀
