104

Is there a way to get the name of the user, given only their username?

Something like this output git show <username> (I know this doesn't work)

username: username
name: First Last
email: email@address

I know I can do this with a GitHub api call, but would prefer to keep it within the CLI.

tshepang
  • 12,111
  • 21
  • 91
  • 136
cwhelms
  • 1,731
  • 3
  • 12
  • 14

11 Answers11

151
git config user.name
git config user.email

I believe these are the commands you are looking for.

Here is where I found them

Cadoiz
  • 1,446
  • 21
  • 31
NewPersona
  • 1,691
  • 3
  • 11
  • 9
79

git config --list

git config -l

will display your username and email together, along with other info

Paul
  • 3,920
  • 31
  • 29
51

Git itself (the command line client, i.e. the "stupid content tracker") has no notion of user names, only GitHub does. In other words: there is no mapping of GitHub usernames to author/committer names and e-mails stored in a Git repository.

When creating a commit with Git it uses the configuration values of user.name (the real name) and user.email (email address). Those config values can be overridden on the console by setting and exporting the environment variables GIT_{COMMITTER,AUTHOR}_{NAME,EMAIL}.

Git doesn't know anything about GitHub's users, because GitHub is not part of Git. So you're only left with an API call to GitHub (I guess you could do that from the command line with a little scripting and make that a Git alias.)

knittl
  • 246,190
  • 53
  • 318
  • 364
  • 8
    Come on. How about https://stackoverflow.com/a/24051281/248616 `git config --list ` – Nam G VU Jul 26 '18 at 18:10
  • 4
    @NamGVU what do you mean? The OP was asking how to map _GitHub_ usernames to full names and emails of committers. And that is not possible. Using `git config --list` (or rather `git config user.name`) will only show _your_ currently configured author/committer name. It will not display any other user names or email addresses. It's interesting that so many answers which do _not_ answer get so many upvotes. Useful for a different usecase? Definitely. Answering the question? No. – knittl Mar 20 '21 at 10:47
  • this is the correct answer. The correct way to get the username is to query the hosting service (Github, Gitlab, Bitbucket, etc). – verboze Oct 24 '22 at 15:56
31

Try this

git config user.name

git config command stores and gives all the information.

git config -l

This commands gives you all the required info that you want.

You can change the information using

git config --global user.name "<Your-name>"

Similarly you can change many info shown to you using -l option.

Prakash
  • 579
  • 4
  • 14
14

While its true that git commits don't have a specific field called "username", a git repo does have users, and the users do have names. ;) If what you want is the github username, then knittl's answer is right. But since your question asked about git cli and not github, here's how you get a git user's email address using the command line:

To see a list of all users in a git repo using the git cli:

git log --format="%an %ae" | sort | uniq

To search for a specific user by name, e.g., "John":

git log --format="%an %ae" | sort | uniq | grep -i john
David
  • 688
  • 7
  • 13
6

Use this to see the logged in user (the actual git account):

git config credential.username

And as other answers the user email and user name (this is differenct from user credentials):

git config user.name
git config user.email

To see the list of all configs:

git config --list
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
  • `git config credential.username` works depending on how you logged in. If the user set up SSH remotes, this won't be set. – Carl Walsh Oct 26 '22 at 22:44
5

You can try this to get infos like:

  • username: git config --get user.name
  • user email: git config --get user.email

There's nothing like "first name" and "last name" for the user.

Hope this will help.

Ange Kouakou
  • 107
  • 1
  • 4
2

if you are using GitHub CLI (because git is already installed):

grep "user:" ~/.config/gh/hosts.yml

2022: Unless you manually set a token by hand, you need to use gh cli with git in order to be able to git push to your own GitHub repos. gh cli can be used to automatically set token credential use, as opposed to onetime (per login), or everytime (per action).

https://github.com/cli/cli

https://cli.github.com

Paul Wratt
  • 51
  • 5
  • I have multiple hosts configured in gh, so I can first grep for the line after `github.com:` then find the user: `grep 'github.com:' ~/.config/gh/hosts.yml -A 1 | grep "user:"` – Carl Walsh Oct 27 '22 at 18:52
  • You might prefer using: `gh api user --jq .name` (note: hosts.yml is written only if the user is already logged) – juj Jan 18 '23 at 13:08
2

Old thread, but since it's still the first hit on Google search, I'll add my two cents. Most answers are technically not correct. As @knittl indicated: to get the username, you'll have to query the service hosting your code, as git itself has no notion of users. You can configure variables like the user.name, but this can be anything and is not necessarily the user creating the commit.

For Github, a way to get the actual username is through their CLI tool gh. Once downloaded and authenticated, you can issue the following to get the username (using jq to retrieve the name):

gh api 'https://api.github.com/user' | jq .login

verboze
  • 419
  • 1
  • 7
  • 10
1

Add my two cents, if you're using windows commnad line:

git config --list | findstr user.name will give username directly.

The findstr here is quite similar to grep in linux.

Eugene
  • 10,627
  • 5
  • 49
  • 67
0

its So Simple You Just Copy and Past

For List Of All Config :- git config --list

and Also You Can Try :- git config -l

if you need to perticular Than You Can Use

user name :- git config user.name

user email :- git config user.email

If you need To add Config Than

user name :- git config user.name "name of user"

user email :- git config user.email "email of user"

Hope its HelpFull, Thank You

Paresh Shiyal
  • 534
  • 4
  • 15