Last active
February 25, 2020 15:09
-
-
Save jakub-g/375bff2570303d3e9b623669a17eee2f to your computer and use it in GitHub Desktop.
go to root folder of git repo from a bash script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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