How to tell git to use the correct identity (name and email) for a given project?
Solution 1
git config user.email "[email protected]"
Doing that one inside a repo will set the configuration on THAT repo, and not globally.
Seems like that's pretty much what you're after, unless I'm misreading you.
Solution 2
You need to use the local set command below:
local set
git config user.email [email protected]
git config user.name 'Mahmoud Zalt'
local get
git config --get user.email
git config --get user.name
The local config file is in the project directory: .git/config
.
global set
git config --global user.email [email protected]
git config --global user.name 'Mahmoud Zalt'
global get
git config --global --get user.email
git config --global --get user.name
The global config file in in your home directory: ~/.gitconfig
.
Remember to quote blanks, etc, for example: 'FirstName LastName'
Solution 3
Edit the config file with in ".git" folder to maintain the different username and email depends upon the repository
- Go to Your repository
- Show the hidden files and go to ".git" folder
- Find the "config" file
- Add the below lines at EOF
[user]
name = Bob
email = [email protected]
This below command show you which username and email set for this repository.
git config --get user.name
git config --get user.email
Example: for mine that config file in D:\workspace\eclipse\ipchat\.git\config
Here ipchat is my repo name
Solution 4
If you use git config user.email "[email protected]"
it will be bound to the current project you are in.
That is what I do for my projects. I set the appropriate identity when I clone/init the repo. It is not fool-proof (if you forget and push before you figure it out you are hosed) but it is about as good as you can get without the ability to say git config --global user.email 'ILLEGAL_VALUE'
Actually, you can make an illegal value. Set your git config --global user.name $(perl -e 'print "x"x968;')
Then if you forget to set your non-global values you will get an error message.
[EDIT] On a different system I had to increase the number of x to 968 to get it to fail with "fatal: Impossibly long personal identifier". Same version of git. Strange.
Solution 5
As of Git 2.13 you can use an includeIf
in your gitconfig to include a file with a different configuration based on the path of the repository where you are running your git commands.
Since a new enough Git comes with Ubuntu 18.04 I've been using this in my ~/.gitconfig
quite happily.
[include]
path = ~/.gitconfig.alias # I like to keep global aliases separate
path = ~/.gitconfig.defaultusername # can maybe leave values unset/empty to get warned if a below path didn't match
# If using multiple identities can use per path user/email
# The trailing / is VERY important, git won't apply the config to subdirectories without it
[includeIf "gitdir:~/projects/azure/"]
path = ~/.gitconfig.azure # user.name and user.email for Azure
[includeIf "gitdir:~/projects/gitlab/"]
path = ~/.gitconfig.gitlab # user.name and user.email for GitLab
[includeIf "gitdir:~/projects/foss/"]
path = ~/.gitconfig.github # user.name and user.email for GitHub
https://motowilliams.com/conditional-includes-for-git-config#disqus_thread
To use Git 2.13 you will either need to add a PPA (Ubuntu older than 18.04/Debian) or download the binaries and install (Windows/other Linux).
Comments
-
Martin Jambon about 4 years
I use my personal laptop for both work and personal projects and I would like to use my work email address for my commits at work (gitolite) and my personal email address for the rest (github).
I read about the following solutions which are all either global or temporary:
git config --global user.email "[email protected]"
git config user.email "[email protected]"
git commit --author "Bob <[email protected]>"
- setting one of the
GIT_AUTHOR_EMAIL
,GIT_COMMITTER_EMAIL
orEMAIL
environment variables
One solution is to run manually a shell function that sets my environment to work or personal, but I am pretty sure that I will often forget to switch to the correct identity resulting in committing under the wrong identity.
Is there a way of binding a certain repository, project name, etc. to an identity (name, email)? What do people do?
-
Martin Jambon about 13 yearsI guess that should do it. I am expecting to have multiple local repositories for the same project, notably in order to switch between branches more easily without having to recompile everything. Maybe it's just a bad svn habit. Thanks.
-
Martin Jambon about 13 yearsIt sounds like a great trick but it didn't work for me. Both commit and push to gitolite remote were successful.
-
Seth Robertson about 13 years@Martin Jambon: Strange. I'm using git version 1.7.4.5
-
Seth Robertson about 13 years@Martin Jambon: You can also try setting your global username/email address to "x". Some older versions of git rejected too small values. Report what git version/os you have if this does not work.
-
Dan Ray about 13 years@Martin--I haven't been using git real long, but the sense I get is that using multiple local repos would be an unusual workflow for git. Even so, you'd only need to run this once on an instance of the repo, and it's set for the lifespan of that repo, so it's not so bad.
-
Seth Robertson about 13 years@Martin Jambon: Try 968 "x" and see if that works better for you.
-
dolmen almost 12 years@DanRay It is in fact a common setup if, as in Martin case, the local repo has local uncommitted files. Switching from one branch to another would not automatically rebuild files. If avoiding "to recompile everything" is a goal, using multiple local repos is the correct way to do it.
-
SashiOno over 7 yearsgit config --global user.email "[email protected]" Adding the --global make it work across all repos. source: help.github.com/articles/setting-your-username-in-git
-
naitsirch over 5 yearsThis is a manual task and may be forgotten. Automatic solutions are suggested by @dragon788 and me
-
topr over 4 yearsVery helpful and exactly what I was looking for. Combined with
git config core.sshCommand
for multiple private keys, gives complete seamless git identity management. -
Jason G about 4 yearsi've done this but git appears to ignore the local setting
-
Tic about 4 yearsJust to add that if you are still stuck with an old git version and you can't use @dragon788 answer, if your version is > 2.6.7, you can use worktree to do multiple checkout without having to config each one.
-
Ahmad Alfy over 3 yearsIt's worth mentioning than
git identity
is not a valid git command. I think your answer is inspired by this article which is great. Thank you -
Bengt over 3 yearsYes, source is mentioned in the post :)