In my ~/.gitconfig
, I list my personal email address under [user]
, since that's what I want to use for Github repos.
But, I've recently started using git for work, too. My company's git repo allows me to commit, but when it sends out announcements of new changesets, it says they are from Anonymous because it doesn't recognize the email address in my .gitconfig
- at least, that's my theory.
Is it possible to specify multiple [user]
definitions in .gitconfig
? Or is there some other way to override the default .gitconfig
for a certain directory? In my case, I check out all work code in ~/worksrc/
- is there a way to specify a .gitconfig
for only that directory (and its subdirectories)?
Another option to get
git
to work with multiple names / emails is by aliasinggit
and using the-c
flag to override the global and repository-specific config.For example, by defining an alias:
To see whether it works, simply type
git config user.email
:Instead of an alias, you could also put a custom
git
executable within your$PATH
.An advantage of these method over a repository-specific
.git/config
is that it applies to everygit
repository when the customgit
program is active. In this way, you can easily switch between users/names without modifying any (shared) configuration.There is a simple solution that seems to work well for avoiding mistakes.
Simply remove the
[user]
section from your~/.gitconfig
, which will prevent you from making any commits without settinguser.name
for each repository.In your
~/.bashrc
, add some simple aliases for the user and email:git aliases (and sections in git configs) to the rescue!
add an alias (from command line):
then, set, for example
and in a new or cloned repo you can run this command:
This solution isn't automatic, but unsetting user and email in your global ~/.gitconfig would force git to remind you to set them manually in each new or cloned repo.
You can configure an individual repo to use a specific user / email address which overrides the global configuration. From the root of the repo, run
whereas the default user / email is configured in your ~/.gitconfig
Or you can add following information in your local
.git/config
fileJust add this to your ~/.bash_profile to switch between default keys for github.com