代码之家  ›  专栏  ›  技术社区  ›  cbutler

基于文件夹设置Git凭据

  •  1
  • cbutler  · 技术社区  · 8 年前

    是否可以让包含多个repo的文件夹使用一组凭据,而让另一个文件夹用户使用其他帐户?例如,我希望mac上有一个文件夹用于工作报告,一个文件夹用于个人信息,并根据我所在的文件夹自动切换git用户名和密码。

    3 回复  |  直到 8 年前
        1
  •  1
  •   cbutler    8 年前

    经过大量的搜索,我找到了我想要的东西(但可能不是我最初要求的那样)。我使用的解决方案是使用ssh。github有一些很好的帮助文档要遵循 here (认证中)但是基本上你必须 1。 Generate 每个帐户的ssh密钥并将其添加到ssh代理* 2。 Add github帐户的ssh密钥 三。在Mac上 Update ~/.ssh/config中的配置文件(向下滚动到“在github或heroku上的多个帐户之间进行选择”)

    • 注意:向ssh代理添加ssh密钥是暂时的。它们只在代理运行时持续。如果你杀死它或重新启动你的计算机,它们会丢失,直到你再次添加它们。

    为了解决这个问题,我创建了一个bash函数,将其添加到所有shell的bash rc文件中,这样我就可以将其作为终端命令调用。代码在这里:

    #!/bin/bash
    
    function addSshKeysToAgent() {
      echo "starting ssh agent:"
      echo ""
    
      eval 'ssh-agent -s'
      echo ""
    
      echo "adding identities: "
      ssh-add -K ~/.ssh/personal_rsa
      ssh-add -K ~/.ssh/work_rsa
      echo ""
    
      echo "Private keys loaded into SSH: "
      ssh-add -l -E md5
      echo ""
    
      echo "ssh keys added to ssh agent."
    }
    
    function clone() {
      addSshKeysToAgent
      git clone gh-work:Company/$1
    }
    

    将personal_rsa和work_rsa替换为ssh密钥文件的名称,将company替换为组织名称(如果您希望这样做的话),该名称来自您更新的配置文件。下面是我的配置文件的外观:

    Host *
     Port 22
     ServerAliveInterval 60
     ForwardAgent yes
     UseKeychain yes
     AddKeysToAgent yes
     IdentityFile ~/.ssh/personal_rsa
     IdentityFile ~/.ssh/work_rsa
    
    Host gh-personal
      Hostname github.com
      User git
      IdentityFile ~/.ssh/personal_rsa
      AddKeysToAgent yes
      UseKeychain yes
    
    Host gh-work
      Hostname github.com
      User git
      IdentityFile ~/.ssh/work_rsa
      AddKeysToAgent yes    
      UseKeychain yes
    

    通过所有这些,您应该能够使用以下命令从终端克隆repo:

    clone reponame.git
    

    我真心希望这能帮到别人。我花了几乎一整天的时间来搜索和破解这些东西,在我写这个回复的时候,我发现了一个非常好的要点,可以做我想做的事情 here

        2
  •  -1
  •   Mattia Righetti rmooney    8 年前

    This 导游可能就是你要找的人。

    看看 git config --local 具体地说,哪一部分将在执行命令的repo中应用更改。

        3
  •  -1
  •   Vikas Yadav    8 年前

    为特定文件夹配置git配置太简单了。只需使用两个git命令就可以实现这一点。只需输入要更改Git帐户信息的目录,然后执行以下操作:

    git config --local user.email "yourmail@example.com"
    
    git config --local user.name "yourName"
    

    干杯!

    推荐文章