防止在找不到远程仓库时,从Github进行Git Pull操作时要求输入用户名和密码

3
我看了所有关于配置“git pull”用户名/密码的问题,但没有一个解决当远程代码库是404错误时的问题。
我曾经从github克隆过一个代码库。由于某种原因,代码库的所有者已经删除了代码库,因此“git pull”失败。然而,在它失败之前,“git pull”会要求输入用户名和密码。
这是因为我有一个克隆了github git代码库的目录,并且正在尝试通过shell脚本更新它们,该脚本进入每个目录并执行“git pull”。
$ git pull
ksshaskpass: cannot connect to X server 
error: unable to read askpass response from '/usr/lib/ssh/ssh-askpass'
Username for 'https://github.com': 
ksshaskpass: cannot connect to X server 
error: unable to read askpass response from '/usr/lib/ssh/ssh-askpass'
Password for 'https://github.com': 
remote: Repository not found.
fatal: Authentication failed for 'https://github.com/[ ... ]

为什么不直接移除丢失的存储库的远程连接? - antlersoft
您可以使用SSH密钥进行无凭证登录。这样不会要求您提供凭据。如果您想这样做... - Vishwanath
3个回答

3

我曾经遇到类似的情况,我的解决方案是通过执行以下操作为Git设置一个假凭据提供程序:

export GIT_ASKPASS="/bin/true"

这会导致拉取/克隆操作立即失败,而不是提示输入用户名和密码。虽然有点取巧,但我找不到其他简单的方法来防止提示。


唉,对我无效。它仍然要求凭据。我的系统配置可能有问题。我已经注意到将stdout和stderr重定向到/dev/null仍然会提示我输入密码。非常奇怪。 - Adam J Richardson

0
我创建了这个脚本来在执行拉取操作之前测试URL。 现在更新时不再提示输入用户名/密码。
#!/bin/bash

find . -type f | egrep '\.git/config' | while read fn ; do \
    dn=${fn%/.git/config} 
    url=$( git -C $dn remote show -n origin | awk '/Fetch URL: / { print $3 }' ) 
    wiki=${url%.wiki.git}
    if [ "$url" != "$wiki" ]; then
        url="${wiki}/wiki"
    fi
    url=$( echo $url | sed -e 's/git:/https:/' )
    wget -O /dev/null $url > /dev/null 2>&1  
    if [ 0 != $? ]; then 
        echo $dn $url 
    else 
        pushd $dn
        git pull
        popd
    fi
done

0
在我的情况下,我在执行git fetch时遇到了提示,但这些仓库在GitHub上并不存在。
尽管输入和输出被重定向(可能是因为git内部尝试使用/dev/tty),但这些提示仍然出现。
我通过将core.askPass配置为虚拟值(我选择了/bin/true,它会退出零并且不会产生任何输出)来调整我的命令。
这是我最终使用的命令:
git -c core.askPass=/bin/true fetch origin ...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接