0

I'm a newbie to the Linux world as well as to git. I recently set up version control to manage my dotfiles by creating a Git repository called 'dotfiles'. However, whenever I commit any changes, I am asked for my Git user name and password and then my user name and email address gets automatically inserted into my .gitconfig file under the user section. For now, this is fine as the Git 'dotfiles' repository is private, but in the future, I may make this repository public. If so, I do not want my user name and email address to be shown in .gitconfig. How do I prevent this from happening?

I noticed in Matthias Bynens' .gitconfig file, there is no user section and there is the following commit section:

[commit]

    # https://help.github.com/articles/signing-commits-using-gpg/
    gpgsign = true

I also noticed that in Anish Athalye's .gitconfig file, there is no commit section but the user section contains this:

[user]
    useConfigOnly = true
  • 1
    note: author identity information is part of every commit to every repo, public and private. so in case you're concerned about privacy, your name and email address are there too – Fox Aug 16 at 20:42
  • @Fox But how to prevent my email address from showing up in .gitconfig? How come Matthias Bynens and Anish Athalye both do not have their email addresses in their .gitconfig files posted in their public repositories? – rplee Aug 16 at 20:48
  • I was merely pointing out additional concerns. As for your question itself, my assumption is that those who you've mentioned are making use of git's multiple configuration files. e.g. both ~/.gitconfig and ~/.config/git/config – Fox Aug 16 at 20:55
  • Did you, at some point, set git config --global user.email? – muru Aug 17 at 3:26
  • @muru Yes, I realized that was the issue. So I set the user name and email address locally instead (I was trying to set up a repository). I deleted the user name and email from my .gitconfig and reran the command inside my repository directory without --global. I verified this when I went into the repository's .git/config file. – rplee Aug 18 at 7:57
1

Git requires that you provide a name and an email address in order to commit. These values are inserted in every commit that you make and they must be nonempty and meet some basic validation. Conventionally, the name is your personal name, but that isn't required by Git itself, although projects you contribute to may require it.

However, having said that, you don't have to provide them in ~/.gitconfig, or even in any config file at all. Git will first of all read from the environment variables GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, GIT_COMMITTER_NAME, and GIT_COMMITTER_EMAIL if they exist. However, usually this is not a good idea because it overrides values when you use git commit --amend, and you want to use another technique.

git-commit(1) explains the other options:

In case (some of) these environment variables are not set, the information is taken from the configuration items user.name and user.email, or, if not present, the environment variable EMAIL, or, if that is not set, system user name and the hostname used for outgoing mail (taken from /etc/mailname and falling back to the fully qualified hostname when that file does not exist).

The author.name and committer.name and their corresponding email options override user.name and user.email if set and are overridden themselves by the environment variables.

The typical usage is to set just the user.name and user.email variables; the other options are provided for more complex use cases.

I typically leave my name in user.name and set EMAIL appropriately in my shell config. Do note that some programs, most notably Homebrew, are broken and filter EMAIL from the environment, so this won't work in all circumstances. It isn't a good idea to rely on the system GECOS field and mail setup being correct, because usually it isn't.

Finally, note that you can use a different config file, usually in ~/.config/git/config to store values that you don't want to store in the main ~/.gitconfig or check in to your dotfiles repository. I do this for values like commit.gpgsign, since not all systems I use have my private keys and can sign commits, and therefore it's system dependent whether I can sign and what keys I use.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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