git多账号

2021/7/2 23:21:19

本文主要是介绍git多账号,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1. 用一个秘钥不能放到多个不同的git账户上,会报错 Key is already in use,提交代码又提示无权限

image.png

```
➜  docker-lnmp git:(master) git push
ERROR: Permission to ogenes/docker-lnmp.git denied to yihuaiyuan.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
```

2. 可以再生成一个ssh秘钥, 注意修改一下filename

➜  .ssh pwd
/Users/ogenes/.ssh

➜  .ssh ssh-keygen -t rsa -C "yi"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/ogenes/.ssh/id_rsa): id_rsa_ogenes
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in id_rsa_ogenes.
Your public key has been saved in id_rsa_ogenes.pub.
The key fingerprint is:
SHA256:wz9cMcxxxxxxxxxxxxxxOHLQA yi
The key's randomart image is:
+---[RSA 3072]----+
|        ..o++++  |
|        .=E..+...|
|        =..o+  o+|
|     . = = = +o+o|
|      o . + +  .o|
|           .     |
+----[SHA256]-----+
➜  .ssh 


➜  .ssh ll id_rsa*
-rw-------  1 ogenes  staff   1.8K Mar 17  2020 id_rsa
-rw-r--r--  1 ogenes  staff   404B Mar 17  2020 id_rsa.pub
-rw-------  1 ogenes  staff   2.5K Jul  2 22:15 id_rsa_ogenes
-rw-r--r--  1 ogenes  staff   556B Jul  2 22:15 id_rsa_ogenes.pub

3. 然后添加到ssh-agent(很重要)

➜  .ssh ssh-add id_rsa                 
Identity added: id_rsa (ogenes@OgenesMac.local)
➜  .ssh ssh-add id_rsa_ogenes
Identity added: id_rsa_ogenes (yi)
➜  .ssh ssh-add -l                     
2048 SHA256:ZnzGkdJbL72m0P7j3KzaSb8V/J4WtjfZYa11eOSVoog ogenes@OgenesMac.local (RSA)
3072 SHA256:wz9cMcEFoukL41lhcYk3deqblPKBHN5dd8DntpOHLQA yi (RSA)

4. 配置~/.ssh/config

➜  .ssh vim config

#ogenes
Host ogenes.github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_ogenes

5. 设置到另一个git账户

image.png

6. 修改该账户下项目的git config,主要是url和user

➜  docker-lnmp git:(master) git remote set-url origin 'git@ogenes.github.com:ogenes/docker-lnmpgit' 
➜  docker-lnmp git:(master) git config user.name ogenes
➜  docker-lnmp git:(master) git config user.email ogenes.yi@gmail.com
➜  docker-lnmp git:(master) cat .git/config

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
[remote "origin"]
        url = git@ogenes.github.com:ogenes/docker-lnmp.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[branch "develop"]
        remote = origin
        merge = refs/heads/develop
[user]
        email = ogenes.yi@gmail.com
        name = ogenes

7. Test

➜  docker-lnmp git:(master) git push
Everything up-to-date



这篇关于git多账号的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程