本文介绍了是否可以在.gitconfig中为每个通配符域配置user.name和user.email?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台工作计算机,并且已全局配置为在提交时使用我的工作电子邮件和姓名.很好但是,我想制定一些规则,说如果回购源是github,请使用用户X和电子邮件Y"

I have a work computer, and it's configured globally to use my work email and name when committing. This is good. However, I'd like to make some sort of rule that says, "if the repo origin is github, use user X and email Y"

我意识到您可以为每个存储库创建一个配置条目,但是我希望它更加自动化:如果克隆为github,则它应该使用github用户详细信息.如果我下班克隆,则应该使用工作详细信息.

I realize you can make a config entry per repository, but I'd like it to be more automatic: if the clone is github, it should use github user details. If I clone from work, it should use work details.

有什么方法可以基于远程域进行全局配置吗?还是另一种方式?

Is there any way to configure this globally based on the remote domain? Or another way?

编辑/更新

我已经接受了以下答案,但是对脚本进行了一些修改:

I have accepted the answer below, but modified the script just a bit:

#!/usr/bin/env bash

# "Real" git is the second one returned by 'which'
REAL_GIT=$(which -a git | sed -n 2p)

# Does the remote "origin" point to GitHub?
if ("$REAL_GIT" remote -v 2>/dev/null | grep '^origin\b.*github.com.*(push)$' >/dev/null 2>&1); then

    # Yes.  Set username and email that you use on GitHub.
    export GIT_AUTHOR_NAME=$("$REAL_GIT" config --global user.ghname)
    export GIT_AUTHOR_EMAIL=$("$REAL_GIT" config --global user.ghemail)

fi

"$REAL_GIT" "$@"

主要的加法是需要两个git config值.

The primary addition is the requirement for two git config values.

git config --global user.ghname "Your Name"
git config --global user.ghemail "you@yourmail.com"

这避免了对脚本中的值进行硬编码,从而使其更具可移植性.也许吗?

This avoids hard coding the values in the script, allowing it to be more portable. Maybe?

推荐答案

(据我所知),Git并没有内置任何功能,但是以下shell脚本似乎运行可靠.修改它,使其具有您在登录GitHub时所需的用户名和电子邮件地址,然后将其另存为路径中名为"git"的可执行文件,该路径位于真实" git之前 .

There is nothing built into Git to do this (as far as I know), but the following shell script seems to work pretty reliably. Modify it to have the user name and email address you want when checking into GitHub, and then save it as an executable named "git" on your path somewhere before the "real" git.

#!/usr/bin/env bash

# "Real" git is the second one returned by 'which'
REAL_GIT=$(which -a git | sed -n 2p)

# Does the remote "origin" point to GitHub?
if ("$REAL_GIT" remote -v 2>/dev/null |
    grep '^origin\b.*github.com.*(push)$' >/dev/null 2>&1); then

    # Yes.  Set username and email that you use on GitHub.
    export GIT_AUTHOR_NAME='*** put your name here ***'
    export GIT_AUTHOR_EMAIL='*** put your email address here ***'

fi

"$REAL_GIT" "$@"

我在计算机上对ssh使用了类似的技巧-我希望ssh在运行时更改窗口背景颜色,然后在退出时将其更改回窗口-并且对我来说可靠.

I use a similar trick with ssh on my machine -- I want ssh to change my window background color when it runs, and then change it back when it exits -- and it has worked reliably for me.

此外,请注意,此代码经过硬编码,只能查看名为origin的远程服务器.

Also, note that this is hard-coded to only look at the remote that is named origin.

这篇关于是否可以在.gitconfig中为每个通配符域配置user.name和user.email?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 05:49