Setting Git username and email without using .gitconfig?
Asked Answered
J

3

12

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 [email protected]

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 protected]

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!

Jaco answered 2/3, 2017 at 11:5 Comment(0)
J
21

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 protected]

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.

Jaco answered 3/3, 2017 at 6:3 Comment(1)
Didn't know you could include config files from git config files. Nice solutionBetake
P
3

All settings can be set on a per repository basis : just drop the --global from your git config commands :

# from inside your repo :
git config user.name "Your Name"
git config user.email [email protected]

These config settings will then be stored in the .git/config file (in the repo folder), and will take precedence over global config options.

This works for any git config option (aliases, colors, ...)

note : you will have to repeat the config for any new clone you make, or for any other repo where you want to use this identity

Platinic answered 2/3, 2017 at 12:48 Comment(1)
…or one can add --local to be 100% explicit.Patinous
J
0

Another possibility is to use $XDG_CONFIG_HOME/git/config (usually ~/.config/git/config) for most of your configuration and include it in your dotfiles repository. You can then add .gitconfig to .gitignore and use it for user.name, user.email and other values you don't want committed.

See the git-config documentation for the order of which files it reads.

Jenny answered 3/2, 2020 at 8:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.