1

I'm writing a shell script to automatically update several directories of git repos, and I want to ignore repos that do not have a remote origin to pull from. So far I have

if git remote 2>&1 >/dev/null; then

which successfully determines whether a directory is a git repo. But for any new git repo,

git remote

returns "origin" and an error status of 0. How do I distinguish between such repos and a repo where the command git pull will do something?

I could use

git remote show origin

but this will connect to the origin server if it exists, and I'd prefer a command that's fast and local.

4

2 回答 2

1

您可以随时检查.git/config是否存在[remote "origin"]?

#/bin/bash
if grep -Fxq '[remote "origin"]' .git/config
then
    echo "yes"
else
    echo "no"
fi
于 2012-11-26T16:54:48.050 回答
0

如果存储库具有origin,则该文件夹.git/refs/remotes/origin必须存在。

test -d .git/refs/remotes/origin

将返回 0。

于 2012-11-26T16:59:19.603 回答