cheatsheet/git-exercise.md

2.0 KiB
Raw Permalink Blame History

Git Exercise — "Track a Poem"

Goal: Turn a folder into a Git repo, make commits, experiment with branches, and merge.

Time: ~1015 minutes


Step 1 — Set up (do once on your machine)

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Step 2 — Create a project and turn it into a Git repo

cd ~/my-project
git init

You should see: Initialized empty Git repository...

Step 3 — Create your first file and commit it

echo "Roses are red" > poem.txt
git add poem.txt
git commit -m "Add first line of poem"

Step 4 — Add a second line

echo "Violets are blue" >> poem.txt

Step 5 — See what changed

git status
git diff

git status shows the file is modified. git diff shows exactly what lines changed.

Step 6 — Commit the change

git add .
git commit -m "Add second line"

Step 7 — View the history

git log --oneline

You should see two commits.

Step 8 — Create a branch and switch to it

git checkout -b spanish

This creates a branch named spanish and switches to it.

Step 9 — Add a Spanish version on this branch

echo "Las rosas son rojas" > poem_es.txt
git add .
git commit -m "Add Spanish version"

Step 10 — Switch back to main

git checkout main

List the files — notice poem_es.txt is gone. It only exists on the spanish branch.

ls

Step 11 — Merge the branch

git merge spanish

Now poem_es.txt is back on main.

ls
git log --oneline

Step 12 — Clean up the branch (optional)

git branch -d spanish

Commands you used: git config, git init, git add, git commit, git status, git diff, git log, git checkout, git merge, git branch

What you learned:

  • Starting a Git repo
  • Staging and committing changes
  • Viewing history and differences
  • Creating and switching branches
  • Merging branches together