我们公司使用了许多定制的开源项目。每当我贡献上游分支时,我都会更改为使用我的个人电子邮件/姓名。有没有办法让每个分支都有 gitconfig?
比如我想要的是
[remote 'gerrit']
name = 'Personal Name'
[branch 'origin']
name = 'Name in company'
最佳答案
使用 Git 2.23(Q3 2019),无需 post-checkout hook,可以正式使用 git config
conditional includes ,没有脚本!
条件包含机制学会了选择基于
HEAD 当前所在的分支。
参见 commit 07b2c0e (2019 年 6 月 5 日)作者:Denton Liu ( Denton-L
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit 3707986 ,2019 年 7 月 9 日)
config
: learn the "onbranch:
"includeIf
condition
Currently, if a user wishes to have individual settings per branch, they are required to manually keep track of the settings in their head and manually set the options on the command-line or change the config at each branch.
Teach config the "
onbranch:
"includeIf
condition so that it can conditionally include configuration files if the branch that is checked out in the current worktree matches the pattern given.
git config
man page现在包括:
onbranch
:
The data that follows the keyword
onbranch:
is taken to be a pattern with standard globbing wildcards and two additional ones,**/
and/**
, that can match multiple path components.If we are in a worktree where the name of the branch that is currently checked out matches the pattern, the include condition is met.
If the pattern ends with
/
,**
will be automatically added.
For example, the patternfoo/
becomesfoo/**
.In other words, it matches all branches that begin with
foo/
. This is useful if your branches are organized hierarchically and you would like to apply a configuration to all the branches in that hierarchy.
所以在你的情况下:
[includeIf "onbranch:gerrit"]
path=gerrit
并且在 .git/gerrit
文件:
[remote 'gerrit']
name = 'Personal Name'
例子:
vonc@vonvb:~/gits/src/git$ git version
git version 2.23.0.b4
vonc@vonvb:~/gits/src/git$ git config includeIf.onbranch:next.path user1
vonc@vonvb:~/gits/src/git$ git config includeIf.onbranch:pu.path user2
vonc@vonvb:~/gits/src/git$ git config --local -l
core.repositoryformatversion=0
core.filemode=true
core.bare=false
...
includeif.onbranch:next.path=user1
includeif.onbranch:pu.path=user2
设置每个分支的配置文件:
vonc@vonvb:~/gits/src/git$ git config --file=.git/user1 user.name user1
vonc@vonvb:~/gits/src/git$ git config --file=.git/user1 user.email user1@email.com
vonc@vonvb:~/gits/src/git$ more .git/user1
[user]
name = user1
email = user1@email.com
vonc@vonvb:~/gits/src/git$ git config --file=.git/user2 user.name user2
vonc@vonvb:~/gits/src/git$ git config --file=.git/user2 user.email user2@email.com
vonc@vonvb:~/gits/src/git$ more .git/user2
[user]
name = user2
email = user2@email.com
检查它是否正常工作!
vonc@vonvb:~/gits/src/git$ git config user.name
VonC
vonc@vonvb:~/gits/src/git$ git checkout next
Branch 'next' set up to track remote branch 'next' from 'origin'.
Switched to a new branch 'next'
vonc@vonvb:~/gits/src/git$ git config user.name
user1
vonc@vonvb:~/gits/src/git$ git checkout pu
Branch 'pu' set up to track remote branch 'pu' from 'origin'.
Switched to a new branch 'pu'
vonc@vonvb:~/gits/src/git$ git config user.name
user2
vonc@vonvb:~/gits/src/git$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
vonc@vonvb:~/gits/src/git$ git config user.name
VonC
来自 master
至 next
至 pu
分支机构:三个不同user.name
! 每个分支一个。
没有钩子(Hook)。没有脚本。
如 Git 2.30(2021 年第一季度)所示,请确保使用 Git 2.24+,否则您可能会收到一些奇怪的错误消息:
参见 commit f1beaae (2020 年 11 月 19 日)Johannes Schindelin ( dscho
) .
(由 Junio C Hamano -- gitster
-- merge 于 commit 1242501 ,2020 年 11 月 30 日)
t1309
: use a neutral branch name in theonbranch
test casesSigned-off-by: Johannes Schindelin
The
onbranch
test cases touched by this patch do not actually try to include any other config. Their purpose is to avoid regressing on two bugs in theinclude.onbranch:<name>.path
code that we fixed in the past, bugs that are actually unrelated to any concrete branch name.The first bug was fixed in 85fe0e800ca ("
config
: work around bug with includeif:onbranch and early config", 2019-07-31, Git v2.23.0-rc1 -- merge).
Essentially, when reading early config, there would be a catch-22 trying to access the refs, and therefore we simply cannot evaluate the condition at that point. The test case ensures that we avoid emitting this bogus message:BUG: refs.c:1851: attempting to get main_ref_store outside of repository
The second test case concerns the non-Git scenario, where we simply do not have a current branch to begin with (because we don't have a repository in the first place), and the test case was introduced in 22932d9169f ("
config
: stop checking whetherthe_repository
isNULL
", 2019-08-06, Git v2.24.0-rc0 -- merge listed in batch #2) to ensure that we don't cause a segmentation fault should the code still incorrectly try to look at any ref.In short, neither of these two test cases will ever look at a current branch name, even in case of regressions. Therefore, the actual branch name does not matter at all. We can therefore easily avoid racially-charged branch names here, and that's what this patch does.
关于git - 每个分支使用 gitconfig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49827353/