设为首页 收藏本站
查看: 992|回复: 0

[经验分享] git 在一台机器上配置多个账户

[复制链接]

尚未签到

发表于 2018-1-14 06:26:19 | 显示全部楼层 |阅读模式
前提:  必须知道怎样配置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代码了

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-434881-1-1.html 上篇帖子: git远程库代码版本回滚方法 下篇帖子: 翻翻git之---溜的飞起的载入效果AVLoadingIndicatorView
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表