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
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 mainRun git -C "$workdir/alice" status --short; there should be no output; the remote also has main.
Task 2: Second Developer Submission
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" pushAlice's origin/main will not update spontaneously:
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:
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 --shortIn Bob's file, select the final value that meets the requirements and submit:
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" pushTask 4: Verify History
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-danglingBoth 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.nameanduser.emailin the current exercise repository. - Non-fast-forward push: after fetching, examine the commit graph; do not force immediately.
git pullasks 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 --abortbefore 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, thengit restore -- path.
Cleanup
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.