I'm writing a bash script and I need a test to see whether a given remote exists.
Suppose, for concreteness, that I want to test whether the remote faraway
exists. If I've pushed something to faraway
, I can do if [ -d .git/refs/remotes/faraway ]; then ...
. But as far as I can see, the alias faraway
can still be defined even if .git/refs/remotes/faraway
does not exist.
One other option is to parse through the output of git remote
and see if faraway
appears there. But I'm wondering whether there is an easier way of checking whether faraway
is defined, regardless of whether .git/refs/remotes/faraway/
exists.
One thought: You could test exit status on git ls-remote faraway
. This will actually force communication with the remote, instead of just looking for its presence or absence locally.
if ! git ls-remote --exit-code faraway > /dev/null 2>&1; then
....
fi