Git Labs

Updated · 9 min read
mediumGit 2.53gitlabspracticehands-onassessment

Reading about first aid and practicing on a dummy are not the same thing. The day someone needs help, only the practice counts. These labs are the dummy for Git: bigger tasks than a lesson's challenge, done in a real or practice repository, each with a list of checks that tell you when you are done. Labs 1 to 5 close each level of the course. Labs 6 and 7 are drills for the two situations people panic about most: undoing a mistake, and commits in the wrong place.

How to Do a Lab

  1. Read the whole lab first, including the "Done when" list, so you know where you are heading.
  2. Work in a practice repository or a copy of a real one, never on a shared project you can't afford to break.
  3. Check every item in "Done when" with the commands given. A lab is done only when all of them are true.
  4. Stuck? Each step names the lesson that explains it.

Lab 1: Put Your Own Project Under Git

Level 1. Take one of your own projects (a Java exercise folder is perfect) and give it a clean Git history.

  1. git init, set user.name and user.email if you haven't (Git Config: Setting Up Git).
  2. Write a .gitignore before the first git add, for build output, IDE files and secrets (Git .gitignore).
  3. Commit the project in small, logical commits with clear subjects, using git add -p where a file holds two changes (Writing Good Commit Messages).
  4. Make a deliberate mistake and undo it three ways: git restore, git reset --soft HEAD~1 and git revert (How to Undo Changes in Git).

Done when:

  • git status says "nothing to commit, working tree clean"
  • git ls-files lists no build output (target/, build/, *.class) and no IDE files (.idea/, *.iml)
  • git log --oneline shows ten or more commits, each subject in the imperative mood and under about 60 characters
  • You can explain what each of the three undo commands did to the branch, the staging area and the files
Terminal
git status
git ls-files | grep -E "target/|build/|\.class$|\.idea/|\.iml$" || echo "No build or IDE files tracked"
git log --oneline | wc -l

Lab 2: A Team Project Through Pull Requests

Level 2. With a friend, or alone with two clones of one repository, build a small project the way teams do.

  1. Create a repository on GitHub, GitLab or Bitbucket, and protect main so changes arrive by pull request (Getting Started on GitHub, GitLab and Bitbucket).
  2. Each person works on short feature branches and opens pull requests; review each other's (Pull Requests and Merge Requests).
  3. Change the same line on two branches on purpose, merge one, and resolve the conflict in the other (Resolving Merge Conflicts).
  4. Release: tag v1.0.0 with an annotated tag and push it (Git Tag).

Done when:

  • At least three pull requests are merged into main
  • One real conflict was resolved, and the result is correct
  • git ls-remote --tags origin lists v1.0.0
  • Both clones show the same git log --oneline --graph --all after git fetch

Lab 3: Clean Up, Debug and Recover

Level 3.

  1. On a branch, make about eight messy commits ("wip", "fix typo", "really fix"), then turn them into three clear commits with an interactive rebase (Interactive Rebase).
  2. In a history of at least 20 commits, introduce a bug in one of them and find it with git bisect run and a small test script (Git Bisect).
  3. Delete a branch with unmerged commits (git branch -D), then get it back from the reflog (Git Reflog).

Done when:

  • git log --oneline main..<branch> shows exactly three commits with clear messages
  • git bisect run named the commit you broke, and git bisect reset returned you to your branch
  • The deleted branch exists again with its commits, checked with git log --oneline <branch>

Lab 4: Internals and Team Setup

Level 4.

  1. Build a commit using plumbing commands only: hash-object -w, update-index, write-tree, commit-tree and update-ref (Build a Commit by Hand).
  2. Drop a stash with git stash drop, then recover it with git fsck --unreachable (Git Recovery: Getting Lost Work Back).
  3. Share a pre-commit hook through the repository with core.hooksPath (Sharing Hooks with a Team).
  4. Sign your commits with an SSH key and verify them locally (Signing Commits and Tags).

Done when:

  • git log -1 on your branch shows the hand-built commit, and git cat-file -p HEAD shows its tree and parent
  • The dropped stash's changes are back in your working folder
  • A commit that breaks the hook's rule is refused, on a fresh clone too, after setting core.hooksPath
  • git log --format="%G? %s" -3 shows G for your signed commits

Lab 5: Contribute to Open Source

Level 5. Find a public project you use, and open a pull request. A documentation fix counts.

  1. Read the project's CONTRIBUTING file, fork it, and clone your fork (Forking a Repository).
  2. Add the original as upstream, create a branch from the latest upstream/main, and make one focused change (Working with Several Remotes).
  3. Push to your fork, open a pull request, and answer review comments with follow-up commits or a clean force push with --force-with-lease (Code Review and Pull Request Etiquette).

Done when:

  • The pull request is open (under review) or merged
  • Its commits have clear messages and change only what the pull request describes
  • Your fork's main is up to date with upstream/main

Lab 6: Six "Oops" Situations

A drill for undoing. Each situation starts with the commands below, run in a new, empty folder; then fix it, and check the result. Open the solution only after trying. The lesson for every fix is How to Undo Changes in Git.

