Search code examples
gitdotfiles

Setting Git username and email without using .gitconfig?


I have a dotfiles repository for macOS which contains the .gitconfig file. I would like to keep username and email separate from this file so that these variables are never committed as part of my public dotfiles repository.

One approach I have seen is to specify a file called .extra, keeping it in the ~/ directory (it is referenced it in .bash_profile) and set the Git variables in that file, so that they are external to my public repository, i.e. any public changes I make to .gitconfig will not include the username and email.

The problem is none of the approaches to setting these values are working for me. Following every attempt I get the following message:

Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly:

git config --global user.name "Your Name"

git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

git commit --amend --reset-author

I have the following in my .extra file, and his failed to work:

GIT_AUTHOR_NAME="First Last"

GIT_COMMITTER_NAME="First Last"

EMAIL=email@example.com

I have tried various approaches using these variables and others, as well as including and excluding quotation marks, and even prefixing these lines with export. At a bit of a loss!


Solution

  • I figured out the solution:

    Add the following to your dotfiles' .gitconfig file:

    [user] # These values are set in ~/.gitconfig_local

    [include] path = ~/.gitconfig_local

    Create a file in ~/ called .gitconfig_local and add the values you don't want committed or made public in your .gitconfig file. In this case:

    [user] name = First Last email = email@example.com

    As you might guess, the [include] command "links" to the local, secondary configuration file which is outside the repository and thus private. The remainder of your settings in .gitconfig such as your aliases, colour and editor settings etc. will still be public.