Project 15.1 · Easy
A learning journal under version control
Start a repository for a journal of what you learn, and save one commit per day.
You will practise: git init, add, commit, commit -am, log --oneline and show.
Steps
- Make a new folder called
journal, open it in VS Code, and run git init in its terminal. - Create
journal.md with a heading. Add it and make your first commit. - Each day, add one line about what you learned and commit it with a short message.
-am adds and commits in one step, but only for files Git already tracks. - Read your history with
git log --oneline. - Look inside the last commit with
git show --stat. Then try plain git show to see the exact line you added.
Hint: keep this journal for the rest of the book. In a few weeks, git log will be a diary of your progress, and the habit of small, frequent commits is exactly what teams expect.
Solution
Terminal session from the book
$ git init
Initialized empty Git repository in /Users/sara/code/journal/.git/
$ git add journal.md
$ git commit -q -m "Start the journal"
$ git commit -q -am "Day 1"
$ git commit -q -am "Day 2"
$ git log --oneline
31670a5 Day 2
62eacf9 Day 1
b7c860c Start the journal
$ git show --stat --format=%s HEAD
Day 2
journal.md | 1 +
1 file changed, 1 insertion(+)
Project 15.2 · Medium
Two ways to undo
Make two mistakes on purpose, one not yet committed and one already committed, and undo each with the right tool.
You will practise: git status --short, git restore for uncommitted changes, and git revert for commits.
Steps
- Replace the whole contents of
journal.md with one silly line, and save. git status --short shows M: modified. - Run
git restore journal.md. The file goes back to the last commit, and the status is clean again. - Write Day 3 properly and commit it.
- Now create
quote.md, add it and commit it. Pretend you regret this commit. - Run
git revert --no-edit HEAD. Git makes a new commit that does the opposite, and the file disappears. - Read
git log --oneline: the mistake and its undo are both in the history, so nobody is surprised.
Hint: restore throws away work that was never saved, so there is no way back; use it with care. revert is always safe, even after you have pushed, because it adds history instead of erasing it.
Solution
Terminal session from the book
$ git status --short
M journal.md
$ git restore journal.md
$ git status --short
$ git commit -q -am "Day 3"
$ git add quote.md
$ git commit -q -m "Add a random quote"
$ git revert --no-edit HEAD
[main 8a28f85] Revert "Add a random quote"
1 file changed, 1 deletion(-)
delete mode 100644 quote.md
$ git log --oneline
8a28f85 Revert "Add a random quote"
4b00ed1 Add a random quote
30896b9 Day 3
31670a5 Day 2
62eacf9 Day 1
b7c860c Start the journal
Project 15.3 · Challenge
Create a merge conflict, then solve it
Conflicts frighten beginners only until they have solved one. Create one on purpose, read it, and resolve it calmly.
You will practise: branches, git switch -c, fast-forward and real merges, conflict markers, and finishing a merge.
Steps
- On
main, add a title line at the top of the journal and commit it. - Create a branch
personal, change the title, and commit. Go back to main. - Create a second branch
ambitious from main, change the same title line differently, and commit. Go back to main. - Merge
personal: it is a fast-forward, because main had not moved. Then merge ambitious: Git cannot choose between two changes to the same line, so it stops. - Open the file. Between
<<<<<<< and ======= is your version (HEAD); between ======= and >>>>>>> is the incoming one. - Write the line you really want, delete all three marker lines, then
git add and git commit. The graph shows the two branches joining.
Hint: a conflict is not an error. It is Git politely asking a human to decide. VS Code shows buttons above each conflict (“Accept Current”, “Accept Incoming”, “Accept Both”), but always read the result before you commit.
Solution
Terminal session from the book
$ git commit -q -am "Add a title"
$ git switch -q -c personal
$ git commit -q -am "Personal title"
$ git switch -q main
$ git switch -q -c ambitious
$ git commit -q -am "Ambitious title"
$ git switch -q main
$ git merge personal
Updating 5ffca6e..d5dc855
Fast-forward
journal.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
$ git merge ambitious
Auto-merging journal.md
CONFLICT (content): Merge conflict in journal.md
Automatic merge failed; fix conflicts and then commit the result.
$ head -5 journal.md
<<<<<<< HEAD
Title: Sara’s learning journal
=======
Title: My road to senior
>>>>>>> ambitious
$ git add journal.md
$ git commit -q -m "Choose the final title"
$ git log --oneline --graph -4
* 9900c88 Choose the final title
|\
| * fc23fdc Ambitious title
* | d5dc855 Personal title
|/
* 5ffca6e Add a title