Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if directory is git repository (without having to cd into it)

Tags:

git

linux

bash

What's a shell command I can use to, using the full directory path, determine whether or not a given directory is a git repository? Specifically, I'd like to be able to do this without being in the directory, and without having to cd into it. I'd also like to be able to do it with a command that returns a simple "true" or "false" (much the way that rev-parse --is-inside-work-tree does), but it's not a requirement.

like image 963
rurouniwallace Avatar asked Sep 15 '16 18:09

rurouniwallace


People also ask

How do I know if a directory is 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.

How do I find my git repository directory?

git directory is a configuration file for git. Use the terminal to display the . git directory with the command ls -a . The ls command lists the current directory contents and by default will not show hidden files.

What directory is git repository?

The Local Repository is the . git/ subdirectory inside the Working Directory.

How can you track an empty directory using git?

To get Git to recognize an empty directory, the unwritten rule is to put a file named . gitkeep in it. Git will see the . gitkeep file in the otherwise empty folder and make that folder part of the next commit or push.


1 Answers

Use git -C <path> rev-parse. It will return 0 if the directory at <path> is a git repository and an error code otherwise.

Further Reading:

  • rev-parse
  • -C <path>
like image 90
ypnos Avatar answered Oct 14 '22 20:10

ypnos