TExcelTAD
Home › Git › Git Exercises

Git Exercises

Time to practise. Each exercise has a starter script that sets up a situation and prints a check at the end. As given, the check shows the "unsolved" state. Replace the # TODO lines with your commands, run the script in your own terminal (or on any Linux machine with Git), and compare the result with the Expected output. The solution is hidden below each exercise: try for a few minutes first.

Note: On this site, bash examples show their saved output; they run in a real shell when you copy them into your terminal. Use an empty folder, because the scripts create files. Your commit hashes will differ from any shown here, since they depend on your name and the date.

# Exercise Topics
1 First commit init, add, commit
2 Commit only some files staging
3 Feature branch branch, switch, merge
4 Resolve a conflict merge conflicts
5 Undo a shared mistake revert
6 Tag a release tag, describe
7 Ignore build output .gitignore, rm --cached
8 Rescue a deleted branch reflog
9 Publish and clone remotes, push, clone
10 Squash your commits reset --soft

1. First commit

Create a repository diary, add a file day1.txt containing Learned git init, and commit it with the message Add first diary entry.

Expected:

commits: 1
status: clean
bash · try it yourself
mkdir diary && cd diary
# TODO: initialise the repository, create day1.txt, stage and commit it
echo "commits: $(git rev-list --count HEAD 2>/dev/null || echo 0)"
[ -z "$(git status --porcelain 2>/dev/null)" ] && git rev-parse -q HEAD >/dev/null 2>&1 && echo "status: clean" || echo "status: not clean"
commits: 0
status: not clean
Show solution
bash
mkdir diary && cd diary
git init -q
echo "Learned git init" > day1.txt
git add day1.txt
git commit -q -m "Add first diary entry"

2. Commit only some files

The script creates index.html, style.css and notes.txt. Commit the two website files (message Add website) but leave notes.txt uncommitted and untracked.

Expected:

committed: index.html style.css
?? notes.txt
bash · try it yourself
mkdir site && cd site && git init -q
echo "<h1>Hi</h1>" > index.html; echo "h1 {}" > style.css; echo "private" > notes.txt
# TODO: stage the two website files and commit them
echo "committed: $(git ls-files | tr '\n' ' ')"
git status -s
committed:
?? index.html
?? notes.txt
?? style.css
Show solution
bash
git add index.html style.css
git commit -q -m "Add website"

3. Feature branch

Create a branch feature/contact, add contact.html (any content) in a commit there, switch back to main and merge the branch. Finally delete the branch.

Expected:

branches: main
files: contact.html index.html
commits on main: 2
bash · try it yourself
mkdir site && cd site && git init -q
echo "home" > index.html && git add . && git commit -q -m "Add home page"
# TODO: branch, commit contact.html, switch back, merge, delete the branch
echo "branches: $(git branch --format='%(refname:short)' | tr '\n' ' ')"
echo "files: $(git ls-files | tr '\n' ' ')"
echo "commits on main: $(git rev-list --count main)"
branches: main
files: index.html
commits on main: 1
Show solution
bash
git switch -c feature/contact
echo "email us" > contact.html
git add contact.html
git commit -q -m "Add contact page"
git switch main
git merge feature/contact          # a fast-forward
git branch -d feature/contact

4. Resolve a conflict

Both branches changed the same line, so git merge theme conflicts. Resolve it so theme.ini contains exactly color=green, and finish the merge.

Expected:

color=green
merge commit parents: 2
bash · try it yourself
mkdir app && cd app && git init -q
echo "color=white" > theme.ini && git add . && git commit -q -m "Theme"
git switch -q -c theme && echo "color=blue" > theme.ini && git commit -q -am "Blue theme"
git switch -q main && echo "color=red" > theme.ini && git commit -q -am "Red theme"
git merge theme >/dev/null
# TODO: write the resolved file, stage it and commit the merge
cat theme.ini
echo "merge commit parents: $(git log -1 --format=%p | wc -w)"
<<<<<<< HEAD
color=red
=======
color=blue
>>>>>>> theme
merge commit parents: 1
Show solution
bash
echo "color=green" > theme.ini
git add theme.ini
git commit -q --no-edit

5. Undo a shared mistake

The commit "Set price to 1000" has already been pushed, so you must not rewrite history. Undo it with a new commit.

Expected:

price=10
3 commits, newest: Revert "Set price to 1000"
bash · try it yourself
mkdir shop && cd shop && git init -q
echo "price=10" > price.ini && git add . && git commit -q -m "Set price"
echo "price=1000" > price.ini && git commit -q -am "Set price to 1000"
# TODO: undo the last commit without rewriting history
cat price.ini
echo "$(git rev-list --count HEAD) commits, newest: $(git log -1 --format=%s)"
price=1000
2 commits, newest: Set price to 1000
Show solution
bash
git revert --no-edit HEAD

