是否可以为不同的项目使用不同的git配置

pri*_*iya 252 git git-config

.gitconfig通常存储在user.home目录中.

我使用不同的身份来处理CompanyA的项目以及CompanyB的其他内容(主要是名称/电子邮件).我如何才能拥有2种不同的git配置,以便我的签到不会使用名称/电子邮件?

sar*_*lin 244

git配置有3个级别; 项目,全球和系统.

  • project:项目配置仅适用于当前项目,并存储在项目目录中的.git/config中.
  • global:全局配置可用于当前用户的所有项目并存储在〜/ .gitconfig中.
  • system:系统配置可用于所有用户/项目并存储在/ etc/gitconfig中.

创建项目特定的配置,您必须在项目的目录下执行:

$ git config user.name "John Doe" 
Run Code Online (Sandbox Code Playgroud)

创建全局配置:

$ git config --global user.name "John Doe"
Run Code Online (Sandbox Code Playgroud)

创建系统配置:

$ git config --system user.name "John Doe" 
Run Code Online (Sandbox Code Playgroud)

正如您可能猜到的,项目会覆盖全局和全局覆盖系统.

  • 是否有可能制作一些"目录"配置?我在家做一些工作,并得到了工作项目和我自己的文件夹.所以我用git repos得到了文件夹〜/ job和〜/ my,并希望为它们下的项目配置不同的配置.例如,job/project1具有来自job/.gitconfig的配置. (53认同)
  • @theprogrammer 你们尝试过这个吗?这个对我有用。/sf/answers/3071929171/ (6认同)
  • 作为附录:只需 `git config user.name` 或 `git config user.email` 将显示 Git 将用于当前存储库的名称或电子邮件。 (3认同)
  • @Serge您是否曾经想过是否可以创建目录级别的配置?我现在有同样的问题。 (2认同)
  • 不,我将整个系统配置设置为个人数据,并制作 bash 脚本以在一个命令中将作业数据设置为某些项目配置。 (2认同)

Amb*_*ber 222

.git/config存储库的特定克隆中的文件是该克隆的本地文件.放置在那里的任何设置只会影响该特定项目的操作.

(默认情况下,git config修改.git/config,而不是~/.gitconfig- 仅--global修改后者.)


cre*_*ea1 173

从git版本2.13开始,git支持条件配置包括.在这个例子中,我们在~/company_a目录中克隆了公司A的回购,而在公司B的回购中~/company_b.

在你的.gitconfig你可以把这样的东西.

[includeIf "gitdir:~/company_a/"]
  path = .gitconfig-company_a
[includeIf "gitdir:~/company_b/"]
  path = .gitconfig-company_b
Run Code Online (Sandbox Code Playgroud)

.gitconfig-company_a的示例内容

[user]
name = John Smith
email = john.smith@companya.net
Run Code Online (Sandbox Code Playgroud)

.gitconfig-company_b的示例内容

[user]
name = John Smith
email = js@companyb.com
Run Code Online (Sandbox Code Playgroud)

  • 请注意,特定于目录的配置仅在存储库中作为子目录生效(例如“~/company_a/repo_1”)。如果您位于“~/company_a”等非存储库目录中并运行“git config user.email”,您仍然会获得全局电子邮件地址。**不要让这让你感到困惑。** (24认同)
  • includeIf有点挑剔,请参阅:/sf/ask/3074343401/#44117413 (4认同)
  • 很高兴看到 2.13 版本中添加了此功能。我已经使用 https://github.com/bddenhartog/git-profiles 一段时间了,但无法让它与 Tower 一起使用。 (2认同)
  • @Joseph你需要安装**git> = 2.13***(Ubuntu 16.04有git 2.7)*.通过[Git PPA](https://launchpad.net/~git-core/+archive/ubuntu/ppa)获取最新版本的git,它将工作:) (2认同)
  • 我只是想注意,“gitdir”文件夹必须以斜杠(“/”)结尾,否则“git”将忽略“ignoreIf”配置(实际上,它会导致“false”)。 (2认同)
  • 还值得一提的是,如果想在 .gitconfig 中保留一些默认值并且仅适用于该特定目录的不同电子邮件,则应在其末尾添加 includeif 子句。如果在开始时添加并在之后指定相同的属性,它们将被覆盖并且不会产生任何效果。 (2认同)

iva*_*d58 13

我通过以下方式为我的电子邮件执行此操作:

git config --global alias.hobbyprofile 'config user.email "me@example.com"'
Run Code Online (Sandbox Code Playgroud)

然后,当我克隆一个新的工作项目时,我只需要运行git hobbyprofile它,它将被配置为使用该电子邮件.


Ste*_*äwe 11

您还可以将环境变量GIT_CONFIG指向git config应该使用的文件.随着GIT_CONFIG=~/.gitconfig-A git config key value指定的文件被操纵.

  • 凉; 带有一些巧妙的外壳魔术,可以用来设置东西,因此git会在向上遍历当前回购目录树时选择它找到的第一个.gitconfig。谢谢! (2认同)

Ger*_*ich 10

另一种方法是使用direnv并将每个目录的配置文件分开。例如:

.
??? companyA
?  ??? .envrc
?  ??? .gitconfig
??? companyB
?  ??? .envrc
?  ??? .gitconfig
??? personal
   ??? .envrc
   ??? .gitconfig
Run Code Online (Sandbox Code Playgroud)

每个都.envrc应该包含如下内容:

export GIT_CONFIG_GLOBAL=$(pwd)/.gitconfig
Run Code Online (Sandbox Code Playgroud)

并且.gitconfig通常是具有所需值的 gitconfig。

这是我在自定义.gitconfig文件中实际拥有的内容:

[user]
    email = my.name@company.com

[include]
    path = ~/.gitconfig
Run Code Online (Sandbox Code Playgroud)

这里只user.email覆盖,其余配置取自默认~/.gitconfig.

  • 这需要 git 2.3.2+ https://lore.kernel.org/lkml/xmqqa6o3xj2e.fsf@gitster.g/T/#u (2认同)

mim*_*mim 9

明确地说,您还--local可以使用当前存储库配置文件

git config --local user.name "John Doe" 
Run Code Online (Sandbox Code Playgroud)


bca*_*ag2 8

谢谢@ crea1

一个小的变体:

由于它写在https://git-scm.com/docs/git-config#_includes上

如果花样以结尾/**将自动添加。例如,模式foo/变为foo/**。换句话说,它与foo所有内容都递归匹配。

所以我用我的情况:

[user] # as default, personal needs
    email = myalias@personal-domain.fr
    name = bcag2
[includeIf "gitdir:~/workspace/"] # job needs, like workspace/* so all included projects
    path = .gitconfig-job
Run Code Online (Sandbox Code Playgroud)

因此,如果项目目录位于my中~/wokspace/,则默认用户设置将替换为:

[user]
name = John Smith
email = js@company.com
Run Code Online (Sandbox Code Playgroud)