tech
Chapter 5: Guided Interactive Practice
Practice Git step by step by creating a repository, making commits, branching, causing a conflict, and resolving it manually.
Chapter 5: Guided Interactive Practice
Reading about Git is only half the battle. To internalize the mental models, you must run the commands yourself. In this chapter, we will build a simulated project, make commits, intentionally cause a merge conflict, and resolve it step-by-step.
🏗️ The Setup: Your Recipe Book Project
We will build a simple command-line recipe book. Open your computer’s Terminal (or Command Prompt) and run these commands.
Phase 1: Initialize the Local Repository
First, let’s create a directory and initialize Git.
# 1. Create a practice folder and enter it
mkdir git-practice && cd git-practice
# 2. Turn this folder into a Git repository
git init
- Explanation: Git has created a hidden
.gitfolder insidegit-practice. It is now watching this folder.
Phase 2: Create Your First Commit
We will write the base recipe file in our Working Directory.
# 3. Create a recipe file with some initial content
echo "Apple Pie Recipe" > recipe.txt
echo "1. Slice 5 sweet apples." >> recipe.txt
echo "2. Mix with sugar and cinnamon." >> recipe.txt
# 4. Check the status of your project
git status
- What you see: Git tells you
recipe.txtis an Untracked file. It is sitting on “The Stage” but is not yet framed by the camera.
# 5. Move recipe.txt to the Staging Area
git add recipe.txt
# 6. Verify it is staged
git status
- What you see: Git now shows
new file: recipe.txtin green text under “Changes to be committed”. It is now in the camera viewfinder.
# 7. Save this snapshot permanently in your Local Repository
git commit -m "feat: Initialize Apple Pie recipe"
- Explanation: Git compressed the file content into a Blob, generated a Commit Object pointing to a Tree representing your folder, calculated a unique hash ID, and stored it inside the
.gitdatabase.
Phase 3: Create a Parallel Universe (Branch)
We want to experiment with adding a crust step, but we aren’t sure if we want it in our main recipe book yet.
# 8. Create and switch to a feature branch called 'flaky-crust'
git switch -c flaky-crust
- Explanation: Git created a pointer named
flaky-crustpointing to your first commit, and moved theHEADpin to it.
# 9. Add a crust instruction to recipe.txt
echo "3. Bake with a flaky butter crust." >> recipe.txt
# 10. Commit this change on our experiment branch
git commit -am "feat: Add flaky crust instruction"
- Note:
commit -amstages all modified files and commits them in one step.
Phase 4: Trigger a Conflict on Main
Now we will switch back to the main branch, modify the exact same line, and cause a conflict.
# 11. Switch back to the main branch
git switch main
- Explanation: Git moved the
HEADpin back to themainbranch pointer. If you openrecipe.txtright now, the crust step is gone! The file is exactly as it was when we committed it in Step 7.
# 12. Add a different instruction on line 3 of recipe.txt
echo "3. Bake with a graham cracker crust." >> recipe.txt
# 13. Commit this change on the main branch
git commit -am "feat: Add graham cracker crust instruction"
Now we have two different timelines:
flaky-crustbranch says Step 3 is “flaky butter crust”.mainbranch says Step 3 is “graham cracker crust”.
Let’s try to merge them:
# 14. Merge flaky-crust branch into main
git merge flaky-crust
The Output:
Auto-merging recipe.txt
CONFLICT (content): Merge conflict in recipe.txt
Automatic merge failed; fix conflicts and then commit the result.
Phase 5: Resolve the Conflict
# 15. Check status to confirm the unmerged paths
git status
- What you see:
recipe.txtis listed in red under “Unmerged paths” with the statusboth modified.
# 16. Open recipe.txt in your text editor (like VS Code or nano)
cat recipe.txt
You will see the conflict markers:
Apple Pie Recipe
1. Slice 5 sweet apples.
2. Mix with sugar and cinnamon.
<<<<<<< HEAD
3. Bake with a graham cracker crust.
=======
3. Bake with a flaky butter crust.
>>>>>>> flaky-crust
Let’s resolve the conflict. We decide that a flaky butter crust is better, but we want to note the graham cracker alternative.
# 17. Open recipe.txt in your editor, delete all conflict markers,
# and edit the text to look exactly like this:
#
# Apple Pie Recipe
# 1. Slice 5 sweet apples.
# 2. Mix with sugar and cinnamon.
# 3. Bake with a flaky butter crust (or graham cracker crust).
Save the file.
# 18. Stage the resolved file to tell Git the conflict is resolved
git add recipe.txt
# 19. Check status to make sure everything is clean and ready
git status
# 20. Complete the merge commit
git commit -m "Merge branch 'flaky-crust' and resolve crust conflict"
Phase 6: View Your History Graph
# 21. View the visual timeline representation
git log --oneline --graph --all
The Output:
* e4f2d3a (HEAD -> main) Merge branch 'flaky-crust' and resolve crust conflict
|\
| * a1b2c3d (flaky-crust) feat: Add flaky crust instruction
* | f5e6d7c feat: Add graham cracker crust instruction
|/
* 8f3a9d2 feat: Initialize Apple Pie recipe
🎉 Congratulations! You have successfully initialized a Git repository, staged files, committed snapshots, created parallel branches, triggered a merge conflict, and resolved it. You now understand Git’s core architecture and commands.