I have a file that I think might be inside a git repo. I am currently in my home directory. How can I get the top level directory of the repo without changing my current working directory?
If I am inside the repo I can run to get the root directory.
(~/code/dir1) $ git rev-parse --show-toplevel
Effectively I want to be able to use a file to find the root of the git directory directly.
(~) $ find . -name "specific_file.py"
Where the important parts of the tree are:
~/code/dir1/.git
~/code/dir1/files/more_files/specific_file.py
Is there a git way of doing this, or is generic shell manipulations the best way of doing this?
I know I can do:
(~) $ cd $(dirname $(find . -name "specific_file.py"))
(~/code/dir1/files/more_files) $ git rev-parse --show-toplevel
~/code/dir1
(~/code/dir1/files/more_files) $ cd -
If I try without being inside the repo I get the message:
Fatal '~/code/dir1/files/more_file/specific_file.py' is outside repository
If I try and set the git directory to being further down the tree:
git --git-dir=$(dirname $(find . -name "specific_file.py")) rev-parse --show-toplevel
It tells me that I'm not working in a git directory.
I have also tried playing with the working tree, but that doesn't seem to be what I'm after as its looking directly for a /.git directory directly on the path.