Is there a way to check if a git repo exists using a bash or csh script? I know that we can use git ls-remote <repository>
to check the existence of the remote repo. But I'd like to somehow do this programatically in a shell script.
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.
You can use ls-remote : $ git ls-remote [email protected]:github/markup.
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. If you pass it the -a flag, it will display hidden files.
git ls-remote is one unique command allowing you to query a remote repo without having to clone/fetch it first. It will list refs/heads and refs/tags of said remote repo.
You can write the command in the script itself:
~$ git ls-remote <existing_repo> -q
~$ echo $?
0
0 means that the repo was found, otherwise you'll get a non-zero value.
-q
is for:
quiet (Do not print remote URL to stderr.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With