4

I work on both work and personal projects on my machine, and for my work projects, I prefer to use my work email, and vice versa.

In shell, I use direnv to set GIT_COMMITTER_EMAIL and GIT_AUTHOR_EMAIL.

How do I achieve something like this in magit?

zsquare
  • 141
  • 3
  • 2
    Might be possible to use `.dir-locals.el` somehow, either modify `process-environment` or `magit-git-global-arguments`. – npostavs Feb 01 '16 at 10:48
  • i wrote https://github.com/wbolster/emacs-direnv to integrate direnv with emacs. but see my other answer, there are better options for your specific use case here. – wouter bolsterlee Nov 05 '19 at 14:19

4 Answers4

6

One way to do this is to set the author configuration in .git/config in each repository, in the same format as in ~/.gitconfig, i.e.:

[user]
    name = My Name
    email = me@example.com
legoscia
  • 6,012
  • 29
  • 54
  • 1
    Hmm... The problem with this approach, is that I need to do this in each repo, and I have a lot of work repos. With direnv, I can set this up in the folder that contains all the repos, and it works. Wondering if there is something similar I can do. – zsquare Feb 01 '16 at 07:15
6

you can do this with one-time git configuration. you can use it for email and so on, but also for using a different ssh key:

in ~/.gitconfig:

[includeIf "gitdir:~/work/"]
path = .gitconfig_work

in ~/.gitconfig_work:

[user]
email = …

[core]
sshCommand = ssh -i ~/.ssh/id_…_work -o 'IdentitiesOnly yes'

explanation: this makes git use another configuration, which specifies email and a ssh key (which you should create with a different filename and then add to your github account) when running git anywhere below the ~/work directory. in practice, this one time configuration makes it work for all current and future clones.

3

Anoter useful option is the git-identity package.

From its README:

This Emacs package lets you manage local Git identities, i.e. user.name and user.email options in .git/config, inside Emacs. It can be useful if you satisfy all of the following conditions:

- You have multiple Git identities on the same machine(s).
- You use Emacs.
- You (almost always) use magit for Git operations on your machine(s).
Manuel Uberti
  • 3,150
  • 18
  • 36
1

Same effective answer as @legoscia, but instead of modifying the file, I find that if you do

git config user.email me@example.com
git config user.name "My Name" 

It sets the local config, i.e .git/config

If you add the flag --global then it modifies your global config.

Thus you can have different name/email setup per repository, and can even check them by running git config user.email. When called without any arguements, it works as a GET command.

Karthik T
  • 131
  • 2
  • Added a comment to @legoscia's answer. Same applies here. – zsquare Feb 01 '16 at 09:29
  • @zsquare This only needs to be done once, Not sure if there is a parent folder customizability, but it cant be hard to script it into every child of a parent folder – Karthik T Feb 02 '16 at 06:59
  • @zsquare perhaps you can look at http://unix.stackexchange.com/questions/42785/can-git-configuration-be-set-across-multiple-repositories and specifically the last answer. – Karthik T Feb 02 '16 at 07:03