The six situations:

  1. You edited fees.txt and want the committed version back.
  2. You staged secret.txt by mistake and want it unstaged, but kept on disk.
  3. The last commit's message has a typo (not pushed).
  4. The last commit should have included one more file (not pushed).
  5. The last commit was a mistake and is already pushed. Undo it without rewriting history.
  6. You ran git reset --hard HEAD~1 and want the commit back.

Set up all six at once:

Terminal
mkdir oops && cd oops
git init --quiet
echo 'Term fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the lat fee"
echo "broken line" >> fees.txt
echo "password=1234" > secret.txt
git add secret.txt
git status --short
git log --oneline
Output
 M fees.txt
A  secret.txt
768436c (HEAD -> main) Add the lat fee
5c7eaa5 Add the term fee

Solutions for situations 1 to 4:

Show solution
Terminal
git restore fees.txt
git restore --staged secret.txt
git commit --quiet --amend -m "Add the late fee"
echo 'Exam fee: $800' > exam.txt
git add exam.txt
git commit --quiet --amend --no-edit
git status --short
git log --oneline --stat -1
Output
?? secret.txt
a6e7dcf (HEAD -> main) Add the late fee
 exam.txt | 1 +
 fees.txt | 1 +
 2 files changed, 2 insertions(+)

Explanation: git restore dropped the broken line, git restore --staged unstaged secret.txt (it is untracked again: add it to .gitignore), and two amends fixed the message and added exam.txt to the same commit. Amending is fine here only because nothing was pushed.

Solutions for situations 5 and 6 (imagine "Add the late fee" was pushed, then lost):

Show solution
Terminal
git revert --no-edit HEAD > /dev/null
git log --oneline
git reset --quiet --hard HEAD~1
git reset --quiet --hard "HEAD@{1}"
git log --oneline
Output
4892496 (HEAD -> main) Revert "Add the late fee"
a6e7dcf Add the late fee
5c7eaa5 Add the term fee
4892496 (HEAD -> main) Revert "Add the late fee"
a6e7dcf Add the late fee
5c7eaa5 Add the term fee

Explanation: git revert added a new commit that undoes "Add the late fee" without removing it from history, the safe choice once pushed. Then git reset --hard HEAD~1 threw that revert away, and git reset --hard HEAD@{1}, the position before the reset in the reflog, brought it straight back.

Done when:

  • You fixed all six without looking at the solutions, and git status matches the expected state each time
  • You can say, for each, whether the fix rewrites history and whether it is safe after a push

Lab 7: Five "Wrong Place" Recipes

A drill for moving commits. Start with this repository, then fix each situation in turn. The lesson is Fixing Commits in the Wrong Place.

  1. Two commits that belong on a new branch feature/exam were made on main.
  2. A commit on feature/exam belongs on main too (a bug fix needed in both).
  3. You started working on main but haven't committed yet, and want the changes on a new branch feature/bus.
Terminal
cd ..
mkdir wrong-place && cd wrong-place
git init --quiet
echo 'Term fee: $12,000' > fees.txt
git add fees.txt
git commit --quiet -m "Add the term fee"
echo 'Exam fee: $800' > exam.txt
git add exam.txt
git commit --quiet -m "Add the exam fee"
echo "Exam fee due in March" >> exam.txt
git commit --quiet -am "Add the exam due date"
git log --oneline
Output
fb8939f (HEAD -> main) Add the exam due date
572db0b Add the exam fee
cb23f63 Add the term fee

Solutions:

Show solution
Terminal
git branch feature/exam
git reset --quiet --hard HEAD~2
git switch --quiet feature/exam
sed -i.bak 's/$12,000/$12,500/' fees.txt && rm fees.txt.bak
git commit --quiet -am "Fix the term fee"
git switch --quiet main
git cherry-pick -x feature/exam > /dev/null
echo 'Bus fee: $1,500' > bus.txt
git switch --quiet -c feature/bus
git add bus.txt
git commit --quiet -m "Add the bus fee"
git log --oneline --graph --all --format="%s%d"
Output
* Add the bus fee (HEAD -> feature/bus)
* Fix the term fee (main)
| * Fix the term fee (feature/exam)
| * Add the exam due date
| * Add the exam fee
|/
* Add the term fee

Explanation: (1) git branch feature/exam kept the two commits on a new branch, and reset --hard HEAD~2 took main back. (2) The fix was made on feature/exam and copied to main with cherry-pick -x, so both have it. (3) Uncommitted changes travel with you when you switch, so git switch -c feature/bus moved them to the new branch before committing.

The two remaining recipes from the lesson, a commit that went to the wrong existing branch, and a pushed commit on the wrong branch, follow the same pattern: copy with cherry-pick, then remove with reset (unpushed) or revert (pushed).

Done when:

  • Every commit ends up on the right branch, checked with git log --oneline --graph --all
  • The branch that had the wrong commits is back where it started, or reverted if it was pushed

Was this tutorial helpful?