皮条客我的Git –管理不同的Git身份

我通常从事需要不同Git身份的不同Git项目。 我在新存储库中的工作流程是

  1. 克隆新的存储库。
  2. 转到克隆的存储库。
  3. 如果需要更改Git身份,请调用运行git config user.name“ Sandra Parsick”的shell脚本; git config user.email sparsick @ web.de`

我从未对这种解决方案感到满意,但它确实有效。 幸运的是, @ BenediktRitter的一条推文和@wosc一条推荐了我的方法的两种替代方法。

第一种方法基于Git功能“ Conditional Includes”(必需的Git版本至少为2.13)。 这个想法是您定义一个默认的Git身份,并为每个特定的目录单独设置Git身份。 这意味着,克隆到特定目录之一下的每个存储库都会自动具有其指定的Git身份。

第二种方法基于受Mercurial扩展hg-persona启发的Python脚本。 这个想法是您可以为每个Git存储库分别设置一个Git身份。 它是git config user。*命令系列的一个命令替代。

在接下来的两节中,我将总结如何设置以及如何使用这两种方法。 我已经在基于Debian的系统上对其进行了测试。 让我们从第一个开始。

总结几个Git存储库的Git身份

如上所述,该方法基于Git特征“ Conditional Includes”。 因此,请确保已至少在2.13版中安装了Git客户端。 假设,我们要有两个Git身份,一个用于Github,一个用于工作。 因此,在您的主文件夹中创建两个.gitconfig文件。

touch ~/.gitconfig_github
touch ~/.gitconfig_work

然后在相应的.gitconfig文件中添加特定的Git身份。

~/.gitconfig_github

[user]
   name = YourNameForGithub
   email = name@forgithub.com
~/.gitconfig_work

[user]
   name = YourNameForWork
   email = name@forwork.com

下一步是将这两个.gitconfig文件添加到我们的全局文件中,并指定何时使用它们。

~/.gitconfig

[user]
   name = defaultName
   email = default@email.com

[includeIf "gitdir:~/workspace_work/"]
   path = .gitconfig_work

[includeIf "gitdir:~/workspace_github/"]
   path = .gitconfig_github

现在,克隆到〜/ workspace_work /下的每个存储库都会自动具有Work的Git身份( .gitconfig_work ),克隆到〜/ workspace_github /下的每个存储库都会自动具有Github的Git身份( .gitconfig_github )。 否则,将使用默认的Git身份。

每个Git存储库分别设置Git身份

对于第二种方法,您必须从PyPI安装ws.git-persona。

sudo apt-get install pip # if PyPI isn't install
pip install ws.git-persona

然后,打开全局〜/ .gitconfig并添加角色。 在我们的案例中,我们添加了两个角色,一个用于Github,一个用于工作。

~/.gitconfig

[persona]
  github = YourNameForGithub <name@forgithub.com>
  work = YourNameForWork <name@forwork.com>

下一步,我们要在Git存储库中切换我们的Git身份。 现在可以使用命令git-persona 。 在下面的示例中,我们切换到Github的身份,然后切换到工作身份。

> git-persona -n github
Setting user.name="YourNameForGithub", user.email="name@forgithub.com"
> git config user.name
YourNameForGithub
> git config user.email
name@forgithub.com
> git-persona -n work
Setting user.name="YourNameForWork", user.email="name@forwork.com"
> git config user.email
name@forwork.com
> git config user.name
YourNameForWork

如果您还有其他方法来管理不同的Git身份,请告诉我并写评论。

链接

  1. 有关Git功能“ Conditional Includes”的博客文章
  2. git-personas的Github存储库

翻译自: https://www.javacodegeeks.com/2017/07/pimp-git-manage-different-git-identities.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值