Git Exercises
Practise the commands. Type the missing piece into each highlighted gap and press Check. Further down are hands-on challenges to try in a terminal, each with a model solution whose output was produced by running it with real Git.
Fill in the blanks
Setup and basics
Set the name that will appear on your commits, for all repositories.
git config user.name "Sam Rivera"
--global writes to ~/.gitconfig (lesson 03).
Make main the name of the first branch in new repositories.
git config --global main
(lesson 03)
Turn the current folder into a Git repository.
git
(lesson 05)
Show a compact, one-line-per-file status.
git status
(lesson 07)
Stage every change in the repository, including new and deleted files.
git add
Run from the top folder, git add . does the same (lesson 08).
Commit the staged changes with a message.
git commit "Add pancake recipe"
(lesson 09)
Stage all modified tracked files and commit in one command.
git commit "Fix typo in omelette recipe"
-a skips untracked files (lesson 09).
Inspecting
Show the history, one line per commit, as a graph of all branches.
git log --graph --all
(lesson 11)
Show the changes that are staged for the next commit.
git diff
Plain git diff shows unstaged changes (lesson 12).
Show the commit two before the current one.
git show HEAD
(lesson 13)
Print pancakes.txt as it was three commits ago, without changing any files.
git show HEAD~3pancakes.txt
(lesson 13)
List the commits on feature that aren't on main.
git log --oneline
(lesson 13)
Find out which .gitignore rule ignores a file.
git -v debug.log
(lesson 14)
Stop tracking a file but keep it on disk.
git rm settings.local.ini
(lesson 14)
Rename a file and stage the rename.
git omelette.txt cheese-omelette.txt
(lesson 15)
Undoing
Discard your uncommitted edits to a file.
git pancakes.txt
(lesson 16)
Unstage a file, keeping your edits.
git restore pancakes.txt
(lesson 16)
Add a forgotten file to the last commit, keeping its message.
git commit --amend
Only for commits you haven't pushed (lesson 17).
Undo the last commit but keep its changes staged.
git reset HEAD~1
(lesson 18)
Undo a commit that has already been pushed, by adding a new commit.
git a1b2c3d
(lesson 19)
Show where HEAD has been, to find lost commits.
git
(lesson 20)
Preview which untracked files and folders would be deleted.
git clean
-n is a dry run; -d includes folders (lesson 21).
Branches
Create a branch called desserts and switch to it.
git switch desserts
The older form is git checkout -b desserts (lesson 23).
Merge desserts into the current branch, always creating a merge commit.
git merge desserts
(lesson 24)
Cancel a merge that hit conflicts.
git merge
(lesson 25)
Continue a rebase after resolving and staging a conflict.
git rebase
(lesson 26)
Clean up the last four commits interactively.
git rebase HEAD~4
(lesson 27)
Copy a single commit onto the current branch, noting where it came from.
git -x a1b2c3d
(lesson 28)
Stash your changes including untracked files.
git stash
(lesson 29)
Delete a branch that has been merged.
git branch desserts
-D forces deletion of unmerged work (lesson 22).
Remotes and releases
Connect your repository to GitHub under the usual name.
git remote add https://github.com/sam-rivera/recipes.git
(lesson 30)
Push a new branch and set its upstream.
git push origin feature/search
(lesson 32)
Overwrite your own remote branch after a rebase — safely.
git push
(lesson 32)
Download new commits without changing your branches, and drop deleted remote branches.
git fetch
(lessons 34–35)
Pull, replaying your local commits on top of the remote's.
git pull
(lesson 34)
Create an annotated tag for a release.
git tag v1.0.0 -m "First release"
(lesson 39)
Find the commits that added or removed the text TAX_RATE.
git log "TAX_RATE"
(lesson 40)
Start a binary search for the commit that broke something.
git start
(lesson 41)
Hands-on challenges
Do these in a terminal, in a fresh practice folder. Try first, then compare with the model solution.
1. A first repository
Create a repository called notes with a file ideas.txt and a .gitignore that ignores *.tmp files. Make two commits — one for each file — then show the history in one line per commit.
Show a model solution
git init notes cd notes echo "Learn Git" > ideas.txt git add ideas.txt git commit -m "Add ideas list" echo "*.tmp" > .gitignore git add .gitignore git commit -m "Ignore temporary files" git log --oneline
Initialized empty Git repository in /home/sam/notes/.git/ [main (root-commit) 3a2c0c0] Add ideas list 1 file changed, 1 insertion(+) create mode 100644 ideas.txt [main b8aa0db] Ignore temporary files 1 file changed, 1 insertion(+) create mode 100644 .gitignore b8aa0db (HEAD -> main) Ignore temporary files 3a2c0c0 Add ideas list
2. Fix the last commit
Commit a file with a misspelled message, then fix the message and add a second file to the same commit — without creating a new one.
Show a model solution
echo "Monday: started Git course" > monday.txt git add monday.txt git commit -m "Add mondy entry" echo "Tuesday: learned branching" > tuesday.txt git add tuesday.txt git commit --amend -m "Add Monday and Tuesday entries" git log --oneline --stat
[main (root-commit) c04034e] Add mondy entry 1 file changed, 1 insertion(+) create mode 100644 monday.txt [main 3fc3525] Add Monday and Tuesday entries Date: Mon Mar 2 09:56:00 2026 +0000 2 files changed, 2 insertions(+) create mode 100644 monday.txt create mode 100644 tuesday.txt 3fc3525 (HEAD -> main) Add Monday and Tuesday entries monday.txt | 1 + tuesday.txt | 1 + 2 files changed, 2 insertions(+)
3. Branch, conflict, merge
In a repository with a file greeting.txt containing Hello, change the line to Hello, world on a branch called english, and to Hola on main. Merge english into main, resolve the conflict by keeping both greetings on separate lines, and finish the merge.
Show a model solution
git switch -c english echo "Hello, world" > greeting.txt git commit -qam "Use English greeting" git switch main echo "Hola" > greeting.txt git commit -qam "Use Spanish greeting" git merge english
Switched to a new branch 'english' Switched to branch 'main' Auto-merging greeting.txt CONFLICT (content): Merge conflict in greeting.txt Automatic merge failed; fix conflicts and then commit the result.
printf 'Hola\nHello, world\n' > greeting.txt
git add greeting.txt
git commit --no-edit
git log --oneline --graph
[main 97354e7] Merge branch 'english' * 97354e7 (HEAD -> main) Merge branch 'english' |\ | * db8b0bd (english) Use English greeting * | bf34a26 Use Spanish greeting |/ * c593842 Add greeting
4. Rescue a deleted branch
Create a branch with one commit, delete it with git branch -D, and bring it back using the reflog.
Show a model solution
git switch -c experiment echo "risky idea" > idea.txt git add idea.txt git commit -qm "Try a risky idea" git switch main git branch -D experiment
Switched to a new branch 'experiment' Switched to branch 'main' Deleted branch experiment (was f80de63).
git reflog -3
git branch experiment HEAD@{1}
git log --oneline experiment
e34e8b9 (HEAD -> main) HEAD@{0}: checkout: moving from experiment to main
f80de63 HEAD@{1}: commit: Try a risky idea
e34e8b9 (HEAD -> main) HEAD@{2}: checkout: moving from main to experiment
f80de63 (experiment) Try a risky idea
e34e8b9 (HEAD -> main) First version5. Tidy before sharing
Make three commits: "Add login form", "WIP" and "fix typo". Use one interactive rebase to fold the last two into the first, so only one commit remains. (Change pick to fixup on the second and third lines of the todo list.)
Show a model solution
echo "<form>login</form>" > login.html git add login.html && git commit -qm "Add login form" echo "<form>login <input></form>" > login.html git commit -qam "WIP" echo "<form>Login <input></form>" > login.html git commit -qam "fix typo" git rebase -i HEAD~3 git log --oneline
Successfully rebased and updated refs/heads/main. 40f0ffb (HEAD -> main) Add login form b990ed0 Create home page