Skip to content

Instantly share code, notes, and snippets.

@jakub-g
Last active February 25, 2020 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakub-g/375bff2570303d3e9b623669a17eee2f to your computer and use it in GitHub Desktop.
Save jakub-g/375bff2570303d3e9b623669a17eee2f to your computer and use it in GitHub Desktop.
go to root folder of git repo from a bash script
#!/bin/bash
# first, switch to the directory of where the currently executing script is located
cd "$( dirname "${BASH_SOURCE[0]}" )"
# now, find the git repo root, and switch to that directory
cd "./"$(git rev-parse --show-cdup)
# now, run stuff from the git root
./somefolder/do-stuff.sh
# The advantages of doing the first two steps as above is:
# 1. Regardless from where the script is executed, it will run exactly in the same way
# 2. The script is future-proof: when it gets moved to a subfolder, it will still
# work the same (provided that the scripts it calls inside are not moved);
# whereas doing things like "cd ../" is not future-proof; if you move the script
# to a subfolder, you'd have to change it to "cd ../../"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment