Do this in a terminal on your own machine, in a new empty folder. You need Git 2.28 or newer (git --version) and a Bash-like shell: the Terminal on macOS and Linux, Git Bash on Windows. Nothing here touches any other project.
1. Create a repository.
mkdir git-practice
cd git-practice
git init -b main
Git answers Initialized empty Git repository in .../git-practice/.git/. git status now says On branch main and No commits yet.
2. Check that Git knows who you are. Every commit stores an author.
git config user.name
git config user.email
Each command should print a value. If one prints nothing, set it for this folder only (leave out --global on a shared computer):
git config user.name "Your Name"
git config user.email "you@example.com"
3. Write, stage, commit. The typo in the heading is on purpose.
echo "# Order Servcie" > README.md
git add README.md
git commit -m "Add README"
The commit prints a line like [main (root-commit) f3c5c63] Add README and 1 file changed, 1 insertion(+). Your hash will differ.
4. Make two unrelated changes before you commit anything.
echo 'fun discount(total: Double): Double = if (total >= 100.0) total * 0.9 else total' > Discount.kt
echo "# Order Service" > README.md
git status --short
The output has two lines. M README.md means a tracked file changed. ?? Discount.kt means Git does not know the file yet.
5. Commit them separately, one concern each.
git add Discount.kt
git status --short
git commit -m "Add discount rule"
git add README.md
git commit -m "Fix typo in README"
Between the two commits git status --short shows A Discount.kt and M README.md: only the staged file goes into the next commit, the other one waits. Each commit reports 1 file changed.
6. Look at what you built.
git log --oneline
git show --stat HEAD~1
git ls-tree --name-only HEAD~1
git cat-file -p HEAD
git cat-file -p HEAD~2
HEAD~1 is the commit before HEAD, HEAD~2 the one before that. What you should see:
git log --oneline lists three commits, newest first: Fix typo in README, Add discount rule, Add README.git show --stat HEAD~1 shows the commit Add discount rule and one changed file, Discount.kt. That is what the commit changed.git ls-tree --name-only HEAD~1 lists two files, Discount.kt and README.md. That is what the commit contains.git cat-file -p HEAD prints a tree line, a parent line, author and committer, and the message.git cat-file -p HEAD~2 prints the same without a parent line, because it is the first commit.Before you get a hint: what have you tried, and where exactly are you stuck?
1. In step 5 you ran git add Discount.kt while README.md also had your typo fix. What does the next git commit record?
2. git show --stat HEAD~1 reports one changed file, but git ls-tree --name-only HEAD~1 lists two files. Why?
3. What is different in the output of git cat-file -p for the first commit of a repository compared to the later ones?
4. Which subject line fits a commit that makes the payment webhook retry once after a 503?
5. A file contains two unrelated edits, a rounding fix and a renamed variable. How do you still get two commits?