120 lines
2.0 KiB
Markdown
120 lines
2.0 KiB
Markdown
# Git Exercise — "Track a Poem"
|
||
|
||
**Goal:** Turn a folder into a Git repo, make commits, experiment with branches, and merge.
|
||
|
||
**Time:** ~10–15 minutes
|
||
|
||
---
|
||
|
||
### Step 1 — Set up (do once on your machine)
|
||
|
||
```bash
|
||
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
|
||
|
||
```bash
|
||
cd ~/my-project
|
||
git init
|
||
```
|
||
|
||
You should see: `Initialized empty Git repository...`
|
||
|
||
### Step 3 — Create your first file and commit it
|
||
|
||
```bash
|
||
echo "Roses are red" > poem.txt
|
||
git add poem.txt
|
||
git commit -m "Add first line of poem"
|
||
```
|
||
|
||
### Step 4 — Add a second line
|
||
|
||
```bash
|
||
echo "Violets are blue" >> poem.txt
|
||
```
|
||
|
||
### Step 5 — See what changed
|
||
|
||
```bash
|
||
git status
|
||
git diff
|
||
```
|
||
|
||
`git status` shows the file is modified. `git diff` shows exactly what lines changed.
|
||
|
||
### Step 6 — Commit the change
|
||
|
||
```bash
|
||
git add .
|
||
git commit -m "Add second line"
|
||
```
|
||
|
||
### Step 7 — View the history
|
||
|
||
```bash
|
||
git log --oneline
|
||
```
|
||
|
||
You should see two commits.
|
||
|
||
### Step 8 — Create a branch and switch to it
|
||
|
||
```bash
|
||
git checkout -b spanish
|
||
```
|
||
|
||
This creates a branch named `spanish` and switches to it.
|
||
|
||
### Step 9 — Add a Spanish version on this branch
|
||
|
||
```bash
|
||
echo "Las rosas son rojas" > poem_es.txt
|
||
git add .
|
||
git commit -m "Add Spanish version"
|
||
```
|
||
|
||
### Step 10 — Switch back to main
|
||
|
||
```bash
|
||
git checkout main
|
||
```
|
||
|
||
List the files — notice `poem_es.txt` is gone. It only exists on the `spanish` branch.
|
||
|
||
```bash
|
||
ls
|
||
```
|
||
|
||
### Step 11 — Merge the branch
|
||
|
||
```bash
|
||
git merge spanish
|
||
```
|
||
|
||
Now `poem_es.txt` is back on `main`.
|
||
|
||
```bash
|
||
ls
|
||
git log --oneline
|
||
```
|
||
|
||
### Step 12 — Clean up the branch (optional)
|
||
|
||
```bash
|
||
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
|