Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a git repo exists in a shell script

Tags:

git

bash

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.

like image 866
BigBrownBear00 Avatar asked Aug 30 '17 11:08

BigBrownBear00


People also ask

How do you check if it is a git repo?

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 know if a repository exists github?

You can use ls-remote : $ git ls-remote [email protected]:github/markup.

How do I find the .git folder?

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.

What is git ls-remote?

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.


1 Answers

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.)

like image 155
Maroun Avatar answered Sep 30 '22 07:09

Maroun