Can I specify multiple users for myself in .gitcon

2018-12-31 16:57发布

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)?

标签: git
19条回答
弹指情弦暗扣
2楼-- · 2018-12-31 17:29

Another option to get git to work with multiple names / emails is by aliasing git and using the -c flag to override the global and repository-specific config.

For example, by defining an alias:

alias git='/usr/bin/git -c user.name="Your name" -c user.email="name@example.com"'

To see whether it works, simply type git config user.email:

$ git config user.email
name@example.com

Instead of an alias, you could also put a custom git executable within your $PATH.

#!/bin/sh
/usr/bin/git -c user.name="Your name" -c user.email="name@example.com" "$@"

An advantage of these method over a repository-specific .git/config is that it applies to every git repository when the custom git program is active. In this way, you can easily switch between users/names without modifying any (shared) configuration.

查看更多
骚的不知所云
3楼-- · 2018-12-31 17:33

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 setting user.name for each repository.

In your ~/.bashrc, add some simple aliases for the user and email:

alias ggmail='git config user.name "My Name";git config user.email me@gmail.com'
alias gwork='git config user.name "My Name";git config user.email me@work.job'
查看更多
人气声优
4楼-- · 2018-12-31 17:33

git aliases (and sections in git configs) to the rescue!

add an alias (from command line):

git config --global alias.identity '! git config user.name $(git config user.$1.name); git config user.email $(git config user.$1.email); :'

then, set, for example

git config --global user.github.name "your github username"
git config --global user.github.email your@github.email

and in a new or cloned repo you can run this command:

git identity github

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.

git config --global --unset user.name
git config --global --unset user.email
查看更多
深知你不懂我心
5楼-- · 2018-12-31 17:35

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

git config user.name "Your Name Here"
git config user.email your@email.com

whereas the default user / email is configured in your ~/.gitconfig

git config --global user.name "Your Name Here"
git config --global user.email your@email.com
查看更多
回忆,回不去的记忆
6楼-- · 2018-12-31 17:36

Or you can add following information in your local .git/config file

[user]  
    name = Your Name
    email = your.email@gmail.com
查看更多
伤终究还是伤i
7楼-- · 2018-12-31 17:36

Just add this to your ~/.bash_profile to switch between default keys for github.com

# Git SSH keys swap
alias work_git="ssh-add -D  && ssh-add -K ~/.ssh/id_rsa_work"
alias personal_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa"
查看更多
登录 后发表回答