• 周六. 3月 2nd, 2024

    Git 使用github时 git push每次都需要输入用户名和密码

    root

    3月 20, 2020 #Git, #github

    刚开始使用github时,代码库地址是使用的https的方式,因为这样简单,避免了设置ssh-key的麻烦。 但在使用linux的git管理代码时,每次git push代码时,就需要输入用户名,和密码。

    下面将版本库https方式修改为git

    git remote rm origin
    git remote add origin git@github.com:用户名/版本库名.git

    上面代码移除原来的https方式远程库,然后添加git方式远程库

    然后在本地生成rsa的key

    ssh-keygen -t rsa -C “用户名”

    然后出现一串提示,一路回车

    Generating public/private rsa key pair.
    Enter file in which to save the key (/root/.ssh/id_rsa): 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /root/.ssh/id_rsa.
    Your public key has been saved in /root/.ssh/id_rsa.pub.
    The key fingerprint is:
    SHA256:42Qmnz4G3ZYbTEmmYTxCNoMbYBb1UU3yfGmRSBNy6fc bianpeijiang
    The key's randomart image is:
    +---[RSA 2048]----+
    |  =+.o*o++*+..   |
    | o  oo.+=**..o   |
    |     o.o Bo.+    |
    |    .   . +o.    |
    |      ..S+ o .   |
    |      .B.o*   E  |
    |       .+. o     |
    |       .o .      |
    |       ...       |
    +----[SHA256]-----+

    根据上边提示, ssh-key放在 /root/.ssh/id_rsa.pub文件中

    cat /root/.ssh/id_rsa.pub

    即可看到内容,复制

    打开github.com中添加ssh key的地方 设置中的SSH and GPG keys,添加进去。

    git push尝试

    fatal: The current branch master has no upstream branch.
    To push the current branch and set the remote as upstream, use
    
        git push --set-upstream origin master
    

    根据提示执行

    git push –set-upstream origin master

    最后会出现Everything up-to-date 说明成功。

    root

    发表回复