Git Labs
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
- Read the whole lab first, including the "Done when" list, so you know where you are heading.
- Work in a practice repository or a copy of a real one, never on a shared project you can't afford to break.
- Check every item in "Done when" with the commands given. A lab is done only when all of them are true.
- 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.
git init, setuser.nameanduser.emailif you haven't (Git Config: Setting Up Git).- Write a
.gitignorebefore the firstgit add, for build output, IDE files and secrets (Git .gitignore). - Commit the project in small, logical commits with clear subjects, using
git add -pwhere a file holds two changes (Writing Good Commit Messages). - Make a deliberate mistake and undo it three ways:
git restore,git reset --soft HEAD~1andgit revert(How to Undo Changes in Git).
Done when:
-
git statussays "nothing to commit, working tree clean" -
git ls-fileslists no build output (target/,build/,*.class) and no IDE files (.idea/,*.iml) -
git log --onelineshows 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
git status
git ls-files | grep -E "target/|build/|\.class$|\.idea/|\.iml$" || echo "No build or IDE files tracked"
git log --oneline | wc -lLab 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.
- Create a repository on GitHub, GitLab or Bitbucket, and protect
mainso changes arrive by pull request (Getting Started on GitHub, GitLab and Bitbucket). - Each person works on short feature branches and opens pull requests; review each other's (Pull Requests and Merge Requests).
- Change the same line on two branches on purpose, merge one, and resolve the conflict in the other (Resolving Merge Conflicts).
- Release: tag
v1.0.0with 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 originlistsv1.0.0 - Both clones show the same
git log --oneline --graph --allaftergit fetch
Lab 3: Clean Up, Debug and Recover
Level 3.
- 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).
- In a history of at least 20 commits, introduce a bug in one of them and find it with
git bisect runand a small test script (Git Bisect). - 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 runnamed the commit you broke, andgit bisect resetreturned 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.
- Build a commit using plumbing commands only:
hash-object -w,update-index,write-tree,commit-treeandupdate-ref(Build a Commit by Hand). - Drop a stash with
git stash drop, then recover it withgit fsck --unreachable(Git Recovery: Getting Lost Work Back). - Share a
pre-commithook through the repository withcore.hooksPath(Sharing Hooks with a Team). - Sign your commits with an SSH key and verify them locally (Signing Commits and Tags).
Done when:
-
git log -1on your branch shows the hand-built commit, andgit cat-file -p HEADshows 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" -3showsGfor 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.
- Read the project's
CONTRIBUTINGfile, fork it, and clone your fork (Forking a Repository). - Add the original as
upstream, create a branch from the latestupstream/main, and make one focused change (Working with Several Remotes). - 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
mainis up to date withupstream/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:
- You edited
fees.txtand want the committed version back. - You staged
secret.txtby mistake and want it unstaged, but kept on disk. - The last commit's message has a typo (not pushed).
- The last commit should have included one more file (not pushed).
- The last commit was a mistake and is already pushed. Undo it without rewriting history.
- You ran
git reset --hard HEAD~1and want the commit back.
Set up all six at once:
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 M fees.txt
A secret.txt
768436c (HEAD -> main) Add the lat fee
5c7eaa5 Add the term feeSolutions for situations 1 to 4:
Show solution
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?? 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
git revert --no-edit HEAD > /dev/null
git log --oneline
git reset --quiet --hard HEAD~1
git reset --quiet --hard "HEAD@{1}"
git log --oneline4892496 (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 feeExplanation: 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 statusmatches 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.
- Two commits that belong on a new branch
feature/examwere made onmain. - A commit on
feature/exambelongs onmaintoo (a bug fix needed in both). - You started working on
mainbut haven't committed yet, and want the changes on a new branchfeature/bus.
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 --onelinefb8939f (HEAD -> main) Add the exam due date
572db0b Add the exam fee
cb23f63 Add the term feeSolutions:
Show solution
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"* 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 feeExplanation: (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?