前言

本文主要解决:
1)一台机器上设置两个账户,并同时上传更新到同一个仓库;
2)ssh-key的设置。

注:最近又用到了github,但是之前没有怎么系统学过,所以这里好好理一下。

解决方案

第一步:单账户的配置

1)首先我有两个github账户
aaa@qq.com
bbb@qq.com

2)我在aaa账户上建立了一个空的仓库叫做testgit

在这里插入图片描述这时候github会自动提示你:

  • 如果你有ssh,可以用ssh和https两种方式来pull 或者 push
  • 如果没有设置ssh,那么只能用https的方式来更新仓库

3)
然后我在本地新建了一个文件夹test,在这个文件夹里面,我运行:

# 初始化仓库
git init 

# 设置remote url 仓库地址
git remote add origin https://github.com/aaa/testgit.git

# 设置用户名和邮箱
git config --global user.name aaa
git config --global user.email aaa@qq.com

# 把仓库拉到本地文件夹
git push origin master 

# 修改下README.md
echo "modifyed by aaa" >> README.md

# 提交
git add -A && git commi -m " by aaa"

# 试图push到远程仓库
git push origin master

这时候,会提示你输密码,因为你是用的https
如果不想每次push都输密码。
那就要设置ssh,如下(参考 [1]):

# 生成一个ssh key
 ssh-keygen -t rsa -b 4096 -C "aaa@qq.com" 

这时候会提示:
> Enter a file in which to save the key (/home/you/.ssh/id_rsa): [Press enter]
为方便识别,我将其命名为:/home/you/.ssh/id_rsa_aaa  (而不是默认的路径)

> Enter passphrase (empty for no passphrase): [Type a passphrase]
这里我直接默认为空,详见 [2]

# 开启ssh-agent服务
eval "$(ssh-agent -s)"

# 把刚刚生成的ssh key加入到ssh-agent中
ssh-add ~/.ssh/id_rsa_aaa

4)登录github的aaa账户,参考[3],在setting设置中添加新的ssh key:
在这里插入图片描述

new即可,名字自己定,内容是 /home/you/.ssh/id_rsa_aaa.pub中的内容,详请参考 [3]

5)将remot url改为ssh连接:

# 修改为ssh
git remote set-url origin git@github.com:aaa/testgit.git

# 确认下设置成功没
git remote -v

6)然后aaa账户就可以在这里随心所欲的修改文件并同步到远程仓库了

第二部:多账户介入仓库的修改

这时候我的另外一个github账户,bbb@qq.com 也想更新这个仓库,怎么办?

其实很简单。

git config --global user.name "bbb"
git config --global user.email "bbb@qq.com"

即可。这时候你对本地文件做了修改,然后git add,git commit 以及最后git push
显示的就是 bbb@qq.com 对这个仓库做了修改。

其他(小)尝试

1)在bbb@qq.com 的github上新建个仓库 testgit

https://github.com/PERSmetric/testgit.git

2)在本地新建个test-2文件夹,在这里面 git clone https://github.com/PERSmetric/testgit.git

然后echo "111" >> README.md 创建一个README.md

3)试图更新至远程仓库:git add -A && git commit -m "update" && git push origin master
然后提示输入密码(因为用的是https的方式)
我输入了bbb@qq.com 及对应密码

发现可以上传

4)我又修改了一下README.md,然后这个git add -A && git commit -m "update" && git push origin master
输入的是aaa@qq.com及其密码,报错如下:

remote: Permission to bbb/testgit.git denied to DehengYang.
fatal: unable to access 'https://github.com/bbb/testgit.git/': The requested URL returned error: 403

ok,没关系。我现在把aaa之前生成的rsa.pub 里面的key加入到bbb的github中。

即:cat ~/.ssh/id_rsa_deheng.pub 中的内容复制到bbb的new ssh key中(见第一步中具体步骤,一样的操作)。
发现不行,github网页报错:
key is already in use
原因是我这个key已经用到之前的aaa账户中了。

原来如此,故重新创建一个ssh key(ssh-keygen -t rsa -b 4096 -C "aaa@qq.com",这里就不细说了)

5)然后修改https方式为ssh(用git remote set-url)

6)然后git push origin master,这时候可以顺利更新远程仓库。

小结

通过以上方式,我大概懂了git的账户上传设置。

在此记录之,分享之。

参考

1)Generating a new SSH key and adding it to the ssh-agent https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
这里是告诉我们如何设置ssh key的,来源于官网。

2)Working with SSH key passphrases https://help.github.com/en/articles/working-with-ssh-key-passphrases
有关passphrases,是给SSH keys加密的。

3)Adding a new SSH key to your GitHub account https://help.github.com/en/articles/adding-a-new-ssh-key-to-your-github-account
如何把ssh key加入到github账户中

4)About SSH https://help.github.com/en/articles/about-ssh
有关ssh的介绍。
有了它就不用再输密码。

Using the SSH protocol, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to GitHub without supplying your username or password at each visit.

5)Changing a remote’s URL https://help.github.com/en/articles/changing-a-remotes-url
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git即可。4

6)[github] 克隆、删除分支,覆盖本地仓库等操作 https://blog.csdn.net/weixin_39278265/article/details/87460029#8__102
自己以前的文章,现在看着还是很有用的,虽然以前是不系统零零散散的学,但是还是记了一些有意义的东西的。

7)Adding an existing project to GitHub using the command line https://help.github.com/en/articles/adding-an-existing-project-to-github-using-the-command-line

$ git add .
# Adds the files in the local repository and stages them for commit. To unstage a file, use ‘git reset HEAD YOUR-FILE’.
$ git commit -m “First commit”
# Commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use ‘git reset --soft HEAD~1’ and commit and add the file again.

这里的撤回操作有空学一学。

8)Your first time with git and github https://kbroman.org/github_tutorial/pages/first_time.html

$ git config --global user.name “Your name here”
$ git config --global user.email “your_email@example.com”

实用教程。

9)Start a new git repository https://kbroman.org/github_tutorial/pages/init.html

$ git remote add origin https://github.com/username/new_repo

同上,实用教程。

10)Setting your username in Git https://help.github.com/en/articles/setting-your-username-in-git

11)Multiple SSH Keys settings for different github account https://gist.github.com/jexchan/2351996
这个文章挺厉害的,先马住。

在这里插入图片描述
12)Remove credentials from Git https://stackoverflow.com/questions/15381198/remove-credentials-from-git/39608906#39608906

13)github account changing on windows https://stackoverflow.com/questions/22736895/github-account-changing-on-windows

14)How to switch git user at terminal? https://superuser.com/questions/1064197/how-to-switch-git-user-at-terminal

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