6. Tag a release

Create an annotated tag v1.0 (message First release) on the commit "Release 1.0" (which is HEAD~2), so git describe reports two commits since the release.

Expected (your hash will differ):

tag type: tag
v1.0-2-g<hash>
bash · try it yourself
mkdir app && cd app && git init -q
for msg in "Start" "Release 1.0" "Fix typo" "Add logo"; do
  echo "$msg" >> log.txt; git add .; git commit -q -m "$msg"
done
# TODO: create the annotated tag on the right commit
echo "tag type: $(git cat-file -t v1.0 2>/dev/null || echo none)"
git describe 2>&1 | head -1
tag type: none
fatal: No names found, cannot describe anything.
Show solution
bash
git tag -a v1.0 -m "First release" HEAD~2

7. Ignore build output

Someone committed the build/ folder. Make Git ignore build/ and *.log from now on, stop tracking the committed build files (but keep them on disk), and commit the change as Ignore build output.

Expected:

tracked: .gitignore app.py
on disk: app.py build debug.log
bash · try it yourself
mkdir app && cd app && git init -q
mkdir build && echo "binary" > build/app.bin && echo "print(1)" > app.py
git add . && git commit -q -m "Initial commit"
echo "noise" > debug.log
# TODO: create .gitignore, untrack build/, commit
echo "tracked: $(git ls-files | tr '\n' ' ')"
echo "on disk: $(ls | tr '\n' ' ')"
tracked: app.py build/app.bin
on disk: app.py build debug.log
Show solution
bash
printf 'build/\n*.log\n' > .gitignore
git rm -r -q --cached build
git add .gitignore
git commit -q -m "Ignore build output"

8. Rescue a deleted branch

The branch recipes was force-deleted before it was merged. Use the reflog to recreate it as rescued.

Expected:

Secret recipe
Start
bash · try it yourself
mkdir kitchen && cd kitchen && git init -q
echo "menu" > menu.txt && git add . && git commit -q -m "Start"
git switch -q -c recipes && echo "grandma's soup" > soup.txt && git add . && git commit -q -m "Secret recipe"
git switch -q main && git branch -q -D recipes
# TODO: find the lost commit in the reflog and create the branch "rescued" there
git log --format=%s rescued 2>&1 | head -2
fatal: ambiguous argument 'rescued': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
Show solution
bash
git reflog                       # "checkout: moving from recipes to main" is HEAD@{0};
git branch rescued HEAD@{1}      # HEAD@{1} is the "Secret recipe" commit

9. Publish and clone

A bare repository ~/server/notes.git plays GitHub. Add it as origin, push main with upstream tracking, then clone it into ~/laptop and check the file arrived.

Expected:

upstream: origin/main
laptop has: todo.txt
bash · try it yourself
git init -q --bare ~/server/notes.git
mkdir ~/notes && cd ~/notes && git init -q
echo "buy milk" > todo.txt && git add . && git commit -q -m "Add todo"
# TODO: add the remote, push with -u, clone into ~/laptop
echo "upstream: $(git rev-parse --abbrev-ref main@{upstream} 2>/dev/null || echo none)"
echo "laptop has: $(ls ~/laptop 2>/dev/null)"
upstream: none
laptop has:
Show solution
bash
git remote add origin ~/server/notes.git
git push -q -u origin main
git clone -q ~/server/notes.git ~/laptop

10. Squash your commits

Your three local "WIP" commits should become one commit called Add search feature, with the same final content.

Expected:

Add search feature
Initial commit
search.txt: input, button, results
bash · try it yourself
mkdir app && cd app && git init -q
echo "app" > app.txt && git add . && git commit -q -m "Initial commit"
for part in input button results; do
  echo -n "$part" >> search.txt; [ "$part" != results ] && echo -n ", " >> search.txt
  git add . && git commit -q -m "WIP $part"
done
echo >> search.txt && git commit -q -am "WIP newline"
# TODO: turn the four WIP commits into one
git log --format=%s
echo "search.txt: $(cat search.txt)"
WIP newline
WIP results
WIP button
WIP input
Initial commit
search.txt: input, button, results
Show solution
bash
git reset --soft HEAD~4
git commit -q -m "Add search feature"

(An interactive rebase with fixup lines does the same: git rebase -i HEAD~4.)

Test yourself

Which command turns your last three local commits into staged changes, ready to be recommitted as one?

  1. git reset --hard HEAD~3
  2. git reset --soft HEAD~3
  3. git revert HEAD~3
  4. git stash -3

You need to undo a commit that is already on the shared main branch. Which command?

  1. git reset --hard HEAD~1 and force push
  2. git revert <commit>
  3. git commit --amend
  4. git branch -D main