Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if directory is under git control

Tags:

git

python

How can I tell if a given directory is part of a git respository?

(The following is in python, but bash or something would be fine.)

os.path.isdir('.svn') 

will tell you if the current directory is controlled by Subversion. Mercurial and Git just have a .hg/.git at the top of the repository, so for hg I can use

os.system('hg -q stat 2> /dev/null > /dev/null') == 0) 

but git status returns a nonzero (error) exit status if nothing's changed.

Is iterating up the path looking for .git myself the best I can do?

like image 435
Grumdrig Avatar asked Jan 11 '10 20:01

Grumdrig


People also ask

How do you know if a certain directory is a git repository?

So how can you tell if a directory is within a git repository? Exit code of 0 means it's a git repository. Any other code (e.g., 128 ) means it's not.

Is .git folder tracked?

No, there isn't. But you can store in git a text files with the 2 or 3 commands you use to reconfigure each repository. You can make it a .


1 Answers

Just found this in git help rev-parse

git rev-parse --is-inside-work-tree 

prints true if it is in the work tree, false if it's in the '.git' tree, and fatal error if it's neither. Both 'true' and 'false' are printed on stdout with an exit status of 0, the fatal error is printed on stderr with an exit status of 128.

like image 63
falstro Avatar answered Oct 14 '22 01:10

falstro