Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using gitconfig per branch

Tags:

git

git-config

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'
like image 967
XeIte Avatar asked Apr 14 '18 02:04

XeIte


2 Answers

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 [email protected]

vonc@vonvb:~/gits/src/git$ more .git/user1
[user]
    name = user1
    email = [email protected]


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 [email protected]

vonc@vonvb:~/gits/src/git$ more .git/user2
[user]
    name = user2
    email = [email protected]

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.

like image 147
VonC Avatar answered Oct 02 '22 04:10

VonC


You can use post-checkout hook for this. Run

$ touch .git/hooks/post-checkout 
$ chmod a+x .git/hooks/post-checkout 

Add contents to post-checkout script (edit names and branches as neccessary)

#!/bin/bash
# $3 "0" - checking out file. "1" - checking out branch.
[[ "$3" == "0" ]] && exit 0 
branch=$(git status --short -b | cut -d' ' -f2-)
case $branch in
  gerrit*)
    git config user.name "Personal Name"
    echo "changed user.name to Personal Name"
    ;;
  master*)
    git config user.name "Company Name"
    echo "changed user.name to Company Name"
    ;;
  *)
    echo "Some other branch, what should user.name be?"
    ;;
esac
like image 27
Moti Korets Avatar answered Oct 02 '22 03:10

Moti Korets