Software engineering · Local Git lab
Two edits, one commit: a first Git review
You update an approved pickup time and jot down a bike-rack idea that is not ready to share. Both changes live on your machine, but only the pickup change belongs in the next commit. Git gives you three places to look:
- Working tree: the files you can edit.
- Staging area: the versions of files prepared for the next commit. Git also calls it the index.
- Commit history: the snapshots already recorded. HEAD means the current commit in this exercise.
Use a Mac or Unix-like terminal with Git already available at /usr/bin/git. If Git is unavailable, stop; this exercise does not install anything. It uses a fictional identity and no remote.
Open an isolated practice shell
Copy this whole block into your terminal. It starts a second shell without carrying over any environment variables from the first one. It also gives Git a private home folder beside, not inside, the practice repository.
/usr/bin/env -i PATH=/usr/bin:/bin LC_ALL=C /bin/sh -c '
test -x /usr/bin/git || exit 1
scope=$(mktemp -d /tmp/library-git.XXXXXX) || exit 1
mkdir "$scope/home" "$scope/repo" || exit 1
cd "$scope/repo" || exit 1
export HOME="$scope/home"
export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null
export GIT_ATTR_NOSYSTEM=1 GIT_TERMINAL_PROMPT=0
export GIT_ALLOW_PROTOCOL="" GIT_PROTOCOL_FROM_USER=0
export GIT_PAGER=cat GIT_EDITOR=/usr/bin/false
export GIT_SEQUENCE_EDITOR=/usr/bin/false
export PS1="git-practice> "
git init -q --template= || exit 1
git rev-parse --show-toplevel || exit 1
exec /bin/sh -i
'Continue only if it prints a path ending in library-git.XXXXXX/repo (the six characters will vary) and the prompt changes to git-practice>. If either check fails, do not run the remaining blocks. Stay inside that shell and folder for the whole exercise. This setup ignores system and global Git settings without changing them. It also starts Git without templates, disables Git network protocols, and does not ask for a password, editor or pager.
Make a starting snapshot
printf '%s\n' 'Pickup window: 10:00-12:00' > pickup.txt
printf '%s\n' 'Bike rack location: undecided' > ideas.txt
git add -- pickup.txt ideas.txt
git -c user.name='Library Practice' -c user.email="$(printf '%s@%s' practice example.invalid)" \
-c commit.gpgSign=false -c core.hooksPath=/dev/null \
commit -qm 'Record starting notes'
git status --shortThe last command prints nothing when the starting snapshot is saved and there are no pending changes. The -c settings apply only to that commit command. They use a practice identity and do not request a signing key or run repository hooks.
Make two edits and stage only the approved one
printf '%s\n' 'Pickup window: 10:00-13:00' > pickup.txt
printf '%s\n' 'Bike rack location: try the south wall' > ideas.txt
git status --shortYou should see:
M ideas.txt
M pickup.txtIn the short status, the first column compares the staging area with the current commit. The second compares the working tree with the staging area. Here both M marks are in the second column: both files are modified, neither is staged.
git add -- pickup.txt
git status --short
git --no-pager diff
git --no-pager diff --staged
git --no-pager log -1 --format=%sNow the short status is:
M ideas.txt
M pickup.txtgit diffshows the unstaged change: the bike-rack line inideas.txt.git diff --stagedshows what the next ordinary commit will record: the pickup line inpickup.txt. Check for-Pickup window: 10:00-12:00and+Pickup window: 10:00-13:00.git logstill shows the subject Record starting notes. Staging did not create a commit.
If the staged diff shows anything other than the intended pickup change, stop before committing. When you edit a file again after staging, Git does not automatically replace the staged version. Run both diffs again if you are unsure.
Commit, then check what actually went in
git -c user.name='Library Practice' -c user.email="$(printf '%s@%s' practice example.invalid)" \
-c commit.gpgSign=false -c core.hooksPath=/dev/null \
commit -qm 'Extend pickup window'
git --no-pager log -2 --format=%s
git status --short
git --no-pager diff --staged
git --no-pager show HEAD:pickup.txt
git --no-pager show HEAD:ideas.txt
cat ideas.txtHistory now lists Extend pickup window above Record starting notes. Short status still shows M ideas.txt, and the staged diff is empty. The committed pickup file ends at 13:00. The committed ideas file still says undecided; your editable copy says try the south wall. The draft was neither committed nor erased. A local commit records a snapshot but does not send it anywhere. It is not a substitute for a backup.
Try it: A teammate says, “I see an empty staged diff, so there is nothing left to review.” Which command gives the quickest counterexample?
Check your answer
git diff shows the unstaged bike-rack change. git status --short names the file but does not show what changed; git log lists recorded commits, not the edits waiting outside them.
When finished, type exit to return to your regular shell. It does not delete the temporary exercise or change your regular Git settings.