You can use curl -I to find out the status from fetching a web page without actually fetching it, and on GitHub you can fetch https://github.com/user/repo only if it's public.
So, if you run:
curl -I https://github.com/user/repo
and the status is 200, you know the repo is public, and you should not push to it.
Credits: my answer here is expanding on the idea described in this question: Check if git repo is public with HTTP request
My original scripting suggestion
To use this in a script, your could combine it with a grep:
if curl -I https://github.com/user/repo | grep "HTTP/2 200"; then
echo repo is public, do not push
else
echo repo is not public, yay
fi
Or, if you want to be safer, grep for 404 specifically instead of assuming not 200 is private:
if curl -I https://github.com/user/repo | grep "HTTP/2 404"; then
echo repo is private, yay
else
echo repo is public, do not push
fi
Caveat: GitHub returns 404 for both private repos and non-existent repos, so if you have a typo in the repo name, you'll also get a 404. But that's OK, because when you try to push to a non-existent repo on GitHub, the git push will error out with "fatal: repository not found" so you still won't accidentally push your code where you don't want to.
Better scripting option
Using the answer from Return only a HTTP status code from curl command, you can use curl to get the return code from fetching the page and save it into a variable to use in further logic:
HTTP_STATUS=$(curl -I https://github.com/user/repo -s -o /dev/null -w"%{http_code}")
if [[ "$HTTP_STATUS" == 200 ]]; then
echo repo is public
elif [[ "$HTTP_STATUS" == 404 ]]; then
echo repo is either private or does not exist
else
echo HTTP status code $HTTP_STATUS, not sure what this means...
fi