Git Exercises With Solutions: Advanced
12 advanced Git exercises with starting repositories, expected results, hints and explained solutions: annotated tags and describe, worktrees, submodules, patches with git am, bundles, removing a secret from all history, fixing authors, plumbing commits, dropped stashes and sparse checkout.
kvi5hnuCode Sprout team member10 min readOn this page
The last exercise set covers Parts 8 to 12: releases, larger projects, patches and bundles, rewriting history, and internals. Several exercises combine techniques from different articles, as real problems do. It works like the beginner and intermediate sets: run the block, add your commands under # --- your commands here ---, and compare the check's output with the expected result.
Exercises with submodules use local folders as remotes, so their setup allows the file protocol with git config --global protocol.file.allow always. Keep that setting to sandboxes: real projects use normal URLs.
1. Tag a release and describe a build
Create an annotated tag v1.0, with the message Version 1.0, on the commit Add ridge page, which is two commits before HEAD. Then write the output of git describe for HEAD into answer.txt.
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Ridge loop" > ridge.txt; git add .; git commit -q -m "Add ridge page"
echo "Forest loop" > forest.txt; git add .; git commit -q -m "Add forest page"
echo "Cave loop" > cave.txt; git add .; git commit -q -m "Add cave page"
# --- your commands here ---
# --- check ---
git cat-file -t v1.0 2>/dev/null || echo "no v1.0 tag"
git log -1 --format=%s v1.0 2>/dev/null
cat answer.txt 2>/dev/null || echo "no answer.txt yet"
no v1.0 tag
no answer.txt yet
Expected result after solving it:
tag
Add ridge page
v1.0-2-g85e97bb
Hint
git tag -a <name> -m <message> <commit> tags any commit. git describe names a commit relative to the nearest annotated tag.
Answer
git tag -a v1.0 -m "Version 1.0" HEAD~2
git describe > answer.txt
The annotated tag is a tag object, unlike a lightweight tag, which is just a reference to the commit. git describe prints v1.0-2-g<id>: the nearest tag, the number of commits since it, and g plus an abbreviated commit ID. That makes a readable build version (article 38).
2. Fix a bug in a second worktree
You're in the middle of uncommitted work on forest-page, and main needs an urgent fix. Without stashing or committing your forest work, create a worktree at ../trail-guide-hotfix on a new branch hotfix from main, commit Lake loop: 5.4 km to lake.txt there as Fix lake distance, and merge hotfix into main (from that worktree). Finally, remove the worktree.
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q -c forest-page
echo "Forest loop: draft" > forest.txt
# --- your commands here ---
# --- check ---
git log --format=%s main
git status --short
echo "worktrees: $(git worktree list | wc -l)"
Add lake page
?? forest.txt
worktrees: 1
Expected result after solving it:
Fix lake distance
Add lake page
?? forest.txt
worktrees: 1
Hint
git worktree add -b <new-branch> <path> <start> creates the folder, the branch and a checkout. main isn't checked out anywhere, so it can be switched to in the new worktree.
Answer
git worktree add -b hotfix ../trail-guide-hotfix main
cd ../trail-guide-hotfix
echo "Lake loop: 5.4 km" > lake.txt
git commit -am "Fix lake distance"
git switch main
git merge hotfix
cd ../trail-guide
git worktree remove ../trail-guide-hotfix
The hotfix happens in a separate folder sharing the same repository, so the uncommitted forest work is never touched. After merging, git worktree remove deletes the folder and its records. The hotfix branch remains, and can be deleted with git branch -d hotfix (article 40).
3. Add a library as a submodule
Add the repository ../maps-lib.git as a submodule at libs/maps, and commit it with the message Add maps library.
git config --global protocol.file.allow always
git init -q --bare maps-lib.git; git clone -q maps-lib.git ml 2>/dev/null
(cd ml && echo "v1" > map.js && git add . && git commit -q -m "Map library v1" && git push -q origin main)
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
# --- your commands here ---
# --- check ---
cat .gitmodules 2>/dev/null || echo "no .gitmodules"
git ls-tree HEAD libs/ | cut -d" " -f1,2
git log --format=%s
no .gitmodules
Add lake page
Expected result after solving it:
[submodule "libs/maps"]
path = libs/maps
url = ../maps-lib.git
160000 commit
Add maps library
Add lake page
Hint
git submodule add <url> <path> clones the repository and stages both .gitmodules and the submodule entry.
Answer
git submodule add ../maps-lib.git libs/maps
git commit -m "Add maps library"
The commit stores libs/maps as a gitlink, mode 160000, holding the library's commit ID, and .gitmodules maps the path to its URL. The relative URL ../maps-lib.git resolves against the superproject's own location (article 41).
4. Update a submodule to the library's latest version
The maps library has released Map library v2. Update the libs/maps submodule to the library's latest commit on main, and commit the update as Update maps library.
git config --global protocol.file.allow always
git init -q --bare maps-lib.git; git clone -q maps-lib.git ml 2>/dev/null
(cd ml && echo "v1" > map.js && git add . && git commit -q -m "Map library v1" && git push -q origin main)
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
git submodule add -q ../maps-lib.git libs/maps; git commit -q -m "Add maps library"
(cd ../ml && echo "v2" > map.js && git commit -q -am "Map library v2" && git push -q)
# --- your commands here ---
# --- check ---
git -C libs/maps log -1 --format="submodule at: %s"
git log -1 --format="last commit: %s"
git status --short
submodule at: Map library v1
last commit: Add maps library
Expected result after solving it:
submodule at: Map library v2
last commit: Update maps library
Hint
One git submodule update option fetches and checks out the tip of the submodule's remote branch. The superproject then shows the submodule as modified.
Answer
git submodule update --remote libs/maps
git add libs/maps
git commit -m "Update maps library"
git submodule update --remote fetches in the submodule and checks out origin/main, which changes the gitlink in the superproject. Committing records the new commit ID. Alternatively, cd libs/maps && git pull origin main (or git switch --detach origin/main after fetching), then add and commit (article 41).
5. Apply a contributor's patches
Ana sent two commits as patch files in ../patches. Apply them to main, keeping Ana as the author of both.
git init -q --bare server.git
git clone -q server.git trail-guide 2>/dev/null; cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"; git push -q origin main
cd ..; git clone -q server.git ana; cd ana
git config user.name "Ana Diaz"; git config user.email ana@example.com
echo "Lake loop: 5.4 km" > lake.txt; git commit -q -am "Fix lake distance"
echo "Ridge loop" > ridge.txt; git add .; git commit -q -m "Add ridge page"
git format-patch -q origin/main -o ../patches > /dev/null
cd ../trail-guide
# --- your commands here ---
# --- check ---
git log --format='%an: %s'
Sam Lee: Add lake page
Expected result after solving it:
Ana Diaz: Add ridge page
Ana Diaz: Fix lake distance
Sam Lee: Add lake page
Hint
git apply changes files only. Another command applies format-patch files as commits.
Answer
git am ../patches/*.patch
git am reads each mailbox-format patch and creates a commit with its author, date and message, so the log shows Ana as author, while you're the committer. With a branch that has moved on, git am --3way resolves most context differences (article 45).
6. Back up everything into a bundle
Create a bundle file ../backup.bundle containing every branch and tag of the repository.
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
git branch maps
git tag -a v1.0 -m "Version 1.0"
git switch -q -c drafts; git commit -q --allow-empty -m "Draft notes"; git switch -q main
# --- your commands here ---
# --- check ---
git bundle list-heads ../backup.bundle 2>/dev/null | awk '{print $2}' || true
[ -f ../backup.bundle ] || echo "no bundle yet"
no bundle yet
Expected result after solving it:
refs/heads/drafts
refs/heads/main
refs/heads/maps
refs/tags/v1.0
HEAD
Hint
git bundle create <file> <refs> takes the same reference arguments as git log, including one that means "all refs".
Answer
git bundle create ../backup.bundle --all
--all includes every branch and tag, plus HEAD. git bundle verify ../backup.bundle checks it, and git clone ../backup.bundle restores it (article 46).
7. Bring a field office up to date from a bundle
The field office's clone was made from a full bundle, and has no network. The file ../update.bundle contains the commits added to main since then. Bring the field office's main up to date with it.
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
git tag -a sent-1 -m "Sent to the field office"
git bundle create -q ../full.bundle --all 2>/dev/null
cd ..; git clone -q full.bundle field-office 2>/dev/null; cd trail-guide
echo "Ridge loop" > ridge.txt; git add .; git commit -q -m "Add ridge page"
echo "Forest loop" > forest.txt; git add .; git commit -q -m "Add forest page"
git bundle create -q ../update.bundle sent-1..main 2>/dev/null
cd ../field-office
# --- your commands here ---
# --- check ---
git log --format=%s main
ls
Add lake page
lake.txt
Expected result after solving it:
Add forest page
Add ridge page
Add lake page
forest.txt
lake.txt
ridge.txt
Hint
A bundle file can be used wherever Git expects a remote URL, including git fetch and git pull.
Answer
git pull ../update.bundle main
git pull ../update.bundle main fetches main from the bundle and merges it, which is a fast-forward here. The field office already has sent-1's commit, the bundle's prerequisite. Equivalent: git fetch ../update.bundle main:refs/remotes/origin/main, then git merge origin/main (article 46).
8. Remove a leaked key from all history
config.env, holding an API key, was committed and later deleted, so the key is still in history. Assume the key has already been rotated. Remove config.env from every commit on main, and make sure no object in the repository still contains the key: not in old refs, the reflog, or unreachable objects. The sandbox doesn't have git filter-repo, so use built-in commands.
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "MAP_API_KEY=sk_live_4f9a2c" > config.env; git add .; git commit -q -m "Add config"
echo "Better maps" >> lake.txt; git commit -q -am "Improve lake page"
git rm -q config.env; git commit -q -m "Remove config"
# --- your commands here ---
# --- check ---
git log --format=%s
echo "objects containing the key: $(git cat-file --batch-all-objects --batch | grep -a -c sk_live_4f9a2c)"
Remove config
Improve lake page
Add config
Add lake page
objects containing the key: 1
Expected result after solving it:
Improve lake page
Add lake page
objects containing the key: 0
Hint
git filter-branch --index-filter can remove a file from every commit without checking them out. Afterwards, three things still hold the old commits: the backup refs filter-branch creates under refs/original/, the reflogs, and the objects themselves until they're pruned.
Answer
FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch --index-filter 'git rm -q --cached --ignore-unmatch config.env' --prune-empty -- --all
git update-ref -d refs/original/refs/heads/main
git reflog expire --expire=now --all
git gc --prune=now
The index filter removes config.env from each commit, and --prune-empty drops the commits that become empty: Add config and Remove config. Then the backup ref is deleted, the reflogs are expired and gc --prune=now deletes the unreachable objects. With git filter-repo, the first line would be git filter-repo --path config.env --invert-paths, and it cleans up by itself. Either way, every other clone still has the key, which is why rotating it comes first (articles 47 and 50).
9. Fix the author of unpushed commits
Your last two commits were made with your personal email, sam@personal.example. Set this repository's email to sam@example.com, and change the author of those two commits to it, keeping their messages and content.
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
git config user.email sam@personal.example
echo "Ridge loop" > ridge.txt; git add .; git commit -q -m "Add ridge page"
echo "Forest loop" > forest.txt; git add .; git commit -q -m "Add forest page"
# --- your commands here ---
# --- check ---
git log --format='%ae %s'
sam@personal.example Add forest page
sam@personal.example Add ridge page
sam@example.com Add lake page
Expected result after solving it:
sam@example.com Add forest page
sam@example.com Add ridge page
sam@example.com Add lake page
Hint
git commit --amend --reset-author takes the current identity. git rebase --exec runs a command after each replayed commit.
Answer
git config user.email sam@example.com
git rebase HEAD~2 --exec 'git commit --amend --no-edit --reset-author'
The rebase replays the two commits and amends each with the new identity. --reset-author also sets the author date to now. Since the commits aren't pushed, nobody else is affected. git commit --amend --no-edit --reset-author alone would only fix the last one (article 47).
10. Make a commit with plumbing commands
Without using git add or git commit, create a commit whose snapshot contains a single file, hello.txt, with the content Hello, whose parent is main's current commit, and whose message is Made by hand. Make the new branch plumbing point to it. Don't change main.
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
# --- your commands here ---
# --- check ---
git log --format=%s plumbing 2>/dev/null || echo "no plumbing branch"
git ls-tree --name-only plumbing 2>/dev/null
git show plumbing:hello.txt 2>/dev/null
git log -1 --format="main: %s" main
no plumbing branch
main: Add lake page
Expected result after solving it:
Made by hand
Add lake page
hello.txt
Hello
main: Add lake page
Hint
You need a blob (git hash-object -w), a tree containing it (git mktree reads ls-tree-style lines, or use git update-index and git write-tree), a commit (git commit-tree), and a ref (git update-ref).
Answer
blob=$(echo "Hello" | git hash-object -w --stdin)
tree=$(printf '100644 blob %s\thello.txt\n' "$blob" | git mktree)
commit=$(echo "Made by hand" | git commit-tree "$tree" -p main)
git update-ref refs/heads/plumbing "$commit"
git mktree builds a tree from a listing, without touching the index, so main's staged state is unaffected. git commit-tree -p main sets the parent, and git update-ref creates the branch. Compared with main, the snapshot "deletes" lake.txt, because it contains only hello.txt (articles 48 and 49).
11. Recover a dropped stash
A stash holding work on lake.txt was dropped with git stash drop. Get its changes back into the working tree.
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Lake loop: 5.4 km, with parking notes" > lake.txt
git stash -q
git stash drop -q
# --- your commands here ---
# --- check ---
cat lake.txt
git status --short
Lake loop
Expected result after solving it:
Lake loop: 5.4 km, with parking notes
M lake.txt
Hint
A stash is a commit. After it's dropped, nothing refers to it, so git fsck reports it as dangling.
Answer
lost=$(git fsck --no-reflogs 2>/dev/null | awk '/dangling commit/ {print $3}')
git stash apply "$lost"
git fsck --no-reflogs lists objects that no ref reaches, and the dropped stash appears as a dangling commit. git stash apply <id> applies it like any stash. With several dangling commits, inspect them with git show <id> first, or list them with git log --oneline (article 26). The git stash drop message also prints the stash's ID.
12. Check out only part of a monorepo
You only work on the web and docs folders of this repository. Make the working tree contain only those two folders, plus the files at the top level, without changing any commit.
git init -q monorepo && cd monorepo
mkdir web api docs tools
for d in web api docs tools; do echo "code" > $d/main.txt; done
echo "# Monorepo" > README.md
git add .; git commit -q -m "Initial layout"
# --- your commands here ---
# --- check ---
ls
echo "files in HEAD: $(git ls-tree -r --name-only HEAD | wc -l)"
README.md
api
docs
tools
web
files in HEAD: 5
Expected result after solving it:
README.md
docs
web
files in HEAD: 5
Hint
Sparse checkout in cone mode takes a list of folders.
Answer
git sparse-checkout set web docs
git sparse-checkout set web docs writes only those folders, plus top-level files, to the working tree. The commits still contain all five files, which the check confirms. git sparse-checkout disable restores the full tree (article 44).
Common questions
Is git filter-branch acceptable for the secret-removal exercise?
In this sandbox, yes, since git filter-repo isn't installed. On real repositories, install filter-repo: it's much faster, safer, and cleans up automatically. The cleanup steps in the exercise show what "really gone" requires locally.
In the plumbing exercise, could I use git update-index and git write-tree instead of git mktree?
Yes, and that's how article 49 does it. The difference is that update-index changes the index of your current checkout, so you'd need to reset it afterwards. mktree builds the tree directly, leaving the index alone.
Why did the dropped-stash recovery use --no-reflogs?
Here plain git fsck finds the same commit, because dropping a stash removes its only reflog entry. --no-reflogs is a safe habit: it makes fsck consider only refs, so commits that only a reflog still reaches, such as ones lost by a reset, show up as dangling too.
Summary
- Annotated tags and
git describename releases and builds. Worktrees allow parallel work without stashing. - Submodules are added with
submodule addand moved withsubmodule update --remoteplus a commit. git amapplies patches as commits, and bundles move complete or incremental history as files.- Removing data from history needs a rewrite plus cleanup of backup refs, reflogs and unreachable objects.
rebase --execfixes recent commits. - Plumbing commands build objects and refs directly,
fsckfinds dangling commits, and sparse checkout limits the working tree.
Practice questions
In the bundle exercise for the field office, what would happen if the field office's clone didn't have the sent-1 commit?
Answer
The pull would fail, because the bundle is incremental: it contains only commits after sent-1, and the receiving repository must already have that commit. git bundle verify ../update.bundle reports "Repository lacks these prerequisite commits". The fix is a full bundle, or applying the earlier bundle first.
After removing the key from history in your repository, which other copies still contain it?
Answer
Every other clone and fork, the server and any caches it keeps (such as pull request refs), backups, and CI caches. Rewriting your repository and force pushing stops the key spreading through new clones, but only rotating it makes the leaked value useless.
That completes the exercises and the course. To test everything together, take the Git course final exam, then the quizzes on interview questions and delicate details.
Previous: Git Exercises With Solutions: Intermediate
Next: Git Course Outline: From First Commit to Git Internals

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.