前提:

必须知道怎样配置git账户,请參考git官方教程:https://help.github.com/articles/generating-ssh-keys

这个教程能教你怎样生成ssh-key,以及怎样加入ssh-key。

补充一点。怎样设置user.name和user.email。命令例如以下:

1)设置局部的user.name和user.email

git config user.name “xxxxxx”

git config user.email “xxx@xxx.com”

2) 设置全局的user.name和user.email

git config --gloable user.name “xxxxxx”

git config –gloable user.email “”





第一步:

建一个新的github账户。名字为testaccount,假设你已经有了。跳过此步(注:你之前已经有了一个老的账户了,假设没有,请看“前提”先来一个账户)

第二步:

假设自己会生成和配置ssh-key。那么要配其它账户首先要在生成一个ssh-key。当然新的ssh-key名称要和之前的有所差别,默认的private key 名称为id_rsa。新的key要换个名称。比方id_rsa2。这样生成一套key的名称分别为id_rsa2和id_rsa2.pub。而默认的文件为id_rsa和id_rsa.pub

在github上建一个新的repository。当然是基于你的第二个账户testaccount的。比如名称为test

第三步:

git clone下来

第四步:

然后要在.ssh文件夹下配置一下config文件(假设没有,创建它),样例例如以下:

# Default account

Host github.com

Hostname github.com

User git

IdentityFile ~/.ssh/id_rsa





# New account

Host github2.com

Hostname github.com

User git

IdentityFile ~/.ssh/id_rsa2





此时发现,这个配置看不懂啊,没关系,下边你能够使用命令,在一个test文件夹下运行git config -l 命令查看配置。例如以下所看到的:

core.symlinks=false

core.autocrlf=false

color.diff=auto

color.status=auto

color.branch=auto

color.interactive=true

pack.packsizelimit=2g

help.format=html

http.sslcainfo=/bin/curl-ca-bundle.crt

sendemail.smtpserver=/bin/msmtp.exe

diff.astextplain.textconv=astextplain

rebase.autosquash=true

mergetool.prompt=false

core.repositoryformatversion=0

core.filemode=false

core.bare=false

core.logallrefupdates=true

core.symlinks=false

core.ignorecase=true

core.hidedotfiles=dotGitOnly

remote.origin.url=git@github.com:testaccount/test.git

remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

branch.master.remote=origin

branch.master.merge=refs/heads/master

user.name= xxxxxx

user.email=xxx@xxx.com





注意这一行remote.origin.url=git@github.com:testaccount/test.git,这里的 github.com表示config文件中的hostname,事实上他并非hostname而是一个别名,这样解释:

git@别名: testaccount/test.git解析的路径为 git@github.com:testaccount/test.git而我配的default account的host和hostname刚好一样,假设仅仅有一个账户的时候,它并不表示别名而是路径。此时我们不须要配置config文件。我们设置config文件的目的是由于我们有两套key,分别用在两个repository,而我们须要分别指向这两个key。简单来说。我们是要通过host来找到key。即通过host映射到IdentityFile。





此时打开test文件夹下.get文件夹(隐藏文件夹)的config文件,内容例如以下:

[core]

repositoryformatversion = 0

filemode = false

bare = false

logallrefupdates = true

symlinks = false

ignorecase = true

hideDotFiles = dotGitOnly

[remote "origin"]

url = git@github.com:testaccount/test.git

