不同的项目可以有不同的 Git 配置吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8801729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to have different Git configuration for different projects?
提问by priya
.gitconfig
is usually stored in the user.home
directory.
.gitconfig
通常存放在user.home
目录中。
I use a different identity to work on projects for Company A and something else for Company B (primarily the name / email). How can I have two different Git configurations so that my check-ins don't go with the name / email?
我使用不同的身份为 A 公司处理项目,为 B 公司使用其他身份(主要是姓名/电子邮件)。我怎样才能有两种不同的 Git 配置,以便我的签入不与姓名/电子邮件一致?
采纳答案by Amber
The .git/config
file in a particular clone of a repository is local to that clone. Any settings placed there will only affect actions for that particular project.
.git/config
存储库的特定克隆中的文件是该克隆的本地文件。放置在那里的任何设置只会影响该特定项目的操作。
(By default, git config
modifies .git/config
, not ~/.gitconfig
- only with --global
does it modify the latter.)
(默认情况下,git config
修改.git/config
,而不是~/.gitconfig
- 只--global
修改后者。)
回答by teymourlouie
There are 3 levels of git config; project, global and system.
有 3 个级别的 git 配置;项目、全局和系统。
- project: Project configs are only available for the current project and stored in .git/config in the project's directory.
- global: Global configs are available for all projects for the current user and stored in ~/.gitconfig.
- system: System configs are available for all the users/projects and stored in /etc/gitconfig.
- project:项目配置仅适用于当前项目,并存储在项目目录的 .git/config 中。
- global:全局配置可用于当前用户的所有项目并存储在 ~/.gitconfig 中。
- system:系统配置可用于所有用户/项目并存储在 /etc/gitconfig.conf 中。
Create a project specific config, you have to execute this under the project's directory:
创建一个项目特定的配置,你必须在项目的目录下执行它:
$ git config user.name "John Doe"
Create a global config:
创建全局配置:
$ git config --global user.name "John Doe"
Create a system config:
创建系统配置:
$ git config --system user.name "John Doe"
And as you may guess, project overrides global and global overrides system.
正如您可能猜到的,项目覆盖全局和全局覆盖系统。
回答by crea1
As of git version 2.13, git supports conditional configuration includes. In this example we clone Company A's repos in ~/company_a
directory, and Company B's repos in ~/company_b
.
从 git 2.13 版本开始,git 支持条件配置包括. 在此示例中,我们将 A 公司的存储库复制到~/company_a
目录中,将 B公司的存储库复制到~/company_b
.
In your .gitconfig
you can put something like this.
在你的.gitconfig
你可以放这样的东西。
[includeIf "gitdir:~/company_a/"]
path = .gitconfig-company_a
[includeIf "gitdir:~/company_b/"]
path = .gitconfig-company_b
Example contents of .gitconfig-company_a
.gitconfig-company_a 的示例内容
[user]
name = John Smith
email = [email protected]
Example contents of .gitconfig-company_b
.gitconfig-company_b 的示例内容
[user]
name = John Smith
email = [email protected]
回答by bcag2
Thanks @crea1
谢谢@crea1
A small variant:
一个小变种:
As it is written on https://git-scm.com/docs/git-config#_includes:
正如它在https://git-scm.com/docs/git-config#_includes上写的:
If the pattern ends with
/
,**
will be automatically added. For example, the patternfoo/
becomesfoo/**
. In other words, it matchesfoo
and everything inside, recursively.
如果模式以 结尾
/
,**
将自动添加。例如,模式foo/
变为foo/**
。换句话说,它以foo
递归方式匹配内部的所有内容。
So I use in my case,
~/.gitconfig:
所以我在我的情况下使用
~/.gitconfig:
[user] # as default, personal needs
email = [email protected]
name = bcag2
[includeIf "gitdir:~/workspace/"] # job needs, like workspace/* so all included projects
path = .gitconfig-job
# all others section: core, alias, log…
So If the project directory is in my ~/wokspace/
, default user settings is replace with
~/.gitconfig-w:
因此,如果项目目录在 my 中~/wokspace/
,则默认用户设置将替换为
~/.gitconfig-w:
[user]
name = John Smith
email = [email protected]
回答by mim
To be explicit, you can also use --local
to use current repository config file:
明确地说,您还--local
可以使用当前存储库配置文件:
git config --local user.name "John Doe"
回答by ivand58
I am doing this for my email in the following way:
我通过以下方式为我的电子邮件执行此操作:
git config --global alias.hobbyprofile 'config user.email "[email protected]"'
Then when I clone a new work project, I have only to run git hobbyprofile
and it will be configured to use that email.
然后当我克隆一个新的工作项目时,我只需要运行git hobbyprofile
它,它将被配置为使用该电子邮件。
回答by Stefan N?we
You can also point the environment variable GIT_CONFIG
to a file that git config
should use. With GIT_CONFIG=~/.gitconfig-A git config key value
the specified file gets manipulated.
您还可以将环境变量GIT_CONFIG
指向git config
应该使用的文件。随着GIT_CONFIG=~/.gitconfig-A git config key value
指定的文件被操纵。
回答by German Lashevich
Another way is to use direnvand to separate config files per directory. For example:
另一种方法是使用direnv并将每个目录的配置文件分开。例如:
.
├── companyA
│ ├── .envrc
│ └── .gitconfig
├── companyB
│ ├── .envrc
│ └── .gitconfig
└── personal
├── .envrc
└── .gitconfig
Each .envrc
should contain something like this:
每个都.envrc
应该包含如下内容:
export GIT_CONFIG=$(pwd)/.gitconfig
And .gitconfig
is usual gitconfig with desired values.
并且.gitconfig
通常是具有所需值的 gitconfig。
回答by Eugene Yarmash
You can customize a project's Git config by changing the repository specific configuration file (i.e. /path/to/repo/.git/config
). BTW, git config
writes to this file by default:
您可以通过更改存储库特定的配置文件(即/path/to/repo/.git/config
)来自定义项目的 Git 配置。顺便说一句,git config
默认情况下写入此文件:
cd /path/to/repo
git config user.name 'John Doe' # sets user.name locally for the repo
I prefer to create separate profiles for different projects (e.g. in ~/.gitconfig.d/
) and then includethem in the repository's config file:
我更喜欢为不同的项目(例如 in ~/.gitconfig.d/
)创建单独的配置文件,然后将它们包含在存储库的配置文件中:
cd /path/to/repo
git config include.path '~/.gitconfig.d/myproject.conf'
This works well if you need to use the same set of options in multiple repos that belong to a single project. You can also set up shell aliases or a custom Git command to manipulate the profiles.
如果您需要在属于单个项目的多个存储库中使用相同的选项集,这很有效。您还可以设置 shell 别名或自定义 Git 命令来操作配置文件。
回答by Erich Meissner
I had an error when trying to git stash
my local changes. The error from git said "Please tell me who you are" and then told me to "Run git config --global user.email "[email protected]
and git config --global user.name "Your name"
to set your account's default identity." However, you must Omit --globalto set the identity only in your current repository.
尝试进行git stash
本地更改时出错。来自 git 的错误说“请告诉我你是谁”,然后告诉我“运行git config --global user.email "[email protected]
并git config --global user.name "Your name"
设置你帐户的默认身份”。但是,您必须省略 --global以仅在当前存储库中设置标识。