Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

git - Using gitconfig per branch

Our company uses many customized opensource project. Whenever I contribute upstream branch I have change to use my personal email/name. Is there any way to have gitconfig per branch?

For example what I want is

[remote 'gerrit']
name = 'Personal Name'

[branch 'origin']
name = 'Name in company'
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With Git 2.23 (Q3 2019), no need for post-checkout hook, and you can use officially git config conditional includes, without script!
The conditional inclusion mechanism learned to base the choice on the branch the HEAD currently is on.

See commit 07b2c0e (05 Jun 2019) by Denton Liu (Denton-L).
(Merged by Junio C Hamano -- gitster -- in commit 3707986, 09 Jul 2019)

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.

The git config man page now includes:

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 pattern foo/ becomes foo/**.

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.

So in your case:

[includeIf "onbranch:gerrit"]
  path=gerrit

And in .git/gerrit file:

[remote 'gerrit']
  name = 'Personal Name'

Example:

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

Set your config files per branch:

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

Check that it is working!

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

From master to next to pu branches: three different user.name! One per branch.

No hooks. No script.


As illustrated with Git 2.30 (Q1 2021), make sure to use Git 2.24+ or you might get some strange error messages:

See commit f1beaae (19 Nov 2020) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit 1242501, 30 Nov 2020)

t1309: use a neutral branch name in the onbranch test cases

Signed-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 the include.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 whether the_repository is NULL", 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...