Git Basics: Validate Revisions and Inspect Repository Context
Shell scripts should validate a requested revision before using it in later Git commands. git rev-parse can require a commit object and report the current checkout context. The fixture accepts a tag, rejects a missing revision, and inspects a nested directory.
Prepare a scratch directory
Tested on Linux with Git 2.52.0 and GNU Bash 5.2.37. You need the named runtime, Bash, a text editor, and the standard mktemp, rm, and rmdir utilities. The script creates a second temporary fixture directory and removes only that directory through its exit trap. Run it in a normal Bash terminal, outside any Git hook or Git wrapper. No global Git configuration is changed.
Keep using the same terminal. Stop if setup fails, before saving the source:
start_dir="$PWD"
work_dir="$(mktemp -d)" || exit 1
cd -- "$work_dir" || exit 1Run the example
Save this as demonstrate-rev-parse.sh:
#!/usr/bin/env bash
set -euo pipefail
export LC_ALL=C
fixture_dir="$(mktemp -d)"
trap 'rm -rf -- "$fixture_dir"' EXIT
export GIT_CONFIG_NOSYSTEM=1
export GIT_CONFIG_GLOBAL=/dev/null
unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE
git init -q -b main "$fixture_dir/repo"
cd -- "$fixture_dir/repo"
git config user.name 'Practice Author'
git config user.email 'practice@example.invalid'
mkdir src
printf 'sample\n' > src/app.txt
git add src/app.txt
git commit -qm 'Project root'
git tag -a v1 -m 'Version one'
requested='v1'
resolved="$(git rev-parse --verify --end-of-options "${requested}^{commit}")"
test "$resolved" = "$(git rev-parse HEAD)"
printf 'Tag resolves to current commit: yes\n'
requested='missing-reference'
if git rev-parse --verify --quiet --end-of-options "${requested}^{commit}" > /dev/null; then
exit 1
else
printf 'Missing revision rejected\n'
fi
cd src
printf 'Inside work tree: %s\n' "$(git rev-parse --is-inside-work-tree)"
printf 'Prefix from repository root: %s\n' "$(git rev-parse --show-prefix)"
root="$(git rev-parse --show-toplevel)"
test -f "$root/src/app.txt"
printf 'Repository root located: yes\n'Run it:
bash demonstrate-rev-parse.shObserved output:
Tag resolves to current commit: yes
Missing revision rejected
Inside work tree: true
Prefix from repository root: src/
Repository root located: yesRead the result
--verify requires one resolvable object expression. Appending ^{commit} also requires that the expression resolves to a commit, peeling an annotated tag when necessary. --end-of-options stops a caller-supplied value from being interpreted as another option. The nested-directory queries describe where the command is running without printing a machine-specific absolute path.
Apply it carefully
Revision syntax is more expressive than branch names: validation as a commit does not enforce a policy such as “only release tags are allowed.” Apply any name allowlist separately. Quote variables in shell commands, and distinguish a missing revision from environmental failures in a production script. --show-toplevel applies to a working-tree checkout; bare repositories need a different context check.
The official reference describes the API and its options. The checks above cover the specific fixture, not every possible input or environment.
Clean up
Remove the source and then the empty practice directory:
rm -- demonstrate-rev-parse.sh
cd -- "$start_dir" || exit 1
rmdir -- "$work_dir"
unset start_dir work_dirIf the directory is not empty, inspect the remaining files rather than deleting unknown contents.
Acknowledgements
This post was created with assistance from AI.