我通常从事需要不同Git身份的不同Git项目。 我在新存储库中的工作流程是
- 克隆新的存储库。
- 转到克隆的存储库。
- 如果需要更改Git身份,请调用运行git config user.name“ Sandra Parsick”的shell脚本; git config user.email sparsick @ web.de`
我从未对这种解决方案感到满意,但它确实有效。 幸运的是, @ BenediktRitter的一条推文和@wosc的一条推荐了我的方法的两种替代方法。
#TIL :您可以配置多个git身份。 在工作和私人项目之间切换时很棒! https://t.co/0WNSCdYbOy
— Benedikt Ritter(@BenediktRitter) 2017年7月4日
@SandraParsick,或者如果您更喜欢https://t.co/KDm8iPxoxe (无耻的插件;),则可以使用显式切换器。
— Wolfgang Schnerring(@wosc) ,2017年7月5日
第一种方法基于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身份,请告诉我并写评论。
链接
翻译自: https://www.javacodegeeks.com/2017/07/pimp-git-manage-different-git-identities.html