Skip to content

Instantly share code, notes, and snippets.

@aviflax
Created August 18, 2020 18:30
Show Gist options
  • Save aviflax/23b05327559c26f64d79618a5ef99872 to your computer and use it in GitHub Desktop.
Save aviflax/23b05327559c26f64d79618a5ef99872 to your computer and use it in GitHub Desktop.
Running a shell script from anywhere in a Git repo
#!/bin/sh
# This can be run from anywhere within the repo.
set -eu
# This is the trick
repo_root=$(git rev-parse --show-toplevel)
# Now that we have the absolute path to the repo root, we can just use it
# in the rest of the script -- no need to worry about relative paths, or
# about whether they’re relative to the user’s cwd or to the location of
# the script file or whatever:
some_path="$repo_root/some/path/to/something/from/the/root"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment