Skip to content

VOL 00 / CH 03 / LESSON 07

3.7 Git Comprehensive Exercise ​

Prerequisites: Pages 1 through 6 of this chapter

Estimated time: 25–35 minutes

Exercise environment: Bash and Git 2.28 or higher; no network access, no global configuration changes

You are ready to continue when: you can use two local clones to commit, resolve conflicts, push, fetch, and merge

The concepts are in place. Put a remote repository and two clones in one temporary directory, then let Alice and Bob complete a reproducible collaboration. This lab runs independently even if an earlier sequence was interrupted.

Task 1: Create Remote and First Developer ​

bash
workdir=$(mktemp -d)
git init --bare -b main "$workdir/remote.git"
git init -b main "$workdir/alice"

git -C "$workdir/alice" config user.name Alice
git -C "$workdir/alice" config user.email alice@example.invalid
printf '%s\n' 'line=base' > "$workdir/alice/config.txt"
git -C "$workdir/alice" add config.txt
git -C "$workdir/alice" commit -m 'Add base configuration'
git -C "$workdir/alice" remote add origin "$workdir/remote.git"
git -C "$workdir/alice" push -u origin main

Run git -C "$workdir/alice" status --short; there should be no output; the remote also has main.

Task 2: Second Developer Submission ​

bash
git clone "$workdir/remote.git" "$workdir/bob"
git -C "$workdir/bob" config user.name Bob
git -C "$workdir/bob" config user.email bob@example.invalid
printf '%s\n' 'from Bob' > "$workdir/bob/BOB.md"
git -C "$workdir/bob" add BOB.md
git -C "$workdir/bob" commit -m 'Add Bob note'
git -C "$workdir/bob" push

Alice's origin/main will not update spontaneously:

bash
git -C "$workdir/alice" fetch origin
git -C "$workdir/alice" log --oneline --left-right main...origin/main
git -C "$workdir/alice" merge --ff-only origin/main
test -f "$workdir/alice/BOB.md"

Task 3: Create and Resolve Conflicts ​

Alice and Bob modify the same line from the same baseline:

bash
printf '%s\n' 'line=alice' > "$workdir/alice/config.txt"
git -C "$workdir/alice" commit -am 'Alice changes configuration'
git -C "$workdir/alice" push

printf '%s\n' 'line=bob' > "$workdir/bob/config.txt"
git -C "$workdir/bob" commit -am 'Bob changes configuration'
git -C "$workdir/bob" fetch origin
merge_status=0
git -C "$workdir/bob" merge origin/main || merge_status=$?
test "$merge_status" -eq 1
git -C "$workdir/bob" status --short

In Bob's file, select the final value that meets the requirements and submit:

bash
printf '%s\n' 'line=alice+bob' > "$workdir/bob/config.txt"
git -C "$workdir/bob" add config.txt
git -C "$workdir/bob" commit -m 'Resolve configuration conflict'
git -C "$workdir/bob" push

Task 4: Verify History ​

bash
git -C "$workdir/alice" fetch origin
git -C "$workdir/alice" merge --ff-only origin/main
test "$(git -C "$workdir/alice" rev-parse HEAD)" = "$(git -C "$workdir/bob" rev-parse HEAD)"
git -C "$workdir/bob" status --short
git -C "$workdir/bob" log --graph --oneline --decorate --all
git -C "$workdir/bob" fsck --no-dangling

Both clones should have the same HEAD and no uncommitted changes. The graph should include both developers' changes and a merge; fsck should report no object errors.

Troubleshooting ​

  • Submit prompt: identity unknown - only set user.name and user.email in the current exercise repository.
  • Non-fast-forward push: after fetching, examine the commit graph; do not force immediately.
  • git pull asks you to choose an integration strategy: explicitly use --ff-only, --rebase, or follow team conventions for merge.
  • Conflicts not yet resolved: check the unmerged paths listed in git status, edit, test, add, and commit.
  • Want to cancel a merge: use git merge --abort before committing; it's best to keep the workspace clean before starting a merge.
  • Correcting uncommitted changes: first check git diff, confirm they are not needed, then git restore -- path.

Cleanup ​

bash
cd
ls -ld -- "$workdir"
rm -r -- "$workdir"

Do not replace this cleanup command with a real repository path. Confirm you can explain the origin and content of $workdir before executing it.

Once the HEAD comparison passes and the temporary directory is removed, the collaboration lab is complete. Next, prepare a debugger and inspect program state with breakpoints.

→ Next: Prepare a Debugging Environment

Built with VitePress | Software Systems Atlas