Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save petergi/9777350324e257687232c528d97eec58 to your computer and use it in GitHub Desktop.

Select an option

Save petergi/9777350324e257687232c528d97eec58 to your computer and use it in GitHub Desktop.
This is a neat little piece of shell judo. It’s doing capability detection against a Git repository and then falling back gracefully. Conceptually, this is the Unix philosophy in action: attempt the simplest thing, and if reality disagrees, degrade politely. In other words: optimistic, defensive, and mildly smug—in the best possible way.
# Try to show the contents of docs/ from the master branch.
# - git show works for files, but may fail for directories.
# - Suppress errors and limit output to 20 lines.
# - If that fails, fall back to listing files under docs/ instead.
# - Always show only the first 20 results to keep output predictable.
git show master:docs/ 2>/dev/null | head -20 \
|| git ls-tree -r master --name-only | grep '^docs/' | head -20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment