I found this user management strategy somewhere, and it's been working great for me:<p><pre><code> git config --global --unset user.name
git config --global --unset user.email
git config --global --unset user.signingkey
git config --global user.useConfigOnly true
git config --global user.<id>.name "<name>"
git config --global user.<id>.email "<email>"
git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; :'
</code></pre>
So given that I have created two users, e.g. personal and work I run:<p><pre><code> git identity work
</code></pre>
in repos that need the work name/e-mail, and<p><pre><code> git identity personal
</code></pre>
in the ones that are private.
This is a great looking tool.<p>What I have been doing by hand for some time is putting code for different customers in different directories and having a conditional in `~/.gitconfig` to determine what config applies there:<p><pre><code> [includeIf "gitdir:~/projects-private/**"]
path = ./.gitconfig-private
[includeIf "gitdir:~/projects-client/**"]
path = ./.gitconfig-work
</code></pre>
Then in .gitconfig-private or .gitconfig-work I have all the usual gitconfig settings that apply, for example the [user] section...<p>Switching to the right directory thus automatically changes the settings.
git has a similar feature built in: <a href="https://deepsource.io/blog/managing-different-git-profiles/" rel="nofollow">https://deepsource.io/blog/managing-different-git-profiles/</a>
I currently use [karn](<a href="https://github.com/prydonius/karn" rel="nofollow">https://github.com/prydonius/karn</a>) to manage multiple `git` identities. Works pretty nice.
I rely on Git's `user.useConfigOnly` option to force myself to set my email per repository. This is what my `user` section in ~/.gitconfig looks like.<p><pre><code> [user]
name = Gurjeet Singh
# Tell Git to _not_ guess my name and email based on `whoami` and `hostname`
useConfigOnly = true
</code></pre>
With this in place, whenever I try to commit for the first time in a repository, Git prompts me<p><pre><code> *** Please tell me who you are.*
</code></pre>
I then add an email address to the repo-local config based on whether it's work or personal project.<p><pre><code> git config user.email me@example.com</code></pre>
What's the point of writing this simple tool in js? I'd like to use it but I don't have npm everywhere (and I don't want to grab a huge tree of dependencies just for editing a couple of lines on a text file...)
You can have a per-project git config, which to me is a more convenient solution.<p>For my work repo I configure the repo to use my work identity. For my personal repos I use my personal identity.<p>I do not need to switch identities when using the same repo.<p>Doing this, you only setup your identity once per repo not every time, which is safer/less error prone. i.e.: you wont leak your work email on github by accident.
Have seen more than a few attempts to solve this problem. Here's my version[0] and there's many more on GitHub. Wonder if there's an opportunity to unify our efforts & get something into git contrib?<p>[0] - <a href="https://github.com/bobbo/git-profile" rel="nofollow">https://github.com/bobbo/git-profile</a>
This line makes it not very useful, because I have to re-add accounts for each project.<p><pre><code> const store = require("data-store")({ path: process.cwd() + "/data.json" });
</code></pre>
I think a `git-user-data.json` file in a centralized location makes far more sense.
Author here!
I'm a freelancer working for multiple agencies at the moment. And One of the requirements was that I use the agency email for all commits.<p>So I made this as a sugar for
git config user.email whatever@gmail.com