为什么 github 在 git 推送文件到 /var/www/html 时询问用户名和密码?

Why github ask username and password when to git push files in /var/www/html?

我在.netrc中设置了用户名和密码。

cat .netrc
machine github.com  
    login  myname  
    password mypassword   
  1. git 推送 /tmp 目录中的文件:
    在 github 的网页中为此推送创建一个新的 repo test-tmp
    sudo cp  -R /var/www/html/cal  /tmp    
    sudo chown -R  debian.debian  /tmp/cal    
    cd  /tmp/cal    
    git init  
    git add  * 
    git commit -m "Add file"    
    git remote add origin https://github.com/gitsnippet/test-tmp.git       
    git push origin master  

Github不问用户名和密码,git push成功。

  1. git 推送 /var/www/html/cal 目录中的文件:
    在 github 的网页中为此推送创建一个新的 repo test-var
    注意:所有的sudo都不能省略,对于/var/www/html中的文件,没有sudo的git命令会出现如下错误

    error: could not lock config file /var/www/html/cal/.git/config: Permission denied.

    sudo git init
    sudo git add  *
    sudo git commit -m "Add file"
    sudo git remote add origin https://github.com/gitsnippet/test-var.git
    sudo git push origin master 

现在 git集线器将询问用户名和密码:

Username for 'https://github.com': xxxx
Password for 'https://gitsnippet@github.com': xxxx

如何让githtb读取.netrc中的用户名和密码,何时git推送/var/www/html中的文件?

sudo 进行推送意味着:

  • 不同的 git config 设置:检查 sudo git config credential.helper(与 git config credential.helper 相比)
  • 一个不同的 $HOME(其中 sudo ls ~/.netrc 可能不存在)

确保这些资源(git config.netrc)存在于两个环境中。

sudo cp  $HOME/.netrc   /root/.netrc