1

Background

I use multiple Github accounts on one computer.

To make sure that the name and email addresses (which I will call User Identity from here) associated with a commit match up to the name on the respective github account, I make use of .gitconfig files as described in the linked answer. Specifically, I use the IncludeIf keyword to designate that a directory should use a specific User Identity.

For example, if a repo is in ~/work/, it will use my "work" User Identity. But a repo in ~/personal/ will use my "personal" User Identity. From the standpoint of someone reading commits from these two repositories, commits would look like they have two different authors.

This system works to keep my identities separate without introducing a lot of complexity in everyday work. But mistakes can happen. I would like to be able to see what User Identity I am using within a particular repo before I issue a commit. That way if I see the wrong identity, I can see where the error in my setup is before I commit changes under the wrong identity.

This would normally be available via the git executable with a command like git config user.name or git config user.email as described by this answer. I am looking for a way to have emacs obtain and show me this information automatically/consistently. Magit seems to have a lot of functionality for git, so I tagged this question as such.

Question

Is it possible to see what User Identity (Either/both of the User Name and Email) I am using within a repository using Magit? Other non-Magit solutions would be acceptable as long as they are accessible via emacs.

Is it possible to have this information available prominently (such as in the mode line or magit status buffer)?


Note: I manage the multiple github accounts by using multiple SSH keys with separate ssh configs pointing to the same HostName but with different ssh keys. The ssh key used is controlled by which alias I provide as the host within git clone or any other repo-controlling command. This may not be fully relevant to my question, but I will provide it just in case.

2
  • I think this may be outside of the scope of magit. Magit uses the git executable, so any authentication scheme that works for git at the command line will work with Magit. If there is a method to check which authentication is active at the commandline, then it would be possible to show that in your modeline or integrate into magit-status. Unfortunately, I don't know how to query which ssh key is active for a particular path at the commandline. It might be useful to focus on finding an answer to that question, and then you can refocus this question on how to display that information in emacs?
    – dgtized
    Commented Aug 22, 2022 at 3:05
  • @dgtized Thank you for your feedback! After reading your comment, I realize that I put a lot of extraneous information in focus so I edited my question to get closer to what I am asking. I also changed the title to git in emacs per your comment about Magit's scope. To clarify: The SSH key is not necessarily tied to User Identity -- I have been able to commit using an SSH key while using a git config for a different account. You would normally be able to access User Identity with git with git config user.name/user.email within a repo. I would accept an answer that did not use Magit.
    – Malle Yeno
    Commented Aug 22, 2022 at 18:13

1 Answer 1

1

Magit has a defcustom called magit-status-headers-hook, which is responsible for filling in the header in magit-status. By default magit-insert-user-header, is not included in the list of status headers, but if that is added to magit-status-headers-hook defcustom, it will show the current user/email according to the git config properties in magit-status output as below:

Head:     main a commit description
Rebase:   origin/main a commit description
Push:     origin/main a commit description
Email:    First Last <[email protected]>

That may be sufficient, but if interested in displaying this information in the modeline, or elsewhere, take a look at how magit-insert-user-header is using (magit-get "user.name") and (magit-get "user.email") to fetch those properties.

1
  • 1
    Both solutions proposed worked like a charm! (magit-get "user.name") works as you described when added to the mode line, and the defcustom for magit-status-headers-hook works if you add in magit-insert-user-header as a typed field (it is not available as an option by default for check-boxes). Thank you! (As a fun aside, your solution worked so well that it helped me identify immediately that I had accidentally listed the path to a .gitconfig with a non-existent capital, which had caused the wrong user to be assigned as author. Looks like this solution is already paying off!)
    – Malle Yeno
    Commented Aug 24, 2022 at 2:26

Your Answer

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

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