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"

Make main the name of the first branch in new repositories.

git config --global  main

Turn the current folder into a Git repository.

git 

Show a compact, one-line-per-file status.

git status 

Stage every change in the repository, including new and deleted files.

git add 

Commit the staged changes with a message.

git commit  "Add pancake recipe"

Stage all modified tracked files and commit in one command.

git commit  "Fix typo in omelette recipe"

Inspecting

Show the history, one line per commit, as a graph of all branches.

git log  --graph --all

Show the changes that are staged for the next commit.

git diff 

Show the commit two before the current one.

git show HEAD

Print pancakes.txt as it was three commits ago, without changing any files.

git show HEAD~3pancakes.txt

List the commits on feature that aren't on main.

git log --oneline 

Find out which .gitignore rule ignores a file.

git  -v debug.log

Stop tracking a file but keep it on disk.

git rm  settings.local.ini

Rename a file and stage the rename.

git  omelette.txt cheese-omelette.txt

Undoing

Discard your uncommitted edits to a file.

git  pancakes.txt

Unstage a file, keeping your edits.

git restore  pancakes.txt

Add a forgotten file to the last commit, keeping its message.

git commit --amend 

Undo the last commit but keep its changes staged.

git reset  HEAD~1

Undo a commit that has already been pushed, by adding a new commit.

git  a1b2c3d

Show where HEAD has been, to find lost commits.

git 

Preview which untracked files and folders would be deleted.

git clean 

Branches

Create a branch called desserts and switch to it.

git switch  desserts

Merge desserts into the current branch, always creating a merge commit.

git merge  desserts

Cancel a merge that hit conflicts.

git merge 

Continue a rebase after resolving and staging a conflict.

git rebase 

Clean up the last four commits interactively.

git rebase  HEAD~4

Copy a single commit onto the current branch, noting where it came from.

git  -x a1b2c3d

Stash your changes including untracked files.

git stash 

Delete a branch that has been merged.

git branch  desserts

Remotes and releases

Connect your repository to GitHub under the usual name.

git remote add  https://github.com/sam-rivera/recipes.git

Push a new branch and set its upstream.

git push  origin feature/search

Overwrite your own remote branch after a rebase — safely.

git push 

Download new commits without changing your branches, and drop deleted remote branches.

git fetch 

Pull, replaying your local commits on top of the remote's.

git pull 

Create an annotated tag for a release.

git tag  v1.0.0 -m "First release"

Find the commits that added or removed the text TAX_RATE.

git log  "TAX_RATE"

Start a binary search for the commit that broke something.

git  start

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
Output
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
Output
[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
Output
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
Output
[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
Output
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
Output
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 version

5. 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
Output
Successfully rebased and updated refs/heads/main.
40f0ffb (HEAD -> main) Add login form
b990ed0 Create home page