← Git

Git Fundamentals

How do you set your name and email in Git, and what are they for?

Show answer — try answering out loud first

With git config user.name and git config user.email. They serve to sign your commits: each commit records who made it. With the --global flag the configuration applies to all your repositories; without it, only to the current repository.

Before making your first commit, Git needs to know who you are, because every commit records an author (name and email). This is what shows up in the history and on platforms like GitHub.

  • --global: saves the configuration for your user, across all repositories on your machine. It's what you do once when installing Git.
  • Without --global (or with --local): applies only to the repository you're in. Useful if you want to use a different email for a project (for example, your work one).

The email matters because GitHub associates commits with your account based on the configured email.

# Global configuration (once per machine)
git config --global user.name "Tu Nombre"
git config --global user.email "tucorreo@ejemplo.com"

# View the entire current configuration
git config --list
# Shows user.name, user.email, and more

# View a specific value
git config user.email
# tucorreo@ejemplo.com

# Configuration only for the current repo (overrides the global one here)
git config user.email "correo-del-trabajo@empresa.com"

# Set the default branch when initializing new repos
git config --global init.defaultBranch main

Forgetting to set the email and having commits come out with an incorrect or "unverified" author on GitHub. It's also common to confuse the levels: setting something with --global expecting it to apply to just one project, or the other way around. The rule: the local config (the repo's) takes priority over the global one.

"With git config user.name and git config user.email I set who I am, because every commit stores the author. With --global it applies to all my repos, which is what I do when installing Git; without --global it applies only to the current repo, useful if I need a different email for a work project. The local config takes priority over the global one. The email matters because GitHub associates commits with my account based on that email."

Quick challenge

You have your global user.email as personal@correo.com, but in a work repo you ran git config user.email trabajo@empresa.com. Which email are commits signed with in that repo?

See answer

With trabajo@empresa.com. The local configuration (the repo's) takes priority over the global one. In any other repository, personal@correo.com would still be used, because that local configuration only applies to the repo where you set it.