3

I have two github accounts – one personal and one for my job. In most of my repos, I use my work account and magit works fine. In a couple personal repos, I have this in .git/config:

sshCommand = ssh -i /my/home/directory/.ssh/personal/id_rsa -F /dev/null

This works well from the command line for things like git push, but when I try to push from magit, I get this error:

ERROR: Permission to <personalaccount>/unified_docs_switcher.git denied to <workaccount>.

Since my work account is mentioned in the error message, it seems like magit's connection to git is ignoring the line in .git/config.

Is there some other config I can add to magit to recognize this?

Is there a different/better solution in magit to using different github accounts?

(Edit: emacs 27.1 on MacOS from here, tested on magit 2.90.1 from melpa-stable and then 20210105.1030 from melpa)

1 Answer 1

4

There's a pure Git solution you can use in your global .gitconfig - I use an include.path config for personal stuff by default:

# .gitconfig
# default user email and key
[include]
        path = .gitconfig-personal

and that file contains name, personal email (which is used for SSH and identifying my GitHub account), and GPG key ID:

# .gitconfig-personal
[user]
    name = jidicula
    email = "johanan@forcepush.tech"
    signingkey = "<personalKeyID>"

Then, below that first [include] in my global .gitconfig, I have an includeIf.path work config to use if the repo matches a pattern provided as an argument to includeIf:

# .gitconfig
# When working with Work
[includeIf "gitdir:**/work/**/.git"]
        path = .gitconfig-work

and that file contains name, work email, and work email GPG key ID:

# .gitconfig-work
[user]
        name = jidicula
        email = "johanan@work.email"
        signingkey = "<workKeyID>"

Anything in this second config will overwrite whatever was defined by default - paired with the conditional includeIf, we get path-dependent Git config.

Putting the global .gitconfig all together, we get:

# .gitconfig
# default user email and key
[include]
        path = .gitconfig-personal
# When working with Work
[includeIf "gitdir:**/work/**/.git"]
        path = .gitconfig-work

Then you'd fill .gitconfig-personal and gitconfig-work with your context-specific configs like emails, keys, usernames, pull behaviour, commit message templates, etc.

You can read more about conditional includes here.

3
  • 1
    Wow, thanks for the detailed answer. I'll give that a try when I get the chance! Jan 24, 2021 at 16:08
  • 1
    Works like a charm! Thanks again! Feb 13, 2021 at 21:01
  • My pleasure, glad to help!
    – jidicula
    Feb 13, 2021 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.