Skip to content

Instantly share code, notes, and snippets.

@swarminglogic swarminglogic/git-root
Last active Jun 14, 2019

What would you like to do?
git-root: finds git root directory without using git executable (POSIX compliant shell script)
#$1: Path to child directory
git_root_recurse_parent() {
# Check if cwd is a git root directory
if [ -d .git/objects -a -d .git/refs -a -f .git/HEAD ] ; then
return 0
# Check if recursion should end (typically if cwd is /)
if [ "${1}" = "$(pwd)" ] ; then
return 1
# Check parent directory in the same way
local cwd=$(pwd)
cd ..
git_root_recurse_parent "${cwd}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.