ab168 发表于 2018-1-14 06:26:19

git 在一台机器上配置多个账户

前提:  必须知道怎样配置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文件,内容例如以下:

  repositoryformatversion = 0
  filemode = false
  bare = false
  logallrefupdates = true
  symlinks = false
  ignorecase = true
  hideDotFiles = dotGitOnly

  url = git@github.com:testaccount/test.git
  fetch = +refs/heads/*:refs/remotes/origin/*

  remote = origin
  merge = refs/heads/master

  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]
查看完整版本: git 在一台机器上配置多个账户