Git Exercises With Solutions: Beginner
13 hands-on beginner Git exercises, each with a ready-made starting repository, the expected result, a hint and an explained solution: first commits, partial staging, amending, ignoring files, searching history, restoring files, branches, merges, conflicts, rebasing, squashing and cherry-picking.
kvi5hnuCode Sprout team member10 min readOn this page
This is the first of three graded exercise sets. Each exercise gives you a small repository in a particular state, and a task to bring it into another state, just like real work. The sets get harder as they go:
| Set | Covers | Articles |
|---|---|---|
| Beginner (this one) | commits, staging, ignoring, history, branches, merging, conflicts, rebasing | 1–16 |
| Intermediate | remotes, pushing and pulling, undoing, stashing, the reflog, revisions, blame, bisect, configuration and hooks | 17–37 |
| Advanced | tags, worktrees, submodules, patches, bundles, rewriting history, internals and recovery | 38–53 |
How to use these exercises
Each exercise has a runnable block in three sections:
- Setup builds the starting repository. Don't change it.
# --- your commands here ---: type your solution here.# --- check ---prints the state of the repository.
Run the block once to see the starting state, add your commands, and run it again. When the check prints the same as the expected result, you've solved it. Your commands don't have to match the answer: any solution that produces the same result is correct. Open the hint if you're stuck, and the answer after a real attempt.
The sandbox has no text editor, so always give messages on the command line, with git commit -m "...", git merge --no-edit or git commit --amend --no-edit. You can also copy any setup into a terminal on your own machine, in an empty folder.
1. Your first commit
The folder contains two new files. Commit both in a single commit with the message Add README and lake page.
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md
echo "Lake loop: 5 km" > lake.txt
# --- your commands here ---
# --- check ---
git log --format=%s 2>&1
git status --short
fatal: your current branch 'main' does not have any commits yet
?? README.md
?? lake.txt
Expected result after solving it:
Add README and lake page
Hint
Files must be staged with git add before git commit records them. git status shows what's staged.
Answer
git add README.md lake.txt
git commit -m "Add README and lake page"
git add copies the files into the staging area, and git commit -m records the staged snapshot with a message. git add . would also work here, since it stages everything in the current folder. Before the first commit, git log reports that the branch has no commits yet (article 3).
2. Commit only some of the changes
You fixed the lake page's distance, and also started some private notes in notes.txt. Commit only the change to lake.txt, with the message Fix lake distance. notes.txt must stay uncommitted.
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Lake loop: 5.4 km" > lake.txt
echo "ask about parking" > notes.txt
# --- your commands here ---
# --- check ---
git log --format=%s
git status --short
Add lake page
M lake.txt
?? notes.txt
Expected result after solving it:
Fix lake distance
Add lake page
?? notes.txt
Hint
Name the file you want to stage, rather than using git add . or git commit -a.
Answer
git add lake.txt
git commit -m "Fix lake distance"
Staging a single path keeps everything else out of the commit, and notes.txt stays untracked (??). Note that git commit -a wouldn't have included notes.txt either, because -a only stages files Git already tracks, but it's a good habit to stage exactly what you mean (article 5).
3. Fix the last commit's message
The last commit's message has a typo: Add ridg page. Change it to Add ridge page, without creating a new commit.
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 ridg page"
# --- your commands here ---
# --- check ---
git log --format=%s
echo "commits: $(git rev-list --count HEAD)"
Add ridg page
Add lake page
commits: 2
Expected result after solving it:
Add ridge page
Add lake page
commits: 2
Answer
git commit --amend -m "Add ridge page"
git commit --amend replaces the last commit with a new one, here with a new message and the same content, so the number of commits stays at two. Amend only commits you haven't pushed, because it creates a new commit ID (article 6).
4. Add a forgotten file to the last commit
You committed the ridge page but forgot its map, ridge-map.txt. Add the map to that same commit, keeping its message.
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 ridge.txt; git commit -q -m "Add ridge page"
echo "ridge map" > ridge-map.txt
# --- your commands here ---
# --- check ---
git log --format=%s
echo "=== files in the last commit"
git show --format= --name-only HEAD
git status --short
Add ridge page
Add lake page
=== files in the last commit
ridge.txt
?? ridge-map.txt
Expected result after solving it:
Add ridge page
Add lake page
=== files in the last commit
ridge-map.txt
ridge.txt
Hint
Stage the file first, then amend. --no-edit keeps the existing message.
Answer
git add ridge-map.txt
git commit --amend --no-edit
Amending takes whatever is staged and builds a replacement for the last commit from it, so the ridge commit now contains both files and there's still one "Add ridge page" commit (article 6).
5. Ignore build output and logs
The project produces a build/ folder and .log files, which shouldn't be committed. Make Git ignore them, and commit the rule with the message Ignore build output and logs. todo.txt is a real project file: commit it too, in the same commit.
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
mkdir build; echo "binary" > build/site.zip
echo "started" > server.log; echo "crash" > debug.log
echo "add forest page" > todo.txt
# --- your commands here ---
# --- check ---
git status --short
echo "=== tracked"
git ls-files
?? build/
?? debug.log
?? server.log
?? todo.txt
=== tracked
lake.txt
Expected result after solving it:
=== tracked
.gitignore
lake.txt
todo.txt
Hint
Create a .gitignore file with one pattern per line. A trailing / matches folders only.
Answer
printf 'build/\n*.log\n' > .gitignore
git add .gitignore todo.txt
git commit -m "Ignore build output and logs"
build/ ignores the folder and everything in it, and *.log ignores log files in any folder. The .gitignore file itself should be committed, so the whole team shares the rules. After this, git status is clean, because the remaining files are ignored (article 8).
6. Find a commit in history
Find the commit that added forest.txt, and write its subject line, and nothing else, into a new file answer.txt. Don't commit 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 "Forest loop" > forest.txt; git add .; git commit -q -m "Start the woodland section"
echo "Forest loop: 6 km" > forest.txt; git commit -q -am "Add forest distance"
echo "Ridge loop" > ridge.txt; git add .; git commit -q -m "Add ridge page"
# --- your commands here ---
# --- check ---
cat answer.txt 2>/dev/null || echo "no answer.txt yet"
no answer.txt yet
Expected result after solving it:
Start the woodland section
Hint
git log -- <path> limits history to commits that touched a file. --diff-filter=A keeps only commits that added it, and --format=%s prints just the subject.
Answer
git log --diff-filter=A --format=%s -- forest.txt > answer.txt
git log --diff-filter=A -- forest.txt keeps the one commit where the file was added. Note that its message doesn't mention "forest", so searching messages with --grep=forest would have found the wrong commit. git log --follow would also track the file across renames (article 9).
7. Which files changed?
Write the names of the files that differ between the commit two before HEAD (HEAD~2) and HEAD, one per line, into changes.txt. Don't commit it.
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; echo "Ridge loop" > ridge.txt; git add .; git commit -q -m "Add pages"
echo "Lake loop: 5 km" > lake.txt; git commit -q -am "Add lake distance"
mkdir maps; echo "map" > maps/lake.map; git add .; git commit -q -m "Add lake map"
# --- your commands here ---
# --- check ---
cat changes.txt 2>/dev/null || echo "no changes.txt yet"
no changes.txt yet
Expected result after solving it:
lake.txt
maps/lake.map
Hint
git diff <commit> <commit> compares two commits. An option prints only the file names.
Answer
git diff --name-only HEAD~2 HEAD > changes.txt
git diff --name-only A B lists the paths that differ between the two snapshots: lake.txt changed in one commit and maps/lake.map was added in the other. --name-status would also show M and A (article 7).
8. Restore a file from an older commit
The last commit accidentally replaced the lake page with junk. Bring back the version of lake.txt from the commit before it, and commit that with the message Restore lake page. Keep the history: don't remove the bad commit.
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5.4 km, flat" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "asdfgh" > lake.txt; git commit -q -am "Update pages"
# --- your commands here ---
# --- check ---
cat lake.txt
git log --format=%s
asdfgh
Update pages
Add lake page
Expected result after solving it:
Lake loop: 5.4 km, flat
Restore lake page
Update pages
Add lake page
Hint
git restore can take a file's content from any commit with --source.
Answer
git restore --source=HEAD~1 lake.txt
git commit -am "Restore lake page"
git restore --source=HEAD~1 lake.txt writes the older version into the working tree, and committing it records the fix as a new commit, which is safe even if the bad commit was pushed. git checkout HEAD~1 -- lake.txt does the same and also stages it. git revert HEAD gives the same content, since the bad commit changed only this file, but with the message Revert "Update pages". git revert --no-commit HEAD followed by git commit -m "Restore lake page" matches the task exactly (articles 10 and 24).
9. Work on a branch and fast-forward
Create a branch ridge-page, commit a new file ridge.txt (any content) on it with the message Add ridge page, then bring it into main as a fast-forward, and delete the branch.
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 branch
git log --format=%s --graph
* main
* Add lake page
Expected result after solving it:
* main
* Add ridge page
* Add lake page
Answer
git switch -c ridge-page
echo "Ridge loop" > ridge.txt
git add ridge.txt
git commit -m "Add ridge page"
git switch main
git merge ridge-page
git branch -d ridge-page
Because main hadn't moved since the branch was created, git merge simply moves main forward to the branch's commit: a fast-forward, with no merge commit. git branch -d deletes the branch safely, since its commit is now on main (articles 11 and 12).
10. Resolve a merge conflict
Merge the branch fix-distance into main. Both changed the same line of lake.txt. The correct final line is Lake loop: 5.4 km, flat, combining both changes. Finish the merge with the default merge message.
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q -c fix-distance
echo "Lake loop: 5.4 km" > lake.txt; git commit -q -am "Fix lake distance"
git switch -q main
echo "Lake loop: 5 km, flat" > lake.txt; git commit -q -am "Describe lake terrain"
# --- your commands here ---
# --- check ---
cat lake.txt
git status --short
git log --format=%s --graph
Lake loop: 5 km, flat
* Describe lake terrain
* Add lake page
Expected result after solving it:
Lake loop: 5.4 km, flat
* Merge branch 'fix-distance'
|\
| * Fix lake distance
* | Describe lake terrain
|/
* Add lake page
Hint
After the merge stops, edit the file to the content you want, removing the conflict markers, then git add it and commit.
Answer
git merge fix-distance # stops with a conflict
echo "Lake loop: 5.4 km, flat" > lake.txt # the resolved content
git add lake.txt
git commit --no-edit
The merge stops with conflict markers in lake.txt. Writing the combined line, staging the file and committing completes the merge, with two parents. In real work, open the file in an editor or a merge tool, and look at both sides before deciding: git diff during the conflict shows them (article 13).
11. Rebase a feature branch onto main
main has moved on since forest-page was created. Rebase forest-page onto main, so that its two commits come after main's, then fast-forward main to include them.
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" > forest.txt; git add .; git commit -q -m "Add forest page"
echo "Forest loop: 6 km" > forest.txt; git commit -q -am "Add forest distance"
git switch -q main
echo "Ridge loop" > ridge.txt; git add .; git commit -q -m "Add ridge page"
# --- your commands here ---
# --- check ---
git log --format=%s --graph --all
* Add forest distance
* Add forest page
| * Add ridge page
|/
* Add lake page
Expected result after solving it:
* Add forest distance
* Add forest page
* Add ridge page
* Add lake page
Hint
Run the rebase from the feature branch. Afterwards main is behind forest-page in a straight line.
Answer
git switch forest-page
git rebase main
git switch main
git merge --ff-only forest-page
git rebase main replays the branch's commits on top of main's latest commit, giving a straight line. main can then fast-forward, so no merge commit is needed. Rebase only branches nobody else has based work on, because it creates new commits (article 14).
12. Squash fixup commits
The last three commits are one piece of work, plus two typo fixes. Combine them into a single commit with the message Add forest page, keeping the final content.
git init -q trail-guide && cd trail-guide
echo "Lake loop" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Forset loop: 6 km" > forest.txt; git add .; git commit -q -m "Add forest page"
echo "Forest loop: 6 km" > forest.txt; git commit -q -am "fix typo"
echo "Forest loop: 6 km, shady" > forest.txt; git commit -q -am "fix another typo"
# --- your commands here ---
# --- check ---
git log --format=%s
cat forest.txt
fix another typo
fix typo
Add forest page
Add lake page
Forest loop: 6 km, shady
Expected result after solving it:
Add forest page
Add lake page
Forest loop: 6 km, shady
Hint
Interactive rebase works, but needs an editor. Another way: move the branch back three commits while keeping the changes staged, then commit once.
Answer
git reset --soft HEAD~3
git commit -m "Add forest page"
git reset --soft HEAD~3 moves the branch back past the three commits but leaves their combined changes staged, so a single commit records the final content. With interactive rebase, you'd mark the second and third commits as fixup. Without an editor, GIT_SEQUENCE_EDITOR="sed -i '2,3s/^pick/fixup/'" git rebase -i HEAD~3 does it (article 15).
13. Take one commit from another branch
The experiment branch contains one commit, Fix lake distance, that main needs now. Bring just that commit into main, without merging the rest of the branch.
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q -c experiment
echo "Cave loop" > cave.txt; git add .; git commit -q -m "Try a cave page"
echo "Lake loop: 5.4 km" > lake.txt; git commit -q -am "Fix lake distance"
echo "Cave loop: 3 km" > cave.txt; git commit -q -am "Add cave distance"
git switch -q main
# --- your commands here ---
# --- check ---
git log --format=%s main
cat lake.txt
ls
Add lake page
Lake loop: 5 km
lake.txt
Expected result after solving it:
Fix lake distance
Add lake page
Lake loop: 5.4 km
lake.txt
Hint
experiment~1 names the commit before the branch's tip. git log --oneline experiment shows the commits and their IDs.
Answer
git cherry-pick experiment~1
git cherry-pick applies one commit's changes to the current branch as a new commit, with the same message. cave.txt stays on experiment. Use git cherry-pick -x to record where the commit came from (article 16).
Common questions
My solution is different from the answer, but the check prints the same. Is it right?
Yes. Most tasks have several correct solutions, such as git checkout HEAD~1 -- lake.txt instead of git restore --source, or interactive rebase instead of reset --soft. What matters is the resulting state.
I made a mess of an exercise. How do I start again? Just run the block again: the setup builds a fresh repository each time. On your own machine, delete the folder and paste the setup again.
Why do commit IDs in my output differ from the ones in the answers? The sandbox fixes the author, the email and every date, so IDs repeat from run to run there. On your own machine, the name, email and time are different, so the IDs are too. The checks print messages and file contents for that reason.
Summary
- Stage exactly what belongs in a commit, and amend only unpushed commits.
.gitignorekeeps generated files out, andgit log -- <path>,--diff-filterandgit diff --name-onlyanswer most "which commit?" and "which files?" questions.git restore --sourcebrings back an old version of a file. Branches, merges, rebases and cherry-picks move work between lines of development.
Practice questions
Which exercise's task could also be solved with git revert HEAD, and why?
Answer
Exercise 8, restoring the lake page. The bad commit changed only lake.txt, so reverting it restores the previous content in a new commit. To get the required message, use git revert --no-commit HEAD, then git commit -m "Restore lake page". If the bad commit had also changed other files that should be kept, restoring only lake.txt would be the right approach.
In exercise 11, why can main fast-forward after the rebase but not before?
Answer
Before the rebase, main and forest-page had each moved on from their common commit, so main couldn't simply move forward to forest-page. After the rebase, forest-page starts from main's latest commit, so main is its ancestor, and moving main forward along the branch is a fast-forward.
When these feel easy, continue with the intermediate exercises, which add remotes, undoing, the reflog, and investigating history.
Previous: Fixing Common Git Problems: A Troubleshooting Guide

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.