How to set different user.name and user.email for each Git branch?
Suppose I am using a shared computer account and working on a repository with multiple branches, each branch developed by a different user on that same computer account. It would be helpful to not have to switch the user.name and user.email every time a different user uses the computer to make commits to the repository.
How do I set a different user.name and user.email for each branch?
2 Solutions collect form web for “How to set different user.name and user.email for each Git branch?”
The usual approach is to:
- not rely on a local configuration to the repo
- rely only on the global configuration (the one in $HOME/.gitconfig)
That way, each user naturally has his/her own user.name/user.email whenever he/she does any commit in a shared repo.
If you really need to only update a branch per user, you could add a pre-commit hook making sure the right branch is about to be committed.
If you are using the same computer account:
a/ don’t 😉 It is not a best practice to share account
b/ setup an alias (one per user) which would setup that config
One approach that might work is:
git worktreeto get one folder per branch (instead of cloning your all repo multiple times)
- change your prompt in order to set the right
GIT_AUTHOR_NAME) and user.email (
GIT_AUTHOR_EMAIL): see “Set environmental variables in a particular directory”
Make a clone for each of you. Run
git config --local user.name xxx and
git config --local user.email firstname.lastname@example.org in each clone.
--local can be omitted since it’s the default.
But why not create an account for each of you in the same computer?