How to Set Git Username and Email with git config

By 

•

Updated on

•

7 min read

Set Git Username and Email

Git uses your username and email address to identify each commit you make.

This article explains how to set or change your Git username and email with the git config command, both globally and for a single repository.

These changes apply only to future commits, and the name and email stored in existing commits do not change.

Quick Reference

For a printable quick reference, see the Git cheatsheet .

CommandDescription
git config user.nameShow the configured username
git config user.emailShow the configured email
git config --global --get user.nameShow the global username only
git config --local --get user.emailShow the repository email only
git config --list --show-originShow all config with sources
git config --global user.name "Name"Set global username
git config --global user.email "email"Set global email
git config user.name "Name"Set repository username
git config user.email "email"Set repository email
git config --global --editEdit global config file
git config --global --unset user.nameRemove global username
git commit --amend --only --reset-author --no-editFix the identity on the last commit

Checking Your Current Git Configuration

Before setting or changing your Git identity, you may want to check what is currently configured.

To see your current username and email:

Terminal
git config user.name
git config user.email

These commands show the configured values Git finds after combining its configuration scopes, with repository settings taking precedence over global ones. The longer form git config --get user.name returns the same result. To check a particular scope, pair --get with --global or --local:

Terminal
git config --global --get user.name
git config --local --get user.email

If the second command prints nothing, no email is set in the repository’s local configuration. Run git config user.email to check whether another scope supplies one.

To find the file supplying the selected username, use --show-origin:

Terminal
git config --show-origin --get user.name

To see all Git configuration values and where they come from:

Terminal
git config --list --show-origin
output
file:/home/user/.gitconfig      user.name=Your Name
file:/home/user/.gitconfig      user.email=youremail@yourdomain.com
file:.git/config                user.email=work@company.com

The --show-origin flag shows whether each setting comes from the global config (~/.gitconfig), system config (/etc/gitconfig), or local repository config (.git/config).

Setting Global Git Username and Email

The global Git username and email address are associated with commits on all repositories on your system that do not have repository-specific values.

To set your global commit name and email address, run the git config command with the --global option:

Terminal
git config --global user.name "Your Name"
git config --global user.email "youremail@yourdomain.com"

Once done, you can verify that the information is correctly set:

Terminal
git config --global --list
output
user.name=Your Name
user.email=youremail@yourdomain.com

The command saves the values in the global configuration file, ~/.gitconfig:

~/.gitconfigconf
[user]
    name = Your Name
    email = youremail@yourdomain.com

You can also edit the file directly, but using the git config command is usually easier and less error-prone.

If you prefer editing the file directly, open it with:

Terminal
git config --global --edit

Setting Git Username and Email for a Single Repository

Sometimes you may need to use a different username or email address for a specific repository. For example, using a personal email for open-source projects and a work email for company repositories.

To set the identity for a single repository, run the git config command without the --global option from within the repository directory.

Suppose you want to set a repository-specific username and email for a repository in ~/Code/myapp. First, switch to the repository root directory:

Terminal
cd ~/Code/myapp

Set the Git username and email address:

Terminal
git config user.name "Your Name"
git config user.email "youremail@yourdomain.com"

Verify that the changes were made correctly:

Terminal
git config --local --list
output
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=git@github.com:yourname/myapp.git
user.name=Your Name
user.email=youremail@yourdomain.com

The --local flag limits the output to this repository. Without it, git config --list merges the system, global, and repository files into a single list, which makes it harder to see what the repository itself overrides.

The repository-specific settings are stored in .git/config, located in the root directory of the repository.

Fixing Commits Made with the Wrong Identity

Correcting your configuration does not touch commits that already exist. If you have just discovered that the last commit carries the wrong name or email, you can rewrite it so that it picks up your corrected settings.

Warning
Amending rewrites the commit and gives it a new SHA. If you have already pushed it, replacing it on the remote requires a force push, which breaks history for anyone who based work on the original commit. Coordinate with your team first, and use git push --force-with-lease rather than --force.

After correcting your configuration, run:

Terminal
git commit --amend --only --reset-author --no-edit

The --only option keeps other staged changes out of the amended commit. The --reset-author option sets the author name and email to the current committer identity and renews the author timestamp. The --no-edit option keeps the existing commit message instead of opening an editor.

If you need a specific author name and email rather than those in your configuration, pass them directly:

Terminal
git commit --amend --only --author="Your Name <youremail@yourdomain.com>" --no-edit

Git stores two identities on every commit, the author and the committer. Both commands above change the author; the committer is always taken from your configuration at the moment you run the amend, which is why fixing your config first matters.

Confirm the result with git log :

Terminal
git log -1 --pretty="%an <%ae>"
output
Your Name <youremail@yourdomain.com>

The author line now shows the corrected identity.

Amending reaches only the most recent commit. To fix an older one, run git rebase -i HEAD~N, replacing N with the number of recent commits to include (for example, HEAD~3 for the last three). Mark the target commit as edit, run the amend command above, and then continue with git rebase --continue. The same cautions apply as when you change a commit message .

Automatic Identity Switching with Conditional Includes

If you work on multiple projects that require different identities, you can configure Git to automatically switch based on the repository location.

Add a conditional include to your ~/.gitconfig:

~/.gitconfigconf
[user]
    name = Personal Name
    email = personal@example.com

[includeIf "gitdir:~/Work/"]
    path = ~/.gitconfig-work

Then create a ~/.gitconfig-work file with your work identity:

~/.gitconfig-workconf
[user]
    name = Work Name
    email = work@company.com

Now any repository under ~/Work/ will automatically use your work identity, while all others use your personal identity. This approach works similarly to .gitignore files, where Git applies configuration based on context.

Removing a Git Configuration Value

To remove a configuration value, use the --unset option:

Terminal
# Remove global username
git config --global --unset user.name

# Remove repository-specific email
git config --unset user.email

FAQ

Does setting user.name and user.email log you in to GitHub or GitLab?
No. The user.name and user.email values only label your commits. They do not authenticate you to any remote. Authentication when you push or pull is handled separately, through SSH keys or a personal access token.

Why does Git ask for a username and password when you push?
That prompt is the remote server authenticating you, not your commit identity. Most hosts no longer accept your account password over HTTPS, so use a personal access token in place of the password, or use SSH keys to avoid HTTPS credential prompts.

Can you set a password with git config?
No. git config does not store account passwords. It manages settings such as your identity, aliases, and editor. Credentials for remotes are held by a credential helper or your SSH agent, not in user.name or user.email.

Conclusion

You can set or change your Git username and email with git config, either globally or for a single repository. If you manage multiple identities, conditional includes are the cleanest way to switch automatically based on repository location.

If you are new to Git, the Pro Git book is a solid next read.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page