October 14, 2020
If you want to version control your files but don’t want to publish your name/email on Github, there’s an easy way to do this with multiple Git config files.
You can include any other files in your config with the [include]
section.
I have a main Git config file located at ~/.config/git/config
(yes, this is also a valid config file location, not just ~/.gitconfig
).
The file looks (for example) like this:
[user]
name = John Doe
email = john@doe.com
[alias]
s = status
co = checkout
fuckitall = reset --hard origin/master
[init]
templatedir = /home/myuser/.dotfiles/git/git_template
I’d like to have the alias section versioned and public, but I want to keep my user info and the path to the template directory private (also because this path changes based on which OS I’m using). What do?
First, create a new file in the same directory as your config file; for example, ~/.config/git/identity
.
In this file, add the stuff you want to keep private, and exclude it from version control:
[user]
name = John Doe
email = john@doe.com
[init]
templatedir = /home/myuser/.dotfiles/git/git_template
Then, change your config file ~/.config/git/config
to include the identity file:
[include]
path = identity
[alias]
s = status
co = checkout
fuckitall = reset --hard origin/master
And that’s all!
The path here is relative to the config file, so if the file and identity
are in the same directory, it will work.
If they’re not, you have to type the full path to the included file.
You can also include multiple files, just add more path = /path/to/file
statements to the [include]
section.
The ~
character is expanded to your home directory.
There’s also a way to conditionally include other config files, e.g. based on the current branch. For this, I’ll refer you to Git’s online documentation.
If you also want to remove your name/email from your entire commit history, bfg is probably the best option.