fetch = +refs/heads/*:refs/remotes/origin/*

[branch "master"]

remote = origin

merge = refs/heads/master

[user]

name = testaccount

email = xxx@xxx.com





我们仅仅需把 url = git@github.com:testaccount/test.git改为 url = git@github2.com:testaccount/test.git,即使用了new account的host来配置。映射到了新的IdentityFile新的key,就可以保存文件再使用命令git config -l 查看配置例如以下:

core.symlinks=false

core.autocrlf=false

color.diff=auto

color.status=auto

color.branch=auto

color.interactive=true

pack.packsizelimit=2g

help.format=html

http.sslcainfo=/bin/curl-ca-bundle.crt

sendemail.smtpserver=/bin/msmtp.exe

diff.astextplain.textconv=astextplain

rebase.autosquash=true

mergetool.prompt=false

core.repositoryformatversion=0

core.filemode=false

core.bare=false

core.logallrefupdates=true

core.symlinks=false

core.ignorecase=true

core.hidedotfiles=dotGitOnly

remote.origin.url=git@github2.com:testaccount/test.git

remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

branch.master.remote=origin

branch.master.merge=refs/heads/master

user.name= testaccount

user.email=xxx@xxx.com





大功告成。你能够push代码了

git 在一台机器上配置多个账户的更多相关文章

  1. Git 在同一台机器上配置多个Git帐号

    在同一台机器上配置多个Git帐号 By:授客 QQ:1033553122 实践环境 win10 Git-2.21.0-64-bit.exe TortoiseGit-2.8.0.0-64bit.msi ...

  2. 【git】一台机器上使用不同的git账号

    1.生成一个新的自定义名称的公钥: ssh-keygen -t rsa -C "shangxiaofei3@163.com" -f ~/.ssh/sxfself 一直点击回车 执行 ...

  3. Hexo博客系列(二)-在多台机器上利用Hexo发布博客

    [原文链接]:https://www.tecchen.xyz/blog-hexo-env-02.html 我的个人博客:https://www.tecchen.xyz,博文同步发布到博客园. 由于精力 ...

  4. git学习笔记:一台电脑上配置两个git账户

    如何在一台电脑上配置两个git账户,现在云端仓库很多,有开源中国的 gitee.com 微软的 github.com 还有 gitlab.com 和 bitbucket.org 等等,下面是具体步骤 ...

  5. 一台电脑上配置多个git的ssh key

    前几天公司的代码库全部迁移到了阿里云上,在配置git的ssh key的时候遇到了一个问题,那就是自己的密钥在添加时提示已经存在,原来是自己的个人账号上已经添加过这个密钥了,公司分配的账号就不能再添加这 ...

  6. git+jenkins在windows机器上新建一个slave节点【转载】

    转至博客:上海-悠悠 前言 我们在跑自动化项目的时候,希望有单独的测试机能跑自动化项目,并且能集成到jenkins上构建任务.如果公司已经有jenkins环境了,那无需重新搭建. 只需在现有的平台基础 ...

  7. 如何在同一台机器上安装多个MySQL的实例

    转自:'http://www.cnblogs.com/shangzekai/p/4375271.html 最近由于工作的需要,需要在同一台机器上搭建两个MySQL的实例,(注:已经存在了一个3306的 ...

  8. 如何在同一台机器上安装多个MySQL的实例 转

    https://www.cnblogs.com/shangzekai/p/4375271.html 最近由于工作的需要,需要在同一台机器上搭建两个MySQL的实例,(注:已经存在了一个3306的MyS ...

  9. 如何在同一台机器上安装多个MySQL的实例(转)

    最近由于工作的需要,需要在同一台机器上搭建两个MySQL的实例,(注:已经存在了一个3306的MySQL的实例). 先说下,什么是mysql的多实例,简单的来说就是一台机器上安装了多个mysql的服务 ...

随机推荐

  1. mac 目录颜色设置

    1 export CLICOLOR=1  2 export LSCOLORS=gxfxaxdxcxegedabagacad

  2. vuec常用插件

    1.  实现下拉刷新和下拉加载效果 iscroll-probe.js 2.手势密码插件 patternLock.js 3.实现复制 clipboard.min.js

  3. 【转】C# 二进制,十进制,十六进制 互转

    //十进制转二进制 Console.WriteLine(Convert.ToString(69, 2)); //十进制转八进制 Console.WriteLine(Convert.ToString(6 ...

  4. OpenGL C#绘图环境配置

    OpenGL C#绘图环境配置   OpenGL简介 OpenGL作为一种图形学编程接口已经非常流行, 虽然在大型游戏方面DirectX有一定的市场占有率, 但由于OpenGL的开放性,可移植性等优点 ...

  5. cc.Label

    cc.Label 1:cc.Label是显示文字的组件;2:cc.Label属性面板:  String: 文本显示的内容;  Horiznotal: 水平对齐的方式: 左 右 居中;  Vertial ...

  6. DTD DOCTYPE

    总结: DOCTYPE是什么 ? 文档类型声明,告诉解析器用什么样的文档类型定义来解析此文档.DOCTYPE不存在或格式不正确会导致文档以兼容模式呈现.   标准模式与兼容模式各有什么区别? 如果页面 ...

  7. 反连接NOT EXISTS子查询中有or 谓词连接条件SQL优化一例

    背景 今天在日常数据库检查中,发现一SQL运行时间特别长,于是抓取出来,进行优化. 优化前: 耗时:503s 返回:0 SQL代码 SELECT * FROM MM_PAYABLEMONEY_TD P ...

  8. python3.x Day5 异常处理

    异常处理: 预计可能会发生的异常,明确如果发生,如何处理,不过一般不参与业务逻辑,也不要一次性捕捉全部异常,不然可能程序就不可控了. data={} mmm=[] try: #捕获异常, data[& ...

  9. C语言学习2

    C语言能够进行嵌套备注的方法: #if(0) do { scanf("%d", &n); getchar(); }]||n>a[M-]); #endif

  10. 本地==〉Github(push)

    [概述] Git中的项目是本地的,为了可以协同工作.需要将项目推送到GitHub服务器上. [步骤] 1) 第一步:创建项目 2) 第二步:在github上创建一个同名的空项目 ①选择Your rep